Blame SOURCES/0001-tpm2_create.c-Fix-an-issue-where-userwithauth-attr-c.patch

ec8dc5
From 696a17861c38b38fb2acf888119d918eb9c12329 Mon Sep 17 00:00:00 2001
ec8dc5
From: Imran Desai <imran.desai@intel.com>
ec8dc5
Date: Thu, 21 May 2020 11:31:43 -0700
ec8dc5
Subject: [PATCH] tpm2_create.c: Fix an issue where userwithauth attr cleared
ec8dc5
 if policy specified
ec8dc5
ec8dc5
Fixes #2037
ec8dc5
ec8dc5
Signed-off-by: Imran Desai <imran.desai@intel.com>
ec8dc5
---
ec8dc5
 man/tpm2_create.1.md                 |  9 +++-
ec8dc5
 test/integration/tests/import_tpm.sh | 78 +++++++++++++++++-----------
ec8dc5
 tools/tpm2_create.c                  | 10 ++--
ec8dc5
 3 files changed, 60 insertions(+), 37 deletions(-)
ec8dc5
ec8dc5
diff --git a/man/tpm2_create.1.md b/man/tpm2_create.1.md
ec8dc5
index e8e5eaac49c3..9a7ba33e6017 100644
ec8dc5
--- a/man/tpm2_create.1.md
ec8dc5
+++ b/man/tpm2_create.1.md
ec8dc5
@@ -13,7 +13,7 @@
ec8dc5
 **tpm2_create**(1) - Create a child object. The object can either be a key or
ec8dc5
 a sealing object. A sealing object allows to seal user data to the TPM, with a
ec8dc5
 maximum size of 256 bytes. Additionally it will load the created object if the
ec8dc5
-**-o** is specified.
ec8dc5
+**-c** is specified.
ec8dc5
 
ec8dc5
 # OPTIONS
ec8dc5
 
ec8dc5
@@ -55,6 +55,13 @@ These options for creating the TPM entity:
ec8dc5
     and unsealing. I.e. one cannot use an object for sealing and cryptography
ec8dc5
     operations.
ec8dc5
 
ec8dc5
+    When **-L** is specified for adding policy based authorization information
ec8dc5
+    AND no string password is specified, the  attribute `TPMA_OBJECT_USERWITHAUTH`
ec8dc5
+    is cleared unless an explicit choice is made by setting of the attribute
ec8dc5
+    with **-a** option. This prevents creation of objects with inadvertant auth
ec8dc5
+    model where in user intended to enforce a policy but inadvertantly created
ec8dc5
+    an object with empty auth which can be used instead of policy authorization.
ec8dc5
+
ec8dc5
   * **-i**, **\--sealing-input**=_FILE_ or _STDIN_:
ec8dc5
 
ec8dc5
     The data file to be sealed, optional. If file is -, read from stdin.
