Blame SOURCES/0007-FAPI-Fix-loading-of-primary-keys.patch

6a14f3
From 517e94ee72b286e9942a5a6ecbffd05fc0b0bcf5 Mon Sep 17 00:00:00 2001
6a14f3
From: Juergen Repp <juergen.repp@sit.fraunhofer.de>
6a14f3
Date: Fri, 5 Nov 2021 23:08:47 +0100
6a14f3
Subject: [PATCH 07/23] FAPI: Fix loading of primary keys.
6a14f3
6a14f3
Problems caused by primary keys created with Fapi_CreateKey are fixed:
6a14f3
6a14f3
* For primary keys not in all cases the unique field was cleared before calling create
6a14f3
  primary.
6a14f3
* If the primary key was used for signing the object was cleared after loading. So
6a14f3
  access e.g. to the certificate did not work.
6a14f3
* For primary keys created with Fapi_Create with an auth value the auth_value was
6a14f3
  not used in inSensitive to recreate the primary key. Now the auth value callback
6a14f3
  is used to initialize inSensitive.
6a14f3
6a14f3
Fixes #2189.
6a14f3
6a14f3
Signed-off-by: Juergen Repp <juergen.repp@sit.fraunhofer.de>
6a14f3
---
6a14f3
 src/tss2-fapi/fapi_int.h  |  1 +
6a14f3
 src/tss2-fapi/fapi_util.c | 62 +++++++++++++++++++++++++++++++++++++--
6a14f3
 2 files changed, 60 insertions(+), 3 deletions(-)
6a14f3
6a14f3
diff --git a/src/tss2-fapi/fapi_int.h b/src/tss2-fapi/fapi_int.h
6a14f3
index d13ec413..7bcf442c 100644
6a14f3
--- a/src/tss2-fapi/fapi_int.h
6a14f3
+++ b/src/tss2-fapi/fapi_int.h
6a14f3
@@ -768,6 +768,7 @@ enum _FAPI_STATE_PRIMARY {
6a14f3
     PRIMARY_READ_HIERARCHY,
6a14f3
     PRIMARY_READ_HIERARCHY_FINISH,
6a14f3
     PRIMARY_AUTHORIZE_HIERARCHY,
6a14f3
+    PRIMARY_GET_AUTH_VALUE,
6a14f3
     PRIMARY_WAIT_FOR_PRIMARY,
6a14f3
     PRIMARY_HAUTH_SENT,
6a14f3
     PRIMARY_CREATED,
6a14f3
diff --git a/src/tss2-fapi/fapi_util.c b/src/tss2-fapi/fapi_util.c
6a14f3
index a0fd714e..90f8b2aa 100644
6a14f3
--- a/src/tss2-fapi/fapi_util.c
6a14f3
+++ b/src/tss2-fapi/fapi_util.c
6a14f3
@@ -362,6 +362,52 @@ ifapi_get_object_path(IFAPI_OBJECT *object)
6a14f3
     return NULL;
6a14f3
 }
6a14f3
 
