Blame SOURCES/0001-Refactor-fix_esys_hierarchies.patch

fbce52
From 2e7839b905f7a493f739d36e3e550e0cee30049e Mon Sep 17 00:00:00 2001
fbce52
From: Andreas Fuchs <andreas.fuchs@sit.fraunhofer.de>
fbce52
Date: Thu, 7 May 2020 19:12:36 +0200
fbce52
Subject: [PATCH] Refactor fix_esys_hierarchies()
fbce52
fbce52
Refactor fix_esys_hierarchies() to return an actual TSS2_RC return code
fbce52
and have an output parameter.
fbce52
fbce52
Signed-off-by: Andreas Fuchs <andreas.fuchs@sit.fraunhofer.de>
fbce52
---
fbce52
 lib/tpm2.c                | 88 +++++++++++++++++++++++++++++----------
fbce52
 lib/tpm2.h                |  2 +-
fbce52
 tools/tpm2_loadexternal.c |  9 +++-
fbce52
 3 files changed, 75 insertions(+), 24 deletions(-)
fbce52
fbce52
diff --git a/lib/tpm2.c b/lib/tpm2.c
fbce52
index 909a4422339d..744fed8c529f 100644
fbce52
--- a/lib/tpm2.c
fbce52
+++ b/lib/tpm2.c
fbce52
@@ -645,39 +645,51 @@ tool_rc tpm2_evictcontrol(ESYS_CONTEXT *esys_context,
fbce52
 }
fbce52
 
fbce52
 /* This function addresses ESAPI change that changes parameter type from
fbce52
- * Esys_TR to TPMI_RH_HIERARCHY and breaks backwards compatibility.
fbce52
+ * Esys_TR to TPMI_RH_HIERARCHY or TPMI_RH_ENABLES and breaks backwards
fbce52
+ * compatibility.
fbce52
  * To keep the tools parameters consistent after v4.0 release we need to
fbce52
  * map the values to appropriate type based on the version of the ESYS API.
fbce52
  * Note: the mapping is based on the ESYS version recognized at compile time.
fbce52
  * The TSS change can be found here:
fbce52
  * https://github.com/tpm2-software/tpm2-tss/pull/1531
fbce52
  */
