Blame SOURCES/0003-tpm2-Restore-original-value-if-unmarshalled-value-wa.patch

ffd936
From a116d366871e2657243209930cc3bda68f9dd938 Mon Sep 17 00:00:00 2001
ffd936
From: Stefan Berger <stefanb@linux.vnet.ibm.com>
ffd936
Date: Wed, 23 Jun 2021 20:54:44 -0400
ffd936
Subject: [PATCH 3/3] tpm2: Restore original value if unmarshalled value was
ffd936
 illegal
ffd936
ffd936
Restore the original value of the memory location where data from
ffd936
a stream was unmarshalled and the unmarshalled value was found to
ffd936
be illegal. The goal is to not keep illegal values in memory.
ffd936
ffd936
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
ffd936
---
ffd936
 src/tpm2/Unmarshal.c | 122 +++++++++++++++++++++++++++++++++++++++++++
ffd936
 1 file changed, 122 insertions(+)
ffd936
ffd936
diff --git a/src/tpm2/Unmarshal.c b/src/tpm2/Unmarshal.c
ffd936
index bac7d76..c839a68 100644
ffd936
--- a/src/tpm2/Unmarshal.c
ffd936
+++ b/src/tpm2/Unmarshal.c
ffd936
@@ -165,6 +165,7 @@ TPM_RC
ffd936
 TPM_GENERATED_Unmarshal(TPM_GENERATED *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_GENERATED orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT32_Unmarshal(target, buffer, size);  
ffd936
@@ -172,6 +173,7 @@ TPM_GENERATED_Unmarshal(TPM_GENERATED *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (*target != TPM_GENERATED_VALUE) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -197,6 +199,7 @@ TPM_RC
ffd936
 TPM_ECC_CURVE_Unmarshal(TPM_ECC_CURVE *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_ECC_CURVE orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT16_Unmarshal(target, buffer, size);
ffd936
@@ -215,6 +218,7 @@ TPM_ECC_CURVE_Unmarshal(TPM_ECC_CURVE *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_CURVE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -240,6 +244,7 @@ TPM_RC
ffd936
 TPM_CLOCK_ADJUST_Unmarshal(TPM_CLOCK_ADJUST *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_CLOCK_ADJUST orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = INT8_Unmarshal(target, buffer, size);  
ffd936
@@ -256,6 +261,7 @@ TPM_CLOCK_ADJUST_Unmarshal(TPM_CLOCK_ADJUST *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -267,6 +273,7 @@ TPM_RC
ffd936
 TPM_EO_Unmarshal(TPM_EO *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_EO orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT16_Unmarshal(target, buffer, size);  
ffd936
@@ -288,6 +295,7 @@ TPM_EO_Unmarshal(TPM_EO *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -299,6 +307,7 @@ TPM_RC
ffd936
 TPM_ST_Unmarshal(TPM_ST *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_ST orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT16_Unmarshal(target, buffer, size);  
ffd936
@@ -324,6 +333,7 @@ TPM_ST_Unmarshal(TPM_ST *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -335,6 +345,7 @@ TPM_RC
ffd936
 TPM_SU_Unmarshal(TPM_SU *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_SU orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT16_Unmarshal(target, buffer, size);  
ffd936
@@ -346,6 +357,7 @@ TPM_SU_Unmarshal(TPM_SU *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -357,6 +369,7 @@ TPM_RC
ffd936
 TPM_SE_Unmarshal(TPM_SE *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_SE orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT8_Unmarshal(target, buffer, size);  
ffd936
@@ -369,6 +382,7 @@ TPM_SE_Unmarshal(TPM_SE *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -380,6 +394,7 @@ TPM_RC
ffd936
 TPM_CAP_Unmarshal(TPM_CAP *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_CAP orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT32_Unmarshal(target, buffer, size);  
ffd936
@@ -400,6 +415,7 @@ TPM_CAP_Unmarshal(TPM_CAP *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -450,6 +466,7 @@ TPM_RC
ffd936
 TPMA_ALGORITHM_Unmarshal(TPMA_ALGORITHM *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMA_ALGORITHM orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT32_Unmarshal((UINT32 *)target, buffer, size); /* libtpms changed */
ffd936
@@ -457,6 +474,7 @@ TPMA_ALGORITHM_Unmarshal(TPMA_ALGORITHM *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (*target & TPMA_ALGORITHM_reserved) {
ffd936
 	    rc = TPM_RC_RESERVED_BITS;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -468,6 +486,7 @@ TPM_RC
ffd936
 TPMA_OBJECT_Unmarshal(TPMA_OBJECT *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMA_OBJECT orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT32_Unmarshal((UINT32 *)target, buffer, size); /* libtpms changed */
ffd936
@@ -475,6 +494,7 @@ TPMA_OBJECT_Unmarshal(TPMA_OBJECT *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (*target & TPMA_OBJECT_reserved) {
ffd936
 	    rc = TPM_RC_RESERVED_BITS;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -486,6 +506,7 @@ TPM_RC
ffd936
 TPMA_SESSION_Unmarshal(TPMA_SESSION *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMA_SESSION orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT8_Unmarshal((UINT8 *)target, buffer, size);  /* libtpms changed */
ffd936
@@ -493,6 +514,7 @@ TPMA_SESSION_Unmarshal(TPMA_SESSION *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (*target & TPMA_SESSION_reserved) {
ffd936
 	    rc = TPM_RC_RESERVED_BITS;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -517,6 +539,7 @@ TPM_RC
ffd936
 TPMA_CC_Unmarshal(TPMA_CC *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMA_CC orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT32_Unmarshal((UINT32 *)target, buffer, size); /* libtpms changed */
ffd936
@@ -524,6 +547,7 @@ TPMA_CC_Unmarshal(TPMA_CC *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (*target & TPMA_CC_reserved) {
ffd936
 	    rc = TPM_RC_RESERVED_BITS;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -535,6 +559,7 @@ TPM_RC
ffd936
 TPMI_YES_NO_Unmarshal(TPMI_YES_NO *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_YES_NO orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT8_Unmarshal(target, buffer, size);  
ffd936
@@ -546,6 +571,7 @@ TPMI_YES_NO_Unmarshal(TPMI_YES_NO *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -557,6 +583,7 @@ TPM_RC
ffd936
 TPMI_DH_OBJECT_Unmarshal(TPMI_DH_OBJECT *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_DH_OBJECT orig_target = *target; // libtpms added
ffd936
     
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -569,6 +596,7 @@ TPMI_DH_OBJECT_Unmarshal(TPMI_DH_OBJECT *target, BYTE **buffer, INT32 *size, BOO
ffd936
 	    isNotPersistent &&
ffd936
 	    isNotLegalNull) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -580,6 +608,7 @@ TPM_RC
ffd936
 TPMI_DH_PARENT_Unmarshal(TPMI_DH_PARENT *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_DH_PARENT orig_target = *target; // libtpms added
ffd936
     
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -598,6 +627,7 @@ TPMI_DH_PARENT_Unmarshal(TPMI_DH_PARENT *target, BYTE **buffer, INT32 *size, BOO
ffd936
 	    isNotEndorsement && 
ffd936
 	    isNotLegalNull) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -609,6 +639,7 @@ TPM_RC
ffd936
 TPMI_DH_PERSISTENT_Unmarshal(TPMI_DH_PERSISTENT *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_DH_PERSISTENT orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -617,6 +648,7 @@ TPMI_DH_PERSISTENT_Unmarshal(TPMI_DH_PERSISTENT *target, BYTE **buffer, INT32 *s
ffd936
 	BOOL isNotPersistent = (*target < PERSISTENT_FIRST) || (*target > PERSISTENT_LAST);
ffd936
 	if (isNotPersistent) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -628,6 +660,7 @@ TPM_RC
ffd936
 TPMI_DH_ENTITY_Unmarshal(TPMI_DH_ENTITY *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_DH_ENTITY orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -654,6 +687,7 @@ TPMI_DH_ENTITY_Unmarshal(TPMI_DH_ENTITY *target, BYTE **buffer, INT32 *size, BOO
ffd936
 	    isNotAuth &&
ffd936
 	    isNotLegalNull) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -665,6 +699,7 @@ TPM_RC
ffd936
 TPMI_DH_PCR_Unmarshal(TPMI_DH_PCR *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_DH_PCR orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -675,6 +710,7 @@ TPMI_DH_PCR_Unmarshal(TPMI_DH_PCR *target, BYTE **buffer, INT32 *size, BOOL allo
ffd936
 	if (isNotPcr &&
ffd936
 	    isNotLegalNull) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -686,6 +722,7 @@ TPM_RC
ffd936
 TPMI_SH_AUTH_SESSION_Unmarshal(TPMI_SH_AUTH_SESSION *target, BYTE **buffer, INT32 *size, BOOL allowPwd)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_SH_AUTH_SESSION orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -698,6 +735,7 @@ TPMI_SH_AUTH_SESSION_Unmarshal(TPMI_SH_AUTH_SESSION *target, BYTE **buffer, INT3
ffd936
 	    isNotPolicySession &&
ffd936
 	    isNotLegalPwd) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -709,6 +747,7 @@ TPM_RC
ffd936
 TPMI_SH_HMAC_Unmarshal(TPMI_SH_HMAC *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_SH_HMAC orig_target = *target; // libtpms added
ffd936
     
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -717,6 +756,7 @@ TPMI_SH_HMAC_Unmarshal(TPMI_SH_HMAC *target, BYTE **buffer, INT32 *size)
ffd936
 	BOOL isNotHmacSession = (*target < HMAC_SESSION_FIRST ) || (*target > HMAC_SESSION_LAST);
ffd936
 	if (isNotHmacSession) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -728,6 +768,7 @@ TPM_RC
ffd936
 TPMI_SH_POLICY_Unmarshal(TPMI_SH_POLICY *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_SH_POLICY orig_target = *target; // libtpms added
ffd936
     
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -736,6 +777,7 @@ TPMI_SH_POLICY_Unmarshal(TPMI_SH_POLICY *target, BYTE **buffer, INT32 *size)
ffd936
 	BOOL isNotPolicySession = (*target < POLICY_SESSION_FIRST) || (*target > POLICY_SESSION_LAST);
ffd936
 	if (isNotPolicySession) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -747,6 +789,7 @@ TPM_RC
ffd936
 TPMI_DH_CONTEXT_Unmarshal(TPMI_DH_CONTEXT *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_DH_CONTEXT orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -759,6 +802,7 @@ TPMI_DH_CONTEXT_Unmarshal(TPMI_DH_CONTEXT *target, BYTE **buffer, INT32 *size)
ffd936
 	    isNotPolicySession &&
ffd936
 	    isNotTransient) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -770,6 +814,7 @@ TPM_RC
ffd936
 TPMI_DH_SAVED_Unmarshal(TPMI_DH_SAVED *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_DH_SAVED orig_target = *target; // libtpms added
ffd936
     allowNull = allowNull;
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
@@ -787,6 +832,7 @@ TPMI_DH_SAVED_Unmarshal(TPMI_DH_SAVED *target, BYTE **buffer, INT32 *size, BOOL
ffd936
 	    isNotSequenceObject &&
ffd936
 	    isNotTransientStClear) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -798,6 +844,7 @@ TPM_RC
ffd936
 TPMI_RH_HIERARCHY_Unmarshal(TPMI_RH_HIERARCHY *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_HIERARCHY orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -814,6 +861,7 @@ TPMI_RH_HIERARCHY_Unmarshal(TPMI_RH_HIERARCHY *target, BYTE **buffer, INT32 *siz
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -825,6 +873,7 @@ TPM_RC
ffd936
 TPMI_RH_ENABLES_Unmarshal(TPMI_RH_ENABLES *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_ENABLES orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -842,6 +891,7 @@ TPMI_RH_ENABLES_Unmarshal(TPMI_RH_ENABLES *target, BYTE **buffer, INT32 *size, B
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -853,6 +903,7 @@ TPM_RC
ffd936
 TPMI_RH_HIERARCHY_AUTH_Unmarshal(TPMI_RH_HIERARCHY_AUTH *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_HIERARCHY_AUTH orig_target = *target; // libtpms added
ffd936
     
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -866,6 +917,7 @@ TPMI_RH_HIERARCHY_AUTH_Unmarshal(TPMI_RH_HIERARCHY_AUTH *target, BYTE **buffer,
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -877,6 +929,7 @@ TPM_RC
ffd936
 TPMI_RH_PLATFORM_Unmarshal(TPMI_RH_PLATFORM *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_PLATFORM orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -887,6 +940,7 @@ TPMI_RH_PLATFORM_Unmarshal(TPMI_RH_PLATFORM *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -898,6 +952,7 @@ TPM_RC
ffd936
 TPMI_RH_ENDORSEMENT_Unmarshal(TPMI_RH_ENDORSEMENT *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_ENDORSEMENT orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -912,6 +967,7 @@ TPMI_RH_ENDORSEMENT_Unmarshal(TPMI_RH_ENDORSEMENT *target, BYTE **buffer, INT32
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -923,6 +979,7 @@ TPM_RC
ffd936
 TPMI_RH_PROVISION_Unmarshal(TPMI_RH_PROVISION *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_PROVISION orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -934,6 +991,7 @@ TPMI_RH_PROVISION_Unmarshal(TPMI_RH_PROVISION *target, BYTE **buffer, INT32 *siz
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -945,6 +1003,7 @@ TPM_RC
ffd936
 TPMI_RH_CLEAR_Unmarshal(TPMI_RH_CLEAR *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_CLEAR orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -956,6 +1015,7 @@ TPMI_RH_CLEAR_Unmarshal(TPMI_RH_CLEAR *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -967,6 +1027,7 @@ TPM_RC
ffd936
 TPMI_RH_NV_AUTH_Unmarshal(TPMI_RH_NV_AUTH *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_NV_AUTH orig_target = *target; // libtpms added
ffd936
     
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -981,6 +1042,7 @@ TPMI_RH_NV_AUTH_Unmarshal(TPMI_RH_NV_AUTH *target, BYTE **buffer, INT32 *size)
ffd936
 		  BOOL isNotNv = (*target < NV_INDEX_FIRST) || (*target > NV_INDEX_LAST);
ffd936
 		  if (isNotNv) {
ffd936
 		      rc = TPM_RC_VALUE;
ffd936
+		      *target = orig_target; // libtpms added
ffd936
 		  }
ffd936
 	      }
ffd936
 	}
ffd936
@@ -994,6 +1056,7 @@ TPM_RC
ffd936
 TPMI_RH_LOCKOUT_Unmarshal(TPMI_RH_LOCKOUT *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_LOCKOUT orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -1004,6 +1067,7 @@ TPMI_RH_LOCKOUT_Unmarshal(TPMI_RH_LOCKOUT *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1015,6 +1079,7 @@ TPM_RC
ffd936
 TPMI_RH_NV_INDEX_Unmarshal(TPMI_RH_NV_INDEX *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RH_NV_INDEX orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_HANDLE_Unmarshal(target, buffer, size);  
ffd936
@@ -1023,6 +1088,7 @@ TPMI_RH_NV_INDEX_Unmarshal(TPMI_RH_NV_INDEX *target, BYTE **buffer, INT32 *size)
ffd936
 	BOOL isNotNv = (*target < NV_INDEX_FIRST) || (*target > NV_INDEX_LAST);
ffd936
 	if (isNotNv) {
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1034,6 +1100,7 @@ TPM_RC
ffd936
 TPMI_ALG_HASH_Unmarshal(TPMI_ALG_HASH *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_HASH orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1062,6 +1129,7 @@ TPMI_ALG_HASH_Unmarshal(TPMI_ALG_HASH *target, BYTE **buffer, INT32 *size, BOOL
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_HASH;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1073,6 +1141,7 @@ TPM_RC
ffd936
 TPMI_ALG_SYM_Unmarshal(TPMI_ALG_SYM *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_SYM orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1101,6 +1170,7 @@ TPMI_ALG_SYM_Unmarshal(TPMI_ALG_SYM *target, BYTE **buffer, INT32 *size, BOOL al
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_SYMMETRIC;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1112,6 +1182,7 @@ TPM_RC
ffd936
 TPMI_ALG_SYM_OBJECT_Unmarshal(TPMI_ALG_SYM_OBJECT *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_SYM_OBJECT orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1137,6 +1208,7 @@ TPMI_ALG_SYM_OBJECT_Unmarshal(TPMI_ALG_SYM_OBJECT *target, BYTE **buffer, INT32
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_SYMMETRIC;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1148,6 +1220,7 @@ TPM_RC
ffd936
 TPMI_ALG_SYM_MODE_Unmarshal(TPMI_ALG_SYM_MODE *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_SYM_MODE orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1179,6 +1252,7 @@ TPMI_ALG_SYM_MODE_Unmarshal(TPMI_ALG_SYM_MODE *target, BYTE **buffer, INT32 *siz
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_MODE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1190,6 +1264,7 @@ TPM_RC
ffd936
 TPMI_ALG_KDF_Unmarshal(TPMI_ALG_KDF *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_KDF orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1215,6 +1290,7 @@ TPMI_ALG_KDF_Unmarshal(TPMI_ALG_KDF *target, BYTE **buffer, INT32 *size, BOOL al
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_KDF;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1226,6 +1302,7 @@ TPM_RC
ffd936
 TPMI_ALG_SIG_SCHEME_Unmarshal(TPMI_ALG_SIG_SCHEME *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_SIG_SCHEME orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1260,6 +1337,7 @@ TPMI_ALG_SIG_SCHEME_Unmarshal(TPMI_ALG_SIG_SCHEME *target, BYTE **buffer, INT32
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_SCHEME;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1271,6 +1349,7 @@ TPM_RC
ffd936
 TPMI_ECC_KEY_EXCHANGE_Unmarshal(TPMI_ECC_KEY_EXCHANGE *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ECC_KEY_EXCHANGE orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1293,6 +1372,7 @@ TPMI_ECC_KEY_EXCHANGE_Unmarshal(TPMI_ECC_KEY_EXCHANGE *target, BYTE **buffer, IN
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_SCHEME;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1305,6 +1385,7 @@ TPM_RC
ffd936
 TPMI_ST_COMMAND_TAG_Unmarshal(TPMI_ST_COMMAND_TAG *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ST_COMMAND_TAG orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ST_Unmarshal(target, buffer, size);  
ffd936
@@ -1316,6 +1397,7 @@ TPMI_ST_COMMAND_TAG_Unmarshal(TPMI_ST_COMMAND_TAG *target, BYTE **buffer, INT32
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_BAD_TAG;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1327,6 +1409,7 @@ TPM_RC
ffd936
 TPMI_ALG_MAC_SCHEME_Unmarshal(TPMI_ALG_MAC_SCHEME *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_MAC_SCHEME orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1358,6 +1441,7 @@ TPMI_ALG_MAC_SCHEME_Unmarshal(TPMI_ALG_MAC_SCHEME *target, BYTE **buffer, INT32
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_SYMMETRIC;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1369,6 +1453,7 @@ TPM_RC
ffd936
 TPMI_ALG_CIPHER_MODE_Unmarshal(TPMI_ALG_CIPHER_MODE*target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_CIPHER_MODE orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -1397,6 +1482,7 @@ TPMI_ALG_CIPHER_MODE_Unmarshal(TPMI_ALG_CIPHER_MODE*target, BYTE **buffer, INT32
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_MODE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -1633,6 +1719,7 @@ TPM_RC
ffd936
 TPMT_TK_CREATION_Unmarshal(TPMT_TK_CREATION *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_ST orig_tag = target->tag; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ST_Unmarshal(&target->tag, buffer, size);
ffd936
@@ -1640,6 +1727,7 @@ TPMT_TK_CREATION_Unmarshal(TPMT_TK_CREATION *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->tag != TPM_ST_CREATION) {
ffd936
 	    rc = TPM_RC_TAG;
ffd936
+	    target->tag = orig_tag; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
@@ -1657,6 +1745,7 @@ TPM_RC
ffd936
 TPMT_TK_VERIFIED_Unmarshal(TPMT_TK_VERIFIED *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_ST orig_tag = target->tag; // libtpms added
ffd936
     
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ST_Unmarshal(&target->tag, buffer, size);
ffd936
@@ -1664,6 +1753,7 @@ TPMT_TK_VERIFIED_Unmarshal(TPMT_TK_VERIFIED *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->tag != TPM_ST_VERIFIED) {
ffd936
 	    rc = TPM_RC_TAG;
ffd936
+	    target->tag = orig_tag; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
@@ -1681,6 +1771,7 @@ TPM_RC
ffd936
 TPMT_TK_AUTH_Unmarshal(TPMT_TK_AUTH *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_ST orig_tag = target->tag; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ST_Unmarshal(&target->tag, buffer, size);
ffd936
@@ -1689,6 +1780,7 @@ TPMT_TK_AUTH_Unmarshal(TPMT_TK_AUTH *target, BYTE **buffer, INT32 *size)
ffd936
 	if ((target->tag != TPM_ST_AUTH_SIGNED) &&
ffd936
 	    (target->tag != TPM_ST_AUTH_SECRET)) {
ffd936
 	    rc = TPM_RC_TAG;
ffd936
+	    target->tag = orig_tag; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
@@ -1706,6 +1798,7 @@ TPM_RC
ffd936
 TPMT_TK_HASHCHECK_Unmarshal(TPMT_TK_HASHCHECK *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPM_ST orig_tag = target->tag; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ST_Unmarshal(&target->tag, buffer, size);
ffd936
@@ -1713,6 +1806,7 @@ TPMT_TK_HASHCHECK_Unmarshal(TPMT_TK_HASHCHECK *target, BYTE **buffer, INT32 *siz
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (target->tag != TPM_ST_HASHCHECK) {
ffd936
 	    rc = TPM_RC_TAG;
ffd936
+	    target->tag = orig_tag; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
@@ -2303,6 +2397,7 @@ TPM_RC
ffd936
 TPMI_ST_ATTEST_Unmarshal(TPMI_ST_ATTEST *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ST_ATTEST orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ST_Unmarshal(target, buffer, size);
ffd936
@@ -2319,6 +2414,7 @@ TPMI_ST_ATTEST_Unmarshal(TPMI_ST_ATTEST *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_SELECTOR;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -2413,6 +2509,7 @@ TPM_RC
ffd936
 TPMI_AES_KEY_BITS_Unmarshal(TPMI_AES_KEY_BITS *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_AES_KEY_BITS orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_KEY_BITS_Unmarshal(target, buffer, size);  
ffd936
@@ -2424,6 +2521,7 @@ TPMI_AES_KEY_BITS_Unmarshal(TPMI_AES_KEY_BITS *target, BYTE **buffer, INT32 *siz
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -2435,6 +2533,7 @@ TPM_RC
ffd936
 TPMI_CAMELLIA_KEY_BITS_Unmarshal(TPMI_CAMELLIA_KEY_BITS *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_CAMELLIA_KEY_BITS orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_KEY_BITS_Unmarshal(target, buffer, size);  
ffd936
@@ -2445,6 +2544,7 @@ TPMI_CAMELLIA_KEY_BITS_Unmarshal(TPMI_CAMELLIA_KEY_BITS *target, BYTE **buffer,
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -2456,6 +2556,7 @@ TPM_RC
ffd936
 TPMI_SM4_KEY_BITS_Unmarshal(TPMI_SM4_KEY_BITS *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_SM4_KEY_BITS orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_KEY_BITS_Unmarshal(target, buffer, size);  
ffd936
@@ -2466,6 +2567,7 @@ TPMI_SM4_KEY_BITS_Unmarshal(TPMI_SM4_KEY_BITS *target, BYTE **buffer, INT32 *siz
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -2477,6 +2579,7 @@ TPM_RC
ffd936
 TPMI_TDES_KEY_BITS_Unmarshal(TPMI_SM4_KEY_BITS *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_SM4_KEY_BITS orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_KEY_BITS_Unmarshal(target, buffer, size);
ffd936
@@ -2488,6 +2591,7 @@ TPMI_TDES_KEY_BITS_Unmarshal(TPMI_SM4_KEY_BITS *target, BYTE **buffer, INT32 *si
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -2760,6 +2864,7 @@ TPM_RC
ffd936
 TPMI_ALG_KEYEDHASH_SCHEME_Unmarshal(TPMI_ALG_KEYEDHASH_SCHEME *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_KEYEDHASH_SCHEME orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -2779,6 +2884,7 @@ TPMI_ALG_KEYEDHASH_SCHEME_Unmarshal(TPMI_ALG_KEYEDHASH_SCHEME *target, BYTE **bu
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3163,6 +3269,7 @@ TPM_RC
ffd936
 TPMI_ALG_ASYM_SCHEME_Unmarshal(TPMI_ALG_ASYM_SCHEME *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_ASYM_SCHEME orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -3206,6 +3313,7 @@ TPMI_ALG_ASYM_SCHEME_Unmarshal(TPMI_ALG_ASYM_SCHEME *target, BYTE **buffer, INT3
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3284,6 +3392,7 @@ TPM_RC
ffd936
 TPMI_ALG_RSA_SCHEME_Unmarshal(TPMI_ALG_RSA_SCHEME *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_RSA_SCHEME orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -3309,6 +3418,7 @@ TPMI_ALG_RSA_SCHEME_Unmarshal(TPMI_ALG_RSA_SCHEME *target, BYTE **buffer, INT32
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3336,6 +3446,7 @@ TPM_RC
ffd936
 TPMI_ALG_RSA_DECRYPT_Unmarshal(TPMI_ALG_RSA_DECRYPT *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_RSA_DECRYPT orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -3355,6 +3466,7 @@ TPMI_ALG_RSA_DECRYPT_Unmarshal(TPMI_ALG_RSA_DECRYPT *target, BYTE **buffer, INT3
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3395,6 +3507,7 @@ TPM_RC
ffd936
 TPMI_RSA_KEY_BITS_Unmarshal(TPMI_RSA_KEY_BITS *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_RSA_KEY_BITS orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_KEY_BITS_Unmarshal(target, buffer, size);  
ffd936
@@ -3406,6 +3519,7 @@ TPMI_RSA_KEY_BITS_Unmarshal(TPMI_RSA_KEY_BITS *target, BYTE **buffer, INT32 *siz
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_VALUE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3490,6 +3604,7 @@ TPM_RC
ffd936
 TPMI_ALG_ECC_SCHEME_Unmarshal(TPMI_ALG_ECC_SCHEME *target, BYTE **buffer, INT32 *size, BOOL allowNull)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_ECC_SCHEME orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -3521,6 +3636,7 @@ TPMI_ALG_ECC_SCHEME_Unmarshal(TPMI_ALG_ECC_SCHEME *target, BYTE **buffer, INT32
ffd936
 	    }
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_SCHEME;
ffd936
+	    *target = orig_target; //  libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3532,6 +3648,7 @@ TPM_RC
ffd936
 TPMI_ECC_CURVE_Unmarshal(TPMI_ECC_CURVE *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ECC_CURVE orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ECC_CURVE_Unmarshal(target, buffer, size);  
ffd936
@@ -3568,6 +3685,7 @@ TPMI_ECC_CURVE_Unmarshal(TPMI_ECC_CURVE *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_CURVE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -3782,6 +3900,7 @@ TPM_RC
ffd936
 TPMI_ALG_PUBLIC_Unmarshal(TPMI_ALG_PUBLIC *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMI_ALG_PUBLIC orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = TPM_ALG_ID_Unmarshal(target, buffer, size);  
ffd936
@@ -3803,6 +3922,7 @@ TPMI_ALG_PUBLIC_Unmarshal(TPMI_ALG_PUBLIC *target, BYTE **buffer, INT32 *size)
ffd936
 	    break;
ffd936
 	  default:
ffd936
 	    rc = TPM_RC_TYPE;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
@@ -4137,6 +4257,7 @@ TPM_RC
ffd936
 TPMA_NV_Unmarshal(TPMA_NV *target, BYTE **buffer, INT32 *size)
ffd936
 {
ffd936
     TPM_RC rc = TPM_RC_SUCCESS;
ffd936
+    TPMA_NV orig_target = *target; // libtpms added
ffd936
 
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	rc = UINT32_Unmarshal((UINT32 *)target, buffer, size);  /* libtpms changed */
ffd936
@@ -4144,6 +4265,7 @@ TPMA_NV_Unmarshal(TPMA_NV *target, BYTE **buffer, INT32 *size)
ffd936
     if (rc == TPM_RC_SUCCESS) {
ffd936
 	if (*target & TPMA_NV_RESERVED) {
ffd936
 	    rc = TPM_RC_RESERVED_BITS;
ffd936
+	    *target = orig_target; // libtpms added
ffd936
 	}
ffd936
     }
ffd936
     return rc;
ffd936
-- 
ffd936
2.29.0
ffd936