6a14f3
+/** Set authorization value for a primary key to be created.
6a14f3
+ *
6a14f3
+ * The callback which provides the auth value must be defined.
6a14f3
+ *
6a14f3
+ * @param[in,out] context The FAPI_CONTEXT.
6a14f3
+ * @param[in]     object The auth value will be assigned to this object.
6a14f3
+ * @param[in,out] inSensitive The sensitive data to store the auth value.
6a14f3
+ *
6a14f3
+ * @retval TSS2_RC_SUCCESS on success.
6a14f3
+ * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN If the callback for getting
6a14f3
+ *         the auth value is not defined.
6a14f3
+ */
6a14f3
+TSS2_RC
6a14f3
+ifapi_set_auth_primary(
6a14f3
+    FAPI_CONTEXT *context,
6a14f3
+    IFAPI_OBJECT *object,
6a14f3
+    TPMS_SENSITIVE_CREATE *inSensitive)
6a14f3
+{
6a14f3
+    TSS2_RC r;
6a14f3
+    const char *auth = NULL;
6a14f3
+    const char *obj_path;
6a14f3
+
6a14f3
+    memset(inSensitive, 0, sizeof(TPMS_SENSITIVE_CREATE));
6a14f3
+
6a14f3
+    if (!object->misc.key.with_auth) {
6a14f3
+        return TSS2_RC_SUCCESS;
6a14f3
+    }
6a14f3
+
6a14f3
+    obj_path = ifapi_get_object_path(object);
6a14f3
+
6a14f3
+    /* Check whether callback is defined. */
6a14f3
+    if (context->callbacks.auth) {
6a14f3
+        r = context->callbacks.auth(obj_path, object->misc.key.description,
6a14f3
+                                    &auth, context->callbacks.authData);
6a14f3
+        return_if_error(r, "AuthCallback");
6a14f3
+        if (auth != NULL) {
6a14f3
+            inSensitive->userAuth.size = strlen(auth);
6a14f3
+            memcpy(&inSensitive->userAuth.buffer[0], auth,
6a14f3
+                   inSensitive->userAuth.size);
6a14f3
+        }
6a14f3
+        return TSS2_RC_SUCCESS;
6a14f3
+    }
6a14f3
+    SAFE_FREE(auth);
6a14f3
+    return_error( TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN, "Authorization callback not defined.");
6a14f3
+}
6a14f3
+
6a14f3
 /** Set authorization value for a FAPI object.
6a14f3
  *
6a14f3
  * The callback which provides the auth value must be defined.
6a14f3
@@ -848,7 +894,7 @@ ifapi_load_primary_finish(FAPI_CONTEXT *context, ESYS_TR *handle)
6a14f3
     IFAPI_KEY *pkey = &context->createPrimary.pkey_object.misc.key;
6a14f3
     TPMS_CAPABILITY_DATA **capabilityData = &context->createPrimary.capabilityData;
6a14f3
     TPMI_YES_NO moreData;
6a14f3
-    ESYS_TR auth_session;
6a14f3
+    ESYS_TR auth_session = ESYS_TR_NONE; /* Initialized due to scanbuild */
6a14f3
 
6a14f3
     LOG_TRACE("call");
6a14f3
 
6a14f3
@@ -923,12 +969,23 @@ ifapi_load_primary_finish(FAPI_CONTEXT *context, ESYS_TR *handle)
6a14f3
         memset(&context->createPrimary.inSensitive, 0, sizeof(TPM2B_SENSITIVE_CREATE));
6a14f3
         memset(&context->createPrimary.outsideInfo, 0, sizeof(TPM2B_DATA));
6a14f3
         memset(&context->createPrimary.creationPCR, 0, sizeof(TPML_PCR_SELECTION));
6a14f3
+        fallthrough;
6a14f3
+
6a14f3
+    statecase(context->primary_state, PRIMARY_GET_AUTH_VALUE);
6a14f3
+        /* Get the auth value to be stored in inSensitive */
6a14f3
+        r = ifapi_set_auth_primary(context, pkey_object,
6a14f3
+                                   &context->createPrimary.inSensitive.sensitive);
6a14f3
+        return_try_again(r);
6a14f3
+        goto_if_error_reset_state(r, "Get auth value for primary", error_cleanup);
6a14f3
 
6a14f3
         /* Prepare primary creation. */
6a14f3
+        TPM2B_PUBLIC public = pkey->public;
6a14f3
+        memset(&public.publicArea.unique, 0, sizeof(TPMU_PUBLIC_ID));
6a14f3
+
6a14f3
         r = Esys_CreatePrimary_Async(context->esys, hierarchy->handle,
6a14f3
                                      auth_session, ESYS_TR_NONE, ESYS_TR_NONE,
6a14f3
                                      &context->createPrimary.inSensitive,
6a14f3
-                                     &pkey->public,
6a14f3
+                                     &public,
6a14f3
                                      &context->createPrimary.outsideInfo,
6a14f3
                                      &context->createPrimary.creationPCR);
6a14f3
         return_if_error(r, "CreatePrimary");
6a14f3
@@ -1905,7 +1962,6 @@ ifapi_load_key_finish(FAPI_CONTEXT *context, bool flush_parent)
6a14f3
         } else {
6a14f3
             LOG_TRACE("success");
6a14f3
             ifapi_cleanup_ifapi_object(context->loadKey.key_object);
6a14f3
-            ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
6a14f3
             return TSS2_RC_SUCCESS;
6a14f3
         }
6a14f3
         break;
6a14f3
-- 
6a14f3
2.34.3
6a14f3