Commit Graph

976 Commits

Author SHA1 Message Date
Stefan Berger
ab267bfe4a swtpm: Only display profile capabilities when --tpm2 is given
Only display profile capabilities when --tpm2 is given since they are only
relevant when a TPM 2 is used.

Adjust test cases.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-23 19:44:57 -04:00
Stefan Berger
74a3d99b93 swtpm_setup: Give fields in tpm2_authblock better names
Give two oif the (unused) fields in the tpm2_authblock better names and
since these two and the continueSession fields are always initialized with
'0', simplify the initializer macro to only take one argument.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-18 15:07:36 -04:00
Stefan Berger
1eb06b6f79 swtpm_setup: Always lock storage while creating initial state
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
38aa3d972c swtpm: Display tpmstate-opt-lock as a new capability
Display the new capability tpmstate-opt-lock, adjust test cases,
and document it in the swptm man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
1d17d09158 swtpm: Add support for lock option parameter to tpmstate option
To support storage backend locking on the file backend, add support for a
lock option parameter to the --tpmstate option. By default the value of
this option (if not given) has to be 'true' for the dir backend, since this
backend has always been locking, and 'false' on the file backend, since
this backend did not lock so far.

If the user chooses no storage backend locking then SWTPM_NVRAM_Unlock &
SWTPM_NVRAM_Lock_Storage do not call the backend for locking at all
anymore.

Document the new option parameter in the swtpm man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
aa483aeb6d swtpm: nvstore_linear: Add support for file-backend locking
Add support for locking the storage file using fcntl(fd, F_SETLK, ...).
Since fcntl needs a file descriptor of the actual storage file, call
SWTPM_NVRAM_LinearFile_DoOpenURI() to open the file in case it has not
been opened, yet. In case of error close the file again but be careful
about the fact that it may not have been mmap'ed, yet.

Since now all backends have .lock and .unlock nvram_backend_ops, they can
be called without checking for a NULL pointer.

Extend an existing test case with a file-backend storage lock test.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
4be72bc65a swtpm: Remove broken logic to check for neither dir nor file backend
Remove the broken logic to check for neither dir:// nor file:// backend.
If an unknow backend type is used, then it will be detected later on
and an error message will be printed out. Even though the logic was
broken it didn't seem to cause failures.

Also have tpmstate_set_mode return void since it cannot fail.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
eb5875793e swtpm: nvstore_linear: Add comment to SWTPM_NVSTORE_LINEAR_MAX_STATES
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
fc57f557d8 swtpm: nvstore_linear_file: Initialize file descriptor to -1
Since 0 is a valid file descriptor and checks for valid file descriptors
is typically '>= 0', initialize the file descriptor to -1 to indicate
that it is unused.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
8ba4ec3c43 swtpm: Refactor parts into SWTPM_NVRAM_LinearFile_DoOpenURI
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-15 10:49:01 -04:00
Stefan Berger
86e8f8b861 swtpm_setup: Rename parameter from optarg to opt_arg (OS X,Wshadow)
Rename the optarg parameter to opt_arg since compiler on OS X
gives a -Wshadow warning.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
47f37b0551 swtpm_ioctl: Rename parameter from optarg to opt_arg (OS X,Wshadow)
Rename the optarg parameter to opt_arg since compiler on OS X
gives a -Wshadow warning.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
799290d229 swtpm_bios: Rename parameter from optarg to opt_arg (OS X,Wshadow)
Rename the optarg parameter to opt_arg since compiler on OS X
gives a -Wshadow warning.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
489483f100 swtpm: Fix typo in header guard #define
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-13 19:46:15 -04:00
Stefan Berger
06168658cb Revert "swtpm: Print message in case error response is too long"
This reverts commit 8d4b247e3d since
CMD_GET_STATEBLOB can have more than 4 bytes in response when an
error happened (long-standing protocol error).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-12 13:19:05 -04:00
Stefan Berger
8d4b247e3d swtpm: Print message in case error response is too long
All error messages must only be 4 bytes long. Print an error message if
this is not the case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-11 17:34:36 -04:00
Stefan Berger
3a0ff17329 swtpm_ioctl: Use ptm_cap_n for non-CUSE PTM_GET_CAPABILITY response
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-11 17:34:36 -04:00
Stefan Berger
47c96a0c64 swtpm: Use ptm_cap_n to build PTM_GET_CAPABILITY response
Use the new ptm_caps_n to create the response for PTM_GET_CAPABILITY in the
socket and chardev implementations. Due to how the result was created
before, the tpm_result part of the response will always have to be
TPM_SUCCESS, since it was always 0 before.

