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

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