Blame allow-to-build-with-openssl-1.1.x.patch

Yunying Sun 835e95
diff -ruN tpm2.0-tools-2.1.0-orig/lib/tpm_kdfa.c tpm2.0-tools-2.1.0/lib/tpm_kdfa.c
Yunying Sun 835e95
--- tpm2.0-tools-2.1.0-orig/lib/tpm_kdfa.c	2017-07-25 01:50:33.000000000 +0800
Yunying Sun 835e95
+++ tpm2.0-tools-2.1.0/lib/tpm_kdfa.c	2017-08-15 10:48:23.063493627 +0800
Yunying Sun 835e95
@@ -51,6 +51,34 @@
Yunying Sun 835e95
     /* no return, not possible */
Yunying Sun 835e95
 }
Yunying Sun 835e95
 
Yunying Sun 835e95
+static HMAC_CTX *hmac_alloc()
Yunying Sun 835e95
+{
Yunying Sun 835e95
+    HMAC_CTX *ctx;
Yunying Sun 835e95
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL /* OpenSSL 1.1.0 */
Yunying Sun 835e95
+    ctx = malloc(sizeof(*ctx));
Yunying Sun 835e95
+#else
Yunying Sun 835e95
+    ctx = HMAC_CTX_new();
Yunying Sun 835e95
+#endif
Yunying Sun 835e95
+    if (!ctx)
Yunying Sun 835e95
+        return NULL;
Yunying Sun 835e95
+
Yunying Sun 835e95
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL
Yunying Sun 835e95
+    HMAC_CTX_init(ctx);
Yunying Sun 835e95
+#endif
Yunying Sun 835e95
+
Yunying Sun 835e95
+    return ctx;
Yunying Sun 835e95
+}
Yunying Sun 835e95
+
Yunying Sun 835e95
+static void hmac_del(HMAC_CTX *ctx)
Yunying Sun 835e95
+{
Yunying Sun 835e95
+#if OPENSSL_VERSION_NUMBER < 0x1010000fL
Yunying Sun 835e95
+    HMAC_CTX_cleanup(ctx);
Yunying Sun 835e95
+    free(ctx);
Yunying Sun 835e95
+#else
Yunying Sun 835e95
+    HMAC_CTX_free(ctx);
Yunying Sun 835e95
+#endif
Yunying Sun 835e95
+}
Yunying Sun 835e95
+
Yunying Sun 835e95
 TPM_RC tpm_kdfa(TPMI_ALG_HASH hashAlg,
Yunying Sun 835e95
         TPM2B *key, char *label, TPM2B *contextU, TPM2B *contextV, UINT16 bits,
Yunying Sun 835e95
         TPM2B_MAX_BUFFER  *resultKey )
Yunying Sun 835e95
@@ -90,12 +118,17 @@
Yunying Sun 835e95
         return TPM_RC_HASH;
Yunying Sun 835e95
     }
Yunying Sun 835e95
 
Yunying Sun 835e95
-    HMAC_CTX ctx;
Yunying Sun 835e95
-    HMAC_CTX_init(&ctx;;
Yunying Sun 835e95
-    int rc = HMAC_Init_ex(&ctx, key->buffer, key->size, md, NULL);
Yunying Sun 835e95
+    HMAC_CTX *ctx = hmac_alloc();
Yunying Sun 835e95
+    if (!ctx) {
Yunying Sun 835e95
+        LOG_ERR("HMAC context allocation failed");
Yunying Sun 835e95
+        return TPM_RC_MEMORY;
Yunying Sun 835e95
+    }
Yunying Sun 835e95
+
Yunying Sun 835e95
+    int rc = HMAC_Init_ex(ctx, key->buffer, key->size, md, NULL);
Yunying Sun 835e95
     if (!rc) {
Yunying Sun 835e95
         LOG_ERR("HMAC Init failed: %s", ERR_error_string(rc, NULL));
Yunying Sun 835e95
-        return TPM_RC_MEMORY;
Yunying Sun 835e95
+        rval = TPM_RC_MEMORY;
Yunying Sun 835e95
+        goto err;
Yunying Sun 835e95
     }
Yunying Sun 835e95
 
Yunying Sun 835e95
     // TODO Why is this a loop? It appears to only execute once.
Yunying Sun 835e95
@@ -118,7 +151,7 @@
Yunying Sun 835e95
         int c;
Yunying Sun 835e95
         for(c=0; c < j; c++) {
Yunying Sun 835e95
             TPM2B_DIGEST *digest = bufferList[c];
Yunying Sun 835e95
-            int rc =  HMAC_Update(&ctx, digest->b.buffer, digest->b.size);
Yunying Sun 835e95
+            int rc =  HMAC_Update(ctx, digest->b.buffer, digest->b.size);
Yunying Sun 835e95
             if (!rc) {
Yunying Sun 835e95
                 LOG_ERR("HMAC Update failed: %s", ERR_error_string(rc, NULL));
Yunying Sun 835e95
                 rval = TPM_RC_MEMORY;
Yunying Sun 835e95
@@ -127,7 +160,7 @@
Yunying Sun 835e95
         }
Yunying Sun 835e95
 
Yunying Sun 835e95
         unsigned size = sizeof(tmpResult.t.buffer);
Yunying Sun 835e95
-        int rc = HMAC_Final(&ctx, tmpResult.t.buffer, &size);
Yunying Sun 835e95
+        int rc = HMAC_Final(ctx, tmpResult.t.buffer, &size);
Yunying Sun 835e95
         if (!rc) {
Yunying Sun 835e95
             LOG_ERR("HMAC Final failed: %s", ERR_error_string(rc, NULL));
Yunying Sun 835e95
             rval = TPM_RC_MEMORY;
Yunying Sun 835e95
@@ -147,7 +180,7 @@
Yunying Sun 835e95
     resultKey->t.size = bytes;
Yunying Sun 835e95
 
Yunying Sun 835e95
 err:
Yunying Sun 835e95
-    HMAC_CTX_cleanup(&ctx;;
Yunying Sun 835e95
+    hmac_del(ctx);
Yunying Sun 835e95
 
Yunying Sun 835e95
     return rval;
Yunying Sun 835e95
 }