Leave the CUSE implementation untouched since there the result was returned
in native endianess resulting in the first 4 bytes carrying the capability
flags.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-11 17:34:36 -04:00
Stefan Berger
f29b870b5a cuse: Move display of help screen into function
Move the display of the help screen into its own function to be able
to usage fprintf with the help screen as format string to avoid static
analyzer warnings.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-08 12:11:37 -04:00
Stefan Berger
3f551e1dc1 swtpm: Implement --print-info to run TPMLIB_GetInfo with flags
Implement --print-info that takes a number as argument and uses this number
as flags to call TPMLIB_GetInfo with. Display the JSON string and exit.

Extend the man page and update other parts where swtpm_ioctl is not necessary
anymore to use.

Extend a test case to also check that swtpm now returns the same result as
swtpm_ioctl does.

Append cmdarg-print-info to printed out capabilties. Adjust test cases.
(Expect 'profiles' to always be part of capabilties JSON.)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-07 11:09:04 -04:00
Stefan Berger
c6db0e3185 swtpm: Support 'remove-disabled' for 'custom:'-prefixed profile names
Extend the support for 'remove-disabled' option parameter of the --profile
option to also work for profiles whose name starts with 'custom:'.

Modify an existing test case to use custom:test as profile name with this
option.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 18:20:18 -04:00
Stefan Berger
34e5103045 swtpm_setup: Implement --print-profiles to display all profiles
Implement support for the --print-profiles option to search the local
and distro profiles directories for profiles (files with .json extension)
and then get the built-in ones from swtpm. Print them all to stdout.

Extend a test case and add description to the man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 09:35:17 -04:00
Stefan Berger
3d7b2445c3 swtpm_setup: Add profile entries to swtpm_setup.conf written by swtpm_setup
Have swtpm_setup --create-config-files write profile entries into
swtpm_setup.conf.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 09:35:17 -04:00
Stefan Berger
5486f0437f swtpm_setup: Add support for --profile-name option
Add support for --profile-name option that searches for a profile in a
configurable local directory or a distro directory
(typically /usr/share/swtpm/profiles).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-02 09:35:17 -04:00
Stefan Berger
7198e0d716 swtpm_setup: Accept profiles with name starting with 'custom:'
Accept profiles that start their name with 'custom:' and do not exceed
32 characters. The content of these profiles will be derived from the
built-in 'custom' profile unless Algorithms, Commands, or Attributes
are provided on the command line.

Adjust a test case to test with profile name starting with 'custom:'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-01 19:12:05 -04:00
Stefan Berger
82fb09c6a2 selinux: Change write to append for appending to log
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-01 09:57:34 -04:00
Stefan Berger
2d4a4c57cc selinux: Add rule for logging to svirt_image_t labeled files from swtpm_t
I was able to recreate the issue on one of my machines where swtpm_t
was trying to append to the log labeled with svirt_image_t. On another
machine this combination of labels does not seem to cause a problem.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2306817
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-30 21:32:22 -04:00
Stefan Berger
aa78a302ec swtpm: Display stderr in case swtpm --print-capabilities failed
When swtpm --print-capabilities for example fails to be able to access
the log file that swtpm_setup logs into, then it may fail to run. In
this case log the stderr output of swtpm.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-30 17:01:04 -04:00
Stefan Berger
2e2124928f swtpm_setup: Return error if reading of config file failed
Return an error if the reading of the config file failed so that
config_file_lines can never be NULL. Remove all checks for
config_file_lines == NULL.