ec8dc5
diff --git a/test/integration/tests/import_tpm.sh b/test/integration/tests/import_tpm.sh
ec8dc5
index ff48185aba70..3d1e10820844 100755
ec8dc5
--- a/test/integration/tests/import_tpm.sh
ec8dc5
+++ b/test/integration/tests/import_tpm.sh
ec8dc5
@@ -54,8 +54,13 @@ load_new_parent() {
ec8dc5
 create_load_duplicatee() {
ec8dc5
     # Create the key we want to duplicate
ec8dc5
     create_policy dpolicy.dat TPM2_CC_Duplicate
ec8dc5
-    tpm2_create -Q -C primary.ctx -g sha256 -G $1 -p foo -r key.prv -u key.pub \
ec8dc5
-    -L dpolicy.dat -a "sensitivedataorigin|decrypt|userwithauth"
ec8dc5
+    if [ -z "$2" ];then
ec8dc5
+        tpm2_create -Q -C primary.ctx -g sha256 -G $1 -r key.prv \
ec8dc5
+        -u key.pub -L dpolicy.dat -a "sensitivedataorigin|decrypt|userwithauth"
ec8dc5
+    else
ec8dc5
+        tpm2_create -Q -C primary.ctx -g sha256 -G $1 -p "$2" -r key.prv \
ec8dc5
+        -u key.pub -L dpolicy.dat -a "sensitivedataorigin|decrypt|userwithauth"
ec8dc5
+    fi
ec8dc5
     # Load the key
ec8dc5
     tpm2_load -Q -C primary.ctx -r key.prv -u key.pub -c key.ctx
ec8dc5
     # Extract the public part for import later
ec8dc5
@@ -113,34 +118,45 @@ for dup_key_type in aes rsa ecc; do
ec8dc5
     done
ec8dc5
 done
ec8dc5
 
ec8dc5
-# Part 2 :
ec8dc5
-# Create a rsa key (Kd)
ec8dc5
-# Encrypt a message using Kd
ec8dc5
-# Duplicate Kd
ec8dc5
-# Import & Load Kd
ec8dc5
-# Decrypt the message and verify
ec8dc5
-tpm2_createprimary -Q -C o -g sha256 -G rsa -c primary.ctx
ec8dc5
-# New parent ...
ec8dc5
-create_load_new_parent
ec8dc5
-# Key to be duplicated
ec8dc5
-create_load_duplicatee rsa
ec8dc5
-# Encrypt a secret message
ec8dc5
-echo "Mary had a little lamb ..." > plain.txt
ec8dc5
-tpm2_rsaencrypt -Q -c key.ctx -o cipher.txt plain.txt
ec8dc5
-# Duplicate the key
ec8dc5
-do_duplication null
ec8dc5
-# Remove, we're done with it
ec8dc5
-rm new_parent.ctx
ec8dc5
-# Load the full thing this time
ec8dc5
-load_new_parent
ec8dc5
-# Import & load the duplicate
ec8dc5
-do_import_load null
ec8dc5
-# Decrypt the secret message using duplicated key
ec8dc5
-tpm2_rsadecrypt -Q -p foo -c dup.ctx -o recovered.txt cipher.txt
ec8dc5
-# Check we got it right ...
ec8dc5
-diff recovered.txt plain.txt
ec8dc5
-# Cleanup
ec8dc5
-rm plain.txt recovered.txt cipher.txt
ec8dc5
-cleanup "no-shut-down"
ec8dc5
+test_key_usage() {
ec8dc5
+    # Part 2 :
ec8dc5
+    # Create a rsa key (Kd)
ec8dc5
+    # Encrypt a message using Kd
ec8dc5
+    # Duplicate Kd
ec8dc5
+    # Import & Load Kd
ec8dc5
+    # Decrypt the message and verify
ec8dc5
+    tpm2_createprimary -Q -C o -g sha256 -G rsa -c primary.ctx
ec8dc5
+    # New parent ...
ec8dc5
+    create_load_new_parent
ec8dc5
+    # Key to be duplicated
ec8dc5
+    create_load_duplicatee rsa "$1"
ec8dc5
+    # Encrypt a secret message
ec8dc5
+    echo "Mary had a little lamb ..." > plain.txt
ec8dc5
+    tpm2_rsaencrypt -Q -c key.ctx -o cipher.txt plain.txt
ec8dc5
+    # Duplicate the key
ec8dc5
+    do_duplication null
ec8dc5
+    # Remove, we're done with it
ec8dc5
+    rm new_parent.ctx
ec8dc5
+    # Load the full thing this time
ec8dc5
+    load_new_parent
ec8dc5
+    # Import & load the duplicate
ec8dc5
+    do_import_load null
ec8dc5
+    # Decrypt the secret message using duplicated key
ec8dc5
+    if [ -z "$1" ];then
ec8dc5
+        tpm2_rsadecrypt -Q -c dup.ctx -o recovered.txt cipher.txt
ec8dc5
+    else
ec8dc5
+        tpm2_rsadecrypt -Q -p "$1" -c dup.ctx -o recovered.txt cipher.txt
ec8dc5
+    fi
ec8dc5
+    # Check we got it right ...
ec8dc5
+    diff recovered.txt plain.txt
ec8dc5
+    # Cleanup
ec8dc5
+    rm plain.txt recovered.txt cipher.txt
ec8dc5
+    cleanup "no-shut-down"
ec8dc5
+}
ec8dc5
+
ec8dc5
+#Test key with password
ec8dc5
+test_key_usage foo
ec8dc5
+#Test key without password
ec8dc5
+test_key_usage
ec8dc5
 
ec8dc5
 exit 0
ec8dc5
diff --git a/tools/tpm2_create.c b/tools/tpm2_create.c
ec8dc5
index 941b77655f55..8e92cc747e17 100644
ec8dc5
--- a/tools/tpm2_create.c
ec8dc5
+++ b/tools/tpm2_create.c
ec8dc5
@@ -47,7 +47,7 @@ struct tpm_create_ctx {
ec8dc5
     TPML_PCR_SELECTION creation_pcr;
ec8dc5
 
ec8dc5
     struct {
ec8dc5
-        UINT8 b :1;
ec8dc5
+        UINT8 a :1;
ec8dc5
         UINT8 i :1;
ec8dc5
         UINT8 L :1;
ec8dc5
         UINT8 u :1;
ec8dc5
@@ -224,7 +224,7 @@ static bool on_option(char key, char *value) {
ec8dc5
         break;
ec8dc5
     case 'a':
ec8dc5
         ctx.object.attrs = value;
ec8dc5
-        ctx.flags.b = 1;
ec8dc5
+        ctx.flags.a = 1;
ec8dc5
         break;
ec8dc5
     case 'i':
ec8dc5
         ctx.object.sealed_data = strcmp("-", value) ? value : NULL;
ec8dc5
@@ -346,12 +346,12 @@ tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
ec8dc5
 
ec8dc5
         ctx.object.alg = "keyedhash";
ec8dc5
 
ec8dc5
-        if (!ctx.flags.b) {
ec8dc5
+        if (!ctx.flags.a) {
ec8dc5
             attrs &= ~TPMA_OBJECT_SIGN_ENCRYPT;
ec8dc5
             attrs &= ~TPMA_OBJECT_DECRYPT;
ec8dc5
             attrs &= ~TPMA_OBJECT_SENSITIVEDATAORIGIN;
ec8dc5
         }
ec8dc5
-    } else if (!ctx.flags.b && !strncmp("hmac", ctx.object.alg, 4)) {
ec8dc5
+    } else if (!ctx.flags.a && !strncmp("hmac", ctx.object.alg, 4)) {
ec8dc5
         attrs &= ~TPMA_OBJECT_DECRYPT;
ec8dc5
     }
ec8dc5
 
ec8dc5
@@ -362,7 +362,7 @@ tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
ec8dc5
         return tool_rc_general_error;
ec8dc5
     }
ec8dc5
 
ec8dc5
-    if (ctx.flags.L && !ctx.object.auth_str) {
ec8dc5
+    if (!ctx.flags.a && ctx.flags.L && !ctx.object.auth_str) {
ec8dc5
         ctx.object.public.publicArea.objectAttributes &=
ec8dc5
                 ~TPMA_OBJECT_USERWITHAUTH;
ec8dc5
     }
ec8dc5
-- 
ec8dc5
2.27.0
ec8dc5