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

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