It's very unlikely reading of the config file failed since there's a file
access check right before it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-27 15:12:58 -04:00
Stefan Berger
2e42685e6d swtpm_setup: Move checking access to and reading of config file into function
Move the checking of access to and reading of the configuration file into
its own function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-27 15:12:58 -04:00
Stefan Berger
3a1be75d04 swtpm_setup: Fix counting of profile related options
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 16:58:06 -04:00
Stefan Berger
e91b27911f swtpm: Initialize empty array
Initialize the empty array authpolicy[0] to quiet a static analyzer.
This array only serves the purpose of providing a non-NULL pointer
when passed to memconcat.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 13:22:56 -04:00
Stefan Berger
c0589349c0 swtpm: Return TPM_FAIL from invalid header version case
Return TPM_FAIL in case of an invalid header version number to avoid a
potential segmentation fault when accessing an unavailable buffer due to
the header parser not returning an error code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 13:22:56 -04:00
Stefan Berger
8ea14c6ea5 swtpm: Check gerror before calling g_error_free
To avoid glib warning messages check the gerror before calling
g_error_free with it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 13:22:56 -04:00
Stefan Berger
82e3f38a82 swtpm_setup: Support default profile from file in swtpm_setup.conf
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
72f2022827 swtpm_setup: Support --profile-file-fd to read profile from file descriptor
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
d5bfdafdc4 swtpm_setup: Support --profile-file <file> to read profile from file
Enable reading a profile from a file. Pass it as open file descriptor to
swtpm.

Adjust one test case to read a profile from a file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
d488dbe7fd swtpm: Support --profile fd=<fd> to read profile from file descriptor
Enable reading a profile from a given file descriptor.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
3171cbae02 swtpm: Support --profile file=<filename> to read profile from file
Enable reading a profile from a file.

If setting a profile fails, display the profile in the error message.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
9ef3db088a swtpm: Return error if json_parser_get_root returns NULL
An empty input string will for example cause json_parser_get_root to return
NULL. Return early in this case to avoid glib error messages.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
12f7f004e7 swtpm: check for len > 0 before calling strncmp
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
3d0fb38187 swtpm: Convert json_get_submap to use g_autoptr on variables
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 13:37:15 -04:00
Stefan Berger
748e6c0f66 swtpm: Initialize seccomp_action with default value
Initialize seccomp_action with a default value that it will also
get assigned when handle_seccomp_options is called. This is done
to silence a static analyzer even though it's not necessary.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 09:20:08 -04:00
Stefan Berger
65c7c72959 swtpm: Implement concat_varrays and fix casts in concat_array calls
Implement concat_varrays that takes gchar ** as input and returns gchar **.
The concat_arrays has the identical implementation but takes const gchar **
as parameters and returns const gchar **. Use concat_arrays from
concat_varrays. Adjust all callers to cast arrays with constant strings to
const gchar *[] when calling concat_arrays. Modify some callers to call
concat_varrays now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 09:20:08 -04:00
Stefan Berger
dba5fa616e utils: Implement wrappers for g_spawn_sync and g_spawn_async
Implement warpper for g_spawn_sync and g_spawn_async that that take argv
and envp arguments as 'const gchar **' since they will be called like
this from swtpm_localca and swtpm_setup. Internally glib also uses the
parameters like this when calling fork_exec() for example.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-23 09:20:08 -04:00
Stefan Berger
402c31df25 swtpm_setup: Initialize key_description with a default string
Initialize key_description with a default string rather than using it
possibly uninitialized. In practice it would never be used uninitialized
since if a platform certificate is to be created, then the ek certificate
would have also been created and therefore swtpm2->ops->create_ek()
would have been called and set key_description.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
bdb8327bab swtpm_bios: Use const char * where a constant string is assigned later
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
871fb98167 swtpm_bios: Prefix all global variables with g_
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
40eb964a81 swtpm_cert: Add void to functions without parameters
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
6748e30f30 swtpm: cuse: Rename global msg to g_msg to avoid shadowing in places
Avoid shadowing global msg through local variables with the same name
by renaming the global msg to g_msg.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
60ca849a57 swtpm: cuse: Remove unused passwd variable
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
245e3d79a3 swtpm: cuse: Add SWTPM_ATTR_UNUSED to an unused variable
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
d8b2ab4758 swtpm: Use variable for malloc rather than size of struct
Avoid this type of complaint from static analyzer:

src/swtpm/tpmlib.c:392:37: note: Result of 'malloc' is converted to a
   pointer of type 'unsigned char', which is incompatible with sizeof
   operand type 'struct tpm_resp_header'

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
9ae02d555f swtpm: Cast parameter to OSSL_PARAM_construct_utf8_string
OSSL_PARAM_construct_utf8_string takes a char * as parameter.
The OpenSSL code base casts constant strings to char *, so we can do this
also.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
d6c9ebbcbd swtpm: Prefix global variables with g_
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
45063edb9f swtpm: Make tpm_running a parameter to mainLoop
Remove tpm_running as a global variable to make it a parameter to
mainLoop.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
de5a99f9bb swtpm: Convert char * initialized with constant strings to const char *
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
f28d2ec722 swtpm: Cast to char * to avoid arithmetic on void *
To avoid arithmetic on void * cast the variable to char *.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
0056d9b5ee swtpm: Change order of parameters to calloc
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
1416fbf4dc swtpm: Make variable a const char * that is assigned a constant string
serverdata will be assiged a const char * later on, therefore make it a
const char *. This can then also be passed into options_parse.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
91178e92aa swtpm: Convert options_parse function to take const char *
Some function pass a const char * into the options parse function.
Therefore, convert it to accept a const char * now.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-22 16:03:36 -04:00
Stefan Berger
e63fb9acf1 swtpm_setup: Always log the active profile
Extend the list of SWTPM_INFO flags with recently added flags for
TPMLIB_GetInfo. Use the CMD_GET_INFO control channel command to get
the currently active profile for a TPM 2 from swtpm and display it in
the log unless it is reconfigured.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-20 10:18:21 -04:00
Stefan Berger
9edfc18b3e swtpm: Have tpmlib_maybe_configure_openssl return -1 on error
The single caller does not need to be adjusted.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-19 20:59:56 -04:00
Stefan Berger
8f7a57561f swtpm: Have json_get_submap_value return -1 on error
None of its callers need to be adjusted.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-19 20:59:56 -04:00
Stefan Berger
2ede504c75 swtpm: Have get_profiles return -1 on error
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-19 20:59:56 -04:00
Stefan Berger
12b0b4013a swtpm: Have json_get/set_map_key_value return negative error codes
Adjust all callers.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-19 20:59:56 -04:00
Stefan Berger
cbaab161d3 swtpm: Ignore remove-disabled parameter on non-'custom' profile
Ignore the remove-disabled parameter on non-'custom' profile identified
by return value '1'. Switch to negative return values in the called function.