fbce52
-uint32_t fix_esys_hierarchy(TPMI_RH_HIERARCHY hierarchy)
fbce52
+TSS2_RC fix_esys_hierarchy(uint32_t in, uint32_t *out)
fbce52
 {
fbce52
 #if defined(ESYS_3_0)
fbce52
-    switch (hierarchy) {
fbce52
+    switch (in) {
fbce52
         case ESYS_TR_RH_NULL:
fbce52
+            /* FALLTHRU */
fbce52
         case ESYS_TR_RH_OWNER:
fbce52
+            /* FALLTHRU */
fbce52
         case ESYS_TR_RH_ENDORSEMENT:
fbce52
+            /* FALLTHRU */
fbce52
         case ESYS_TR_RH_PLATFORM:
fbce52
+            /* FALLTHRU */
fbce52
         case ESYS_TR_RH_PLATFORM_NV:
fbce52
-            return hierarchy;
fbce52
+            *out = in;
fbce52
+            return TSS2_RC_SUCCESS;
fbce52
         case TPM2_RH_NULL:
fbce52
-            return ESYS_TR_RH_NULL;
fbce52
+            *out = ESYS_TR_RH_NULL;
fbce52
+            return TSS2_RC_SUCCESS;
fbce52
         case TPM2_RH_OWNER:
fbce52
-            return ESYS_TR_RH_OWNER;
fbce52
+            *out = ESYS_TR_RH_OWNER;
fbce52
+            return TSS2_RC_SUCCESS;
fbce52
         case TPM2_RH_ENDORSEMENT:
fbce52
-            return ESYS_TR_RH_ENDORSEMENT;
fbce52
+            *out = ESYS_TR_RH_ENDORSEMENT;
fbce52
+            return TSS2_RC_SUCCESS;
fbce52
         case TPM2_RH_PLATFORM:
fbce52
-            return ESYS_TR_RH_PLATFORM;
fbce52
+            *out = ESYS_TR_RH_PLATFORM;
fbce52
+            return TSS2_RC_SUCCESS;
fbce52
         case TPM2_RH_PLATFORM_NV:
fbce52
-            return ESYS_TR_RH_PLATFORM_NV;
fbce52
+            *out = ESYS_TR_RH_PLATFORM_NV;
fbce52
+            return TSS2_RC_SUCCESS;
fbce52
         default:
fbce52
-            LOG_ERR("An unknown hierarchy handle was passed: 0x%08x", hierarchy);
fbce52
-            return 0xffffffff;
fbce52
+            LOG_ERR("An unknown hierarchy handle was passed: 0x%08x", in);
fbce52
+            return TSS2_ESYS_RC_BAD_VALUE;
fbce52
     }
fbce52
 #elif defined(ESYS_2_3)
fbce52
-    return hierarchy;
fbce52
+    *out = in;
fbce52
+    return TSS2_RC_SUCCESS;
fbce52
 #else
fbce52
 #error "Need to define either ESYS_3_0 or ESYS_2_3"
fbce52
 #endif
fbce52
@@ -688,8 +700,14 @@ tool_rc tpm2_hash(ESYS_CONTEXT *esys_context, ESYS_TR shandle1, ESYS_TR shandle2
fbce52
         TPMI_RH_HIERARCHY hierarchy, TPM2B_DIGEST **out_hash,
fbce52
         TPMT_TK_HASHCHECK **validation) {
fbce52
 
fbce52
-    TSS2_RC rval = Esys_Hash(esys_context, shandle1, shandle2, shandle3, data,
fbce52
-            hash_alg, fix_esys_hierarchy(hierarchy), out_hash, validation);
fbce52
+    TSS2_RC rval = fix_esys_hierarchy(hierarchy, &hierarchy);
fbce52
+    if (rval != TSS2_RC_SUCCESS) {
fbce52
+        LOG_ERR("Unknown hierarchy");
fbce52
+        return tool_rc_from_tpm(rval);
fbce52
+    }
fbce52
+
fbce52
+    rval = Esys_Hash(esys_context, shandle1, shandle2, shandle3, data,
fbce52
+            hash_alg, hierarchy, out_hash, validation);
fbce52
     if (rval != TSS2_RC_SUCCESS) {
fbce52
         LOG_PERR(Esys_Hash, rval);
fbce52
         return tool_rc_from_tpm(rval);
fbce52
@@ -729,9 +747,15 @@ tool_rc tpm2_sequence_complete(ESYS_CONTEXT *esys_context,
fbce52
         TPMI_RH_HIERARCHY hierarchy, TPM2B_DIGEST **result,
fbce52
         TPMT_TK_HASHCHECK **validation) {
fbce52
 
fbce52
-    TSS2_RC rval = Esys_SequenceComplete(esys_context, sequence_handle,
fbce52
+    TSS2_RC rval = fix_esys_hierarchy(hierarchy, &hierarchy);
fbce52
+    if (rval != TSS2_RC_SUCCESS) {
fbce52
+        LOG_ERR("Unknown hierarchy");
fbce52
+        return tool_rc_from_tpm(rval);
fbce52
+    }
fbce52
+
fbce52
+    rval = Esys_SequenceComplete(esys_context, sequence_handle,
fbce52
             ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE, buffer,
fbce52
-            fix_esys_hierarchy(hierarchy), result, validation);
fbce52
+            hierarchy, result, validation);
fbce52
     if (rval != TSS2_RC_SUCCESS) {
fbce52
         LOG_PERR(Esys_SequenceComplete, rval);
fbce52
         return tool_rc_from_tpm(rval);
fbce52
@@ -1161,8 +1185,14 @@ tool_rc tpm2_hierarchycontrol(ESYS_CONTEXT *esys_context,
fbce52
         return rc;
fbce52
     }
fbce52
 
fbce52
-    TSS2_RC rval = Esys_HierarchyControl(esys_context, auth_hierarchy->tr_handle,
fbce52
-            shandle, ESYS_TR_NONE, ESYS_TR_NONE, fix_esys_hierarchy(enable), state);
fbce52
+    TSS2_RC rval = fix_esys_hierarchy(enable, &enable);
fbce52
+    if (rval != TSS2_RC_SUCCESS) {
fbce52
+        LOG_ERR("Unknown hierarchy");
fbce52
+        return tool_rc_from_tpm(rval);
fbce52
+    }
fbce52
+
fbce52
+    rval = Esys_HierarchyControl(esys_context, auth_hierarchy->tr_handle,
fbce52
+            shandle, ESYS_TR_NONE, ESYS_TR_NONE, enable, state);
fbce52
     if (rval != TPM2_RC_SUCCESS && rval != TPM2_RC_INITIALIZE) {
fbce52
         LOG_PERR(Esys_HierarchyControl, rval);
fbce52
         return tool_rc_from_tpm(rval);
fbce52
@@ -1257,9 +1287,17 @@ tool_rc tpm2_hmac_sequencecomplete(ESYS_CONTEXT *esys_context,
fbce52
         return rc;
fbce52
     }
fbce52
 
fbce52
-    TPM2_RC rval = Esys_SequenceComplete(esys_context, sequence_handle,
fbce52
+    uint32_t hierarchy;
fbce52
+
fbce52
+    TSS2_RC rval = fix_esys_hierarchy(TPM2_RH_NULL, &hierarchy);
fbce52
+    if (rval != TSS2_RC_SUCCESS) {
fbce52
+        LOG_ERR("Unknown hierarchy");
fbce52
+        return tool_rc_from_tpm(rval);
fbce52
+    }
fbce52
+
fbce52
+    rval = Esys_SequenceComplete(esys_context, sequence_handle,
fbce52
             hmac_key_obj_shandle, ESYS_TR_NONE, ESYS_TR_NONE, input_buffer,
fbce52
-            fix_esys_hierarchy(TPM2_RH_NULL), result, validation);
fbce52
+            hierarchy, result, validation);
fbce52
     if (rval != TSS2_RC_SUCCESS) {
fbce52
         LOG_PERR(Esys_HMAC, rval);
fbce52
         return tool_rc_from_tpm(rval);
fbce52
@@ -1913,9 +1951,15 @@ tool_rc tpm2_loadexternal(ESYS_CONTEXT *ectx, const TPM2B_SENSITIVE *private,
fbce52
         const TPM2B_PUBLIC *public, TPMI_RH_HIERARCHY hierarchy,
fbce52
         ESYS_TR *object_handle) {
fbce52
 
fbce52
-    TSS2_RC rval = Esys_LoadExternal(ectx,
fbce52
+    TSS2_RC rval = fix_esys_hierarchy(hierarchy, &hierarchy);
fbce52
+    if (rval != TSS2_RC_SUCCESS) {
fbce52
+        LOG_ERR("Unknown hierarchy");
fbce52
+        return tool_rc_from_tpm(rval);
fbce52
+    }
fbce52
+
fbce52
+    rval = Esys_LoadExternal(ectx,
fbce52
             ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
fbce52
-            private, public, fix_esys_hierarchy(hierarchy),
fbce52
+            private, public, hierarchy,
fbce52
             object_handle);
fbce52
     if (rval != TSS2_RC_SUCCESS) {
fbce52
         LOG_PERR(Esys_LoadExternal, rval);
fbce52
diff --git a/lib/tpm2.h b/lib/tpm2.h
fbce52
index a37e05606c7a..2e4ae5e7dddc 100644
fbce52
--- a/lib/tpm2.h
fbce52
+++ b/lib/tpm2.h
fbce52
@@ -389,7 +389,7 @@ tool_rc tpm2_policy_nv_written(ESYS_CONTEXT *esys_context,
fbce52
         ESYS_TR policy_session, ESYS_TR shandle1, ESYS_TR shandle2,
fbce52
         ESYS_TR shandle3, TPMI_YES_NO written_set);
fbce52
 
fbce52
-uint32_t fix_esys_hierarchy(TPMI_RH_HIERARCHY hierarchy);
fbce52
+TSS2_RC fix_esys_hierarchy(uint32_t in, uint32_t *out);
fbce52
 
fbce52
 tool_rc tpm2_certifycreation(ESYS_CONTEXT *esys_context,
fbce52
     tpm2_loaded_object *signingkey_obj, tpm2_loaded_object *certifiedkey_obj,
fbce52
diff --git a/tools/tpm2_loadexternal.c b/tools/tpm2_loadexternal.c
fbce52
index 70fb72877aae..4127ca1b524b 100644
fbce52
--- a/tools/tpm2_loadexternal.c
fbce52
+++ b/tools/tpm2_loadexternal.c
fbce52
@@ -48,9 +48,16 @@ static tpm_loadexternal_ctx ctx = {
fbce52
 static tool_rc load_external(ESYS_CONTEXT *ectx, TPM2B_PUBLIC *pub,
fbce52
         TPM2B_SENSITIVE *priv, bool has_priv, TPM2B_NAME **name) {
fbce52
 
fbce52
+    uint32_t hierarchy;
fbce52
+    TSS2_RC rval = fix_esys_hierarchy(ctx.hierarchy_value, &hierarchy);
fbce52
+    if (rval != TSS2_RC_SUCCESS) {
fbce52
+        LOG_ERR("Unknown hierarchy");
fbce52
+        return tool_rc_from_tpm(rval);
fbce52
+    }
fbce52
+
fbce52
     tool_rc rc = tpm2_loadexternal(ectx,
fbce52
             has_priv ? priv : NULL, pub,
fbce52
-            fix_esys_hierarchy(ctx.hierarchy_value), &ctx.handle);
fbce52
+            hierarchy, &ctx.handle);
fbce52
     if (rc != tool_rc_success) {
fbce52
         return rc;
fbce52
     }
fbce52
-- 
fbce52
2.27.0
fbce52