]> git.proxmox.com Git - swtpm.git/commitdiff
swtpm: Move TPMLIB_ChooseTPMVersion into capabilities_print_json
authorStefan Berger <stefanb@linux.ibm.com>
Mon, 18 Jul 2022 22:24:33 +0000 (18:24 -0400)
committerStefan Berger <stefanb@us.ibm.com>
Tue, 19 Jul 2022 23:17:47 +0000 (19:17 -0400)
All callers to capabilities_print_json() call TPMLIB_ChooseTPMVersion
right before. Move it into the function now and check the return
code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
src/swtpm/capabilities.c
src/swtpm/capabilities.h
src/swtpm/cuse_tpm.c
src/swtpm/swtpm.c
src/swtpm/swtpm_chardev.c

index 685630a0a456d1501b603714e35cb0f38a381088..ae9c92c378b70e7f820b226f8ddb75f167141009 100644 (file)
@@ -114,7 +114,7 @@ oom:
     goto cleanup;
 }
 
-int capabilities_print_json(bool cusetpm)
+int capabilities_print_json(bool cusetpm, TPMLIB_TPMVersion tpmversion)
 {
     char *string = NULL;
     int ret = -1;
@@ -130,6 +130,11 @@ int capabilities_print_json(bool cusetpm)
     const char *nvram_backend_dir = "\"nvram-backend-dir\", ";
     const char *nvram_backend_file = "\"nvram-backend-file\"";
 
+    if (TPMLIB_ChooseTPMVersion(tpmversion) != TPM_SUCCESS) {
+        logprintf(STDERR_FILENO, "Could not choose TPM version.\n");
+        goto cleanup;
+    }
+
     ret = get_rsa_keysize_caps(&keysizecaps);
     if (ret < 0)
         goto cleanup;
index 56ff562ab5d3dd0cf6e2fb18394956947e2619b5..46dbebc74d8295a09b1f9e66e6ac26fe433edfd2 100644 (file)
@@ -40,6 +40,8 @@
 
 #include <stdbool.h>
 
-int capabilities_print_json(bool cusetpm);
+#include <libtpms/tpm_library.h>
+
+int capabilities_print_json(bool cusetpm, TPMLIB_TPMVersion tpmversion);
 
 #endif /* SWTPM_CAPABILITIES_H */
index 3026e26fffb300cb066875157cec03df2dc3f5de..797e0a677e3dceb31eb8c617b528ea1379e3b2b2 100644 (file)
@@ -1616,8 +1616,7 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
          * Choose the TPM version so that getting/setting buffer size works.
          * Ignore failure, for backward compatibility when TPM 1.2 is disabled.
          */
-        TPMLIB_ChooseTPMVersion(tpmversion);
-        ret = capabilities_print_json(true) ? EXIT_FAILURE : EXIT_SUCCESS;
+        ret = capabilities_print_json(true, tpmversion) ? EXIT_FAILURE : EXIT_SUCCESS;
         goto exit;
     }
 
index e618c567b1d2428e58ff75be30c8bc4a23cc7d90..0714d9de9907e355dc94f2662309329fd8fa89a3 100644 (file)
@@ -432,8 +432,7 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
          * Choose the TPM version so that getting/setting buffer size works.
          * Ignore failure, for backward compatibility when TPM 1.2 is disabled.
          */
-        TPMLIB_ChooseTPMVersion(mlp.tpmversion);
-        ret = capabilities_print_json(false);
+        ret = capabilities_print_json(false, mlp.tpmversion);
         exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
     }
 
index ab6d8fd417c6b52849d8c4977b2db78adf128228..be92e7cab0f847a287e65a4f1cd2d6c53c9e49ab 100644 (file)
@@ -486,8 +486,7 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
          * Choose the TPM version so that getting/setting buffer size works.
          * Ignore failure, for backward compatibility when TPM 1.2 is disabled.
          */
-        TPMLIB_ChooseTPMVersion(mlp.tpmversion);
-        ret = capabilities_print_json(false);
+        ret = capabilities_print_json(false, mlp.tpmversion);
         exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
     }