Extend a test case to ensure that the --profile-remove-disabled option
on swtpm_setup, which is passed through to swtpm, has no effect on 'null'
and 'default-v1' profiles.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-19 20:59:56 -04:00
Stefan Berger
bd91c7628d swtpm: Fix a typo in a return code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-19 20:59:56 -04:00
Stefan Berger
6b1112aef2 swtpm: Check for good entropy source in chroot environment
Very old OpenSSL versions (e.g., 1.1.0i) are using /dev/urandom to get
entropy while newer ones are using the getrandom syscall that does not
need the device file. In some environments access to the created
/dev/urandom device file may not work (EACCESS; chroot test case) and
then OpenSSL will start failing operations that depend on good entropy.
Therefore, check the status of the random number generator after chroot.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-19 16:54:28 -04:00
Stefan Berger
017f99cedd swtpm: Implement a check for HMAC+sha1 for testing future restrictions
HMAC+sha1 may be restricted next, so test for it but do not support
forced removal of support for it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-17 11:44:27 -04:00
Stefan Berger
1d2fd75bfd swtpm_setup: Implement --profile-remove-fips-disabled option
Implement the --profile-remove-fips-disabled option that is used to tell
swtpm to remove algorithms that are disabled by FIPS mode on the host.
Internally, this option passes the remove-fips-disabled option parameter
with the --profile option to swtpm.

Add a test cases passing this option and check that the resulting profiles
have key sizes adjusted and relevant attributes set.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-16 13:20:46 -04:00
Stefan Berger
c3de83e7fe swtpm: Implement function to check whether a crypto algorithm is disabled
Implement a function that checks whether a crypto algorithm identified by
TPM algorithm identifiers is disabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-16 13:20:46 -04:00
Stefan Berger
8e497a6a60 swtpm: Implement functions to get and set key values in a JSON map
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-16 13:20:46 -04:00
Stefan Berger
be1f9cd75c swtpm: Implement strv_remove, strv_dedup, and strv_extend
strv_remove: Remove matches from a 2nd array in a 1st array
strv_dedup:  Remove duplicates in an array
strv_extend: Append elements of a 2nd array to a 1st array

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-16 13:20:46 -04:00
Stefan Berger
db6f67b25b swtpm: Prefix public function from check_algos with 'check_'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-16 13:20:46 -04:00
Stefan Berger
a946f381f9 swtpm: Convert check_rsaes to check_rsa_encryption
Convert check_rsaes to check_rsa_encryption that can also be used for
testing of unpadded RSA encryption.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-16 13:20:46 -04:00
Stefan Berger
da9e7636b7 swtpm: Use the padding parameter passed to swtpm_rsa_sign
Do not ignore the padding parameter passed to swtpm_rsasign but use
it as parameter to the OpenSSL function.

Change "rsapss" to "tsassa" in one case where it was wrong.

Also rename swtpm_rsasign to swtpm_rsa_sign.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-16 13:20:46 -04:00
Stefan Berger
46c95ff849 swtpm: Add SWTPM_ATTR_UNUSED to unused function parameters
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-13 16:02:58 -04:00
Stefan Berger
a0e9d4b3fe swtpm: Print cmdarg-print-profiles as part of capabilities
Commit 96fe5afa forgot to add cmdarg-print-profiles to the list of
capabilities. Also fix typo in the man page and sort shown output
to match application output.

Fixes: 96fe5afa ("swtpm: Add support for --print-profiles option")
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-10 08:01:41 -04:00
Stefan Berger
d496df181a swtpm: Check whether SHA1 signature support is disabled in profile
To avoid setting the environment variable OPENSSL_ENABLE_SHA1_SIGNATURES
check whether SHA1 signature support is disabled in the TPM 2 profile.
It is disabled if either 'fips-host' or the pair 'no-sha1-signing' and
'no-sha1-verification' are found among the enabled attributes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-03 14:34:00 -04:00
Stefan Berger
0ddc7ed254 swtpm: Use TPMLIB_WasManufactured to check whether profile was applied
Use TPMLIB_WasManufactured to check whether a profile was applied since a
new instance was created. If a profile was given and no new TPM 2 instance
was created then display an error message and exit with an error code.
This avoids silently ignoring a provided profile that was not applied
since the TPM 2 instance already existed.

