]> git.proxmox.com Git - swtpm.git/commitdiff
swtpm: Implement function reporting error when choosing unsupported TPM
authorStefan Berger <stefanb@linux.ibm.com>
Fri, 19 Nov 2021 22:35:33 +0000 (17:35 -0500)
committerStefan Berger <stefanb@us.ibm.com>
Mon, 22 Nov 2021 12:39:28 +0000 (07:39 -0500)
Implement tpmlib_choose_tpm_version() that reports an error when an un-
supported version is chosen. Have it used by existing code where possible.

If TPM 1.2 is not supported by libtpms, the following message is now
displayed:

swtpm: Error: TPM 1.2 is not supported by libtpms.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2024583
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
src/swtpm/cuse_tpm.c
src/swtpm/swtpm.c
src/swtpm/swtpm_chardev.c
src/swtpm/tpmlib.c
src/swtpm/tpmlib.h

index d577d88e1e3e00e5c7322bfe14c7ec3326a2d766..9dbc00df28255172db3ebb476cc9261dd9852557 100644 (file)
@@ -1621,9 +1621,7 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
         goto exit;
     }
 
-    if (TPMLIB_ChooseTPMVersion(tpmversion) != TPM_SUCCESS) {
-        logprintf(STDERR_FILENO,
-                  "Error: Could not choose TPM version.\n");
+    if (tpmlib_choose_tpm_version(tpmversion) != TPM_SUCCESS) {
         ret = EXIT_FAILURE;
         goto exit;
     }
index d4ad6829e46181f4847cce3e367d99f7aa309455..722a7432f2513631f60ded7dadb692212a9c932f 100644 (file)
@@ -437,11 +437,8 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
         exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
     }
 
-    if (TPMLIB_ChooseTPMVersion(mlp.tpmversion) != TPM_SUCCESS) {
-        logprintf(STDERR_FILENO,
-                  "Error: Could not choose TPM version.\n");
+    if (tpmlib_choose_tpm_version(mlp.tpmversion) != TPM_SUCCESS)
         exit(EXIT_FAILURE);
-    }
 
     if (handle_ctrlchannel_options(ctrlchdata, &mlp.cc) < 0 ||
         handle_server_options(serverdata, &server) < 0) {
index 165d568aa9afc7a4451738dc2c45b6ffbf6d61a5..9710927ec6268216824568fa2fd2d9034f89267e 100644 (file)
@@ -491,11 +491,8 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
         exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
     }
 
-    if (TPMLIB_ChooseTPMVersion(mlp.tpmversion) != TPM_SUCCESS) {
-        logprintf(STDERR_FILENO,
-                  "Error: Could not choose TPM version.\n");
+    if (tpmlib_choose_tpm_version(mlp.tpmversion) != TPM_SUCCESS)
         exit(EXIT_FAILURE);
-    }
 
     tpmstate_set_version(mlp.tpmversion);
 
index 657f7f51e81e54cc028db690d10ed899d5435424..bebd9e9d0f0820f601795afdee76ff9b5f32a19c 100644 (file)
@@ -89,15 +89,27 @@ TPM_RESULT tpmlib_register_callbacks(struct libtpms_callbacks *cbs)
     return res;
 }
 
-TPM_RESULT tpmlib_start(uint32_t flags, TPMLIB_TPMVersion tpmversion)
+TPM_RESULT tpmlib_choose_tpm_version(TPMLIB_TPMVersion tpmversion)
 {
     TPM_RESULT res;
 
     if ((res = TPMLIB_ChooseTPMVersion(tpmversion)) != TPM_SUCCESS) {
+        const char *version = "TPM 1.2";
+
+        if (tpmversion == TPMLIB_TPM_VERSION_2)
+            version = "TPM 2";
         logprintf(STDERR_FILENO,
-                  "Error: Could not choose TPM 2 implementation.\n");
-        return res;
+                  "Error: %s is not supported by libtpms.\n", version);
     }
+    return res;
+}
+
+TPM_RESULT tpmlib_start(uint32_t flags, TPMLIB_TPMVersion tpmversion)
+{
+    TPM_RESULT res;
+
+    if ((res = tpmlib_choose_tpm_version(tpmversion)) != TPM_SUCCESS)
+        return res;
 
     if ((res = TPMLIB_MainInit()) != TPM_SUCCESS) {
         logprintf(STDERR_FILENO,
index 3086db124966d66dde0ea637a57097368b74237b..16ab717af250a751fb3cf5572762171f87a38908 100644 (file)
@@ -46,6 +46,7 @@
 const char *tpmlib_get_blobname(uint32_t blobtype);
 enum TPMLIB_StateType tpmlib_blobtype_to_statetype(uint32_t blobtype);
 TPM_RESULT tpmlib_register_callbacks(struct libtpms_callbacks *cbs);
+TPM_RESULT tpmlib_choose_tpm_version(TPMLIB_TPMVersion tpmversion);
 TPM_RESULT tpmlib_start(uint32_t flags, TPMLIB_TPMVersion tpmversion);
 int tpmlib_get_tpm_property(enum TPMLIB_TPMProperty prop);
 bool tpmlib_is_request_cancelable(TPMLIB_TPMVersion tpmversion,