Make sure that a profile is only applied once by swtpm by clearing the
json_profile once TPMLIB_MainInit succeeded.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-30 17:29:03 -04:00
Stefan Berger
863476868e swtpm: Display error messages if g_setenv fails
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-21 10:44:27 -04:00
Stefan Berger
8d31d88c69 swtpm: Determine whether OpenSSL needs to be configured (FIPs, SHA1 signature)
Get the list of enabled 'RuntimeAlgorithms' from libtpms and determine
whether any of these enabled algorithms is disabled in OpenSSL due to FIPS
mode. If FIPS mode on the host is enabled then disabled FIPS mode in
OpenSSL so that the TPM 2 can function properly.

The following algorithms are disabled when OpenSSL is in FIPS mode:
- camellia
- rsaes
- tdes
- ECC keys <224 bits
- RSA keys <2048 bits

Per openssl-ciphers man page it should be possible to disable the following
algorithms use by cipher-suites:

- AES128, AES256, AES
- CAMELLIA128, CAMELLIA256, CAMELLIA
- 3DES
- SHA1, SHA, SHA256, SHA384
- CBC

Note: It's not clear at what API level these are disabled. I have not been able
to use !SHA256 to disabled SHA256.

Also test whether signatures with SHA1 are working and enable them by setting
OPENSSL_ENABLE_SHA1_SIGNATURES=1.

The following output is expected for RHEL 9.x and CentOS 9 but does not
appear on Fedora 40 since there sha1 signatures are not (yet) disabled.

> swtpm socket \
    --tpmstate dir=/tmp/myvtpm \
    --ctrl type=tcp,port=2322 \
    --server type=tcp,port=2321,disconnect \
    --flags not-need-init,startup-clear \
    --log level=0 \
    --tpm2 \
    --profile name=null
Warning: Profile-enabled algorithms contain disabled 'RSA-1024-sign(SHA1, pkcs1-pss)'
Warning: Setting OPENSSL_ENABLE_SHA1_SIGNATURES=1

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-08-20 20:53:53 -04:00
Stefan Berger
6a01a97e1d swtpm: Check whether bufferSize parameter is too small (Coverity)
Check whether the bufferSize parameter is too small and an underflow of the
expression bufferSize - offset could theoretically occur. However, in
practice this will never happen since the caller will always provide a
bufferSize of around 4kb.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-23 09:19:52 -04:00
Stefan Berger
88a89f4970 swtpm: Join parameters for string formatting
Since 'comma1' will always be true combine it with cmdarg_profile into one
string formatting parameter following the same formatting as further above.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-23 09:19:52 -04:00
Stefan Berger
415700e8f7 swtpm: Check for error code returned by sysconf call
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-23 09:19:52 -04:00
Stefan Berger
25eb5f75ea swtpm_setup: Read default profile from swtpm_setup.conf
If the user did not provide the profile on the command line read the
default profile from the swtpm_setup.conf configuration file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
96fe5afac7 swtpm: Add support for --print-profiles option
Add support for --print-profiles option to print all profiles supported
by libtpms.

Usage:

  swtpm socket --tpm2 --print-profiles | jq

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
8bfa8399b3 swtpm_setup: Print profile names as part of capabilities JSON
Print a list of names of profiles implemented by libtpms as part of the
capabilities JSON. The profiles map will only be visible if libtpms v0.10
with the TPMLIB_SetProfile() API is used.

swtpm_setup --print-capabilities --tpm2 | jq
{
  "type": "swtpm_setup",
  "features": [
    [...]
  ],
  "profiles": [
    "default-v1",
    "null",
    "custom"
  ],
  "version": "0.10.0"
}

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
df11aeb6b9 swtpm_setup: Add support for --profile parameter
Add support for the --profile parameter that allows a user to select
a profile for the TPM 2 instance. The profile parameter must be a
string-formatted JSON map describing the profile to use.

Resolves: https://github.com/stefanberger/libtpms/issues/284
Resolves: https://github.com/stefanberger/swtpm/issues/710
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
3a49ce1302 swtpm: Print profile names as part of capabilities JSON
Print a list of names of profiles implemented by libtpms as part of the
capabilities JSON. The profiles map will only be visible if libtpms v0.10
with the TPMLIB_SetProfile() API is used.

swtpm socket --print-capabilities  --tpm2| jq
{
  "type": "swtpm",
  "features": [
    [...]
  ],
  "profiles": {
    "names": [
      "default-v1",
      "null",
      "custom"
    ],
    ...
  },
  "version": "0.10.0"
}

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
fb9ef19ac8 swtpm: Display new capability to allow setting a profile
Display the new capability verb 'cmdarg-profile' indicating that the
--profile option with the name= and profile= parameters is supported.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
0184b9113d swtpm: Add support for --profile option to set a profile on TPM 2
Add support for the --profile option for a TPM 2. The 'name=' parameter
allows a user to select a specific profile available in libtpms. The
'profile=' parameter allows a user to pass a JSON profile that must
contain a name field with a profile known to libtpms. It may contain
an algorithm field that has a comma-separated list of verbs with the
names of algorithms that the TPM 2 is supposed to provide.

The --profile option only has an effect the first time a TPM 2 is started
since afterwards whenever the state of the TPM 2 is read, the profile
found in the state is being used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
02ca22e7f6 swtpm: Support parsing of JSON maps as option values
Allow passing a JSON map as part of an option value in the format of
--foo name={...},... Prior to this patch this would not have worked since
the option values were broken apart around commas, which a map may also
contain. Now, if a '{' is following the '=', the value is attempted to be
parsed as a JSON map and the end of the map is searched considering
possibly embedded maps.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-22 09:46:58 -04:00
Stefan Berger
cc9ee0fbc6 swtpm_setup: Get default rsa keysize from setup_setup.conf if not given
If the user did not provide the RSA keysize to use try to read it from
setup_setup.conf and if nothing is found there fall back to using the
internal default RSA keysize (2048).

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-17 18:34:25 -04:00
Stefan Berger
5dfc42c622 swtpm_setup: Factor-out read_file_lines from get_default_pcr_banks
Factor-out read_file_lines from get_default_pcr_banks and pass the array
of lines from the config file into get_default_pcr_banks now. Now other
functions will also be able to access the lines from the config file
without having to re-read the config file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-07-17 18:34:25 -04:00
Marc-André Lureau
aa5c7191ac selinux: add missing rules for libvirt system
Reported & tested:
https://issues.redhat.com/browse/RHEL-47273

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2024-07-15 09:47:38 -04:00
Stefan Berger
303d9cfdb5 swtpm: Use umask() to create/truncated state file rather than fchmod()
Since swtpm does not use concurrency while writing state files, set
the process umask to 0 when the state file is created or truncated for
writing and the user requested specific mode bits. This avoids an
additional syscall in the possibly timing critical path when processing
a TPM command and having to write the state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-13 13:53:36 -04:00
Stefan Berger
c2e524dbc7 swtpm: Use fchmod to set mode bits provided by user
The mode bits that the user provided were only applied with open() and were
subject to masking with the value of current umask. When umask was set to
0027 the test case test_commandline was failing because the mode bits on
the create TPM state file were not the expected ones (masked by umask).
Therefore, set the mode bits using fchmod if the user provided them,
otherwise do not set them. This way the mode bits will be set to the values
the user requested.

Currently the directory storage backend was setting the mode bits to the
default value (0640) *after* opening the TPM state file. Now, if the user
did not provide any mode bits then the mode bits will be set so that the
file can be written to as owner. This ensures that at least mode bits 0600
are set by default. However, if the user provided mode bit flags then these
will be used without modification.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-12 14:23:54 -04:00
Stefan Berger
85a3a14a41 swtpm: Replace mkstemp with g_mkstemp_full (Coverity)
Replace mkstemp with g_mkstemp_full and pass parameters that lead to
the same mode bits and file opening flags and mkstemp had. This addresses
a Coverity complaint regarding missing application of umask before
mkstemp.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-11 12:02:28 -04:00