All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Gary Lin via Grub-devel <grub-devel@gnu.org>
To: The development of GNU GRUB <grub-devel@gnu.org>
Cc: Gary Lin <glin@suse.com>,
	Hernan Gatta <hegatta@linux.microsoft.com>,
	Daniel Axtens <dja@axtens.net>,
	Daniel Kiper <daniel.kiper@oracle.com>,
	shkhisti@microsoft.com, jaskaran.khurana@microsoft.com,
	christopher.co@microsoft.com, daniel.mihai@microsoft.com,
	jaredz@redhat.com, development@efficientek.com,
	jejb@linux.ibm.com, mchang@suse.com, patrick.colp@oracle.com,
	Stefan Berger <stefanb@linux.ibm.com>
Subject: [PATCH v13 00/20] Automatic Disk Unlock with TPM2
Date: Thu, 25 Apr 2024 16:01:46 +0800	[thread overview]
Message-ID: <20240425080206.23902-1-glin@suse.com> (raw)

GIT repo for v13: https://github.com/lcp/grub2/tree/tpm2-unlock-v13

This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by
Hernan Gatta to introduce the key protector framework and TPM2 stack
to GRUB2, and this could be a useful feature for the systems to
implement full disk encryption.

To support TPM 2.0 Key File format(*2), patch 1~5,7 are grabbed from
Daniel Axtens's "appended signature secure boot support" (*3) to import
libtasn1 into grub2. Besides, the libtasn1 version is upgraded to
4.19.0 instead of 4.16.0 in the original patch.

Patch 6 fixes a potential buffer overrun in libtasn1.
(https://gitlab.com/gnutls/libtasn1/-/issues/49)

Patch 8 adds the document for libtasn1 and the steps to upgrade the
library.

Patch 9~13 are Hernan Gatta's patches with the follow-up fixes and
improvements:
- Converting 8 spaces into 1 tab
- Merging the minor build fix from Michael Chang
  - Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf
  - Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
  - Based on the patch from Olaf Kirch <OKir@suse.com>
- Adjusting the input parameters of TPM2_EvictControl to match the order
  in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands on TPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command for TPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be
  NULL so that we don't have to declare empty variables
- Only enabling grub-protect for "efi" since the TPM2 stack currently
  relies on the EFI TCG2 protocol to send TPM2 commands
- Using grub_cpu_to_be*() in the TPM2 stack instead of grub_swap_bytes*()
  which may cause problems in big-indian machines
- Changing the short name of "--protector" of "cryptomount" from "-k" to
  "-P" to avoid the conflict with "--key-file"
- Supporting TPM 2.0 Key File Format besides the raw sealed key
- Adding the external libtasn1 dependency to grub-protect to write the
  TPM 2.0 Key files
- Extending the TPM2 TSS stack to support authorized policy

Patch 14 implements the authorized policy support.

Patch 15 implements the missing NV index mode. (Thanks to Patrick Colp)

Patch 16 improves the 'cryptomount' command to fall back to the
passphrase mode when the key protector fails to unlock the encrypted
partition. (Another patch from Patrick Colp)

Patch 17 and 18 fix the potential security issues spotted by Fabian Vogt.

Patch 19 and 20 implement the TPM key unsealing testcases.

To utilize the TPM2 key protector to unlock the encrypted partition
(sdb1), here are the sample steps:

1. Add an extra random key for LUKS (luks-key)
   $ dd if=/dev/urandom of=luks-key bs=1 count=32
   $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf2

2. Seal the key
   $ sudo grub-protect --action=add \
                       --protector=tpm2 \
                       --tpm2key \
                       --tpm2-keyfile=luks-key \
                       --tpm2-outfile=/boot/efi/boot/grub2/sealed.tpm

3. Unseal the key with the proper commands in grub.cfg:
   tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm
   cryptomount -u <SDB1_UUID> -P tpm2

(*1) https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00006.html
(*2) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
(*3) https://lists.gnu.org/archive/html/grub-devel/2021-06/msg00044.html

v13:
- Fixing typos and a few multi-line comments
- Improving the conditional checks for the arguments of
  tpm2_key_protector_init 
- Updating to the latest TPM 2.0 Key File format
  - Adding the new optional fields: description and rsaParent
  - Handling "rsaParent == TRUE" when unsealing the tpm2key
  - Setting "rsaParent" to "TRUE" when sealing the key with RSA SRK
- Removing non-standard SRKs: RSA3072, RSA4096, ECC_NIST_P384,
  ECC_NIST_P521, and ECC_SM2_P256
- Adding more error messages to grub-protect
- Improving the error checking for the swtpm chardev instance
- Exiting the tpm2_test script if grub-protect failed to seal the key   

v12:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00108.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v12
- Fixing typos and indentation
- Removing the unused TPM commands: TPM2_HashSequenceStart,
  TPM2_SequenceUpdate, and TPM2_SequenceComplete,
- Following the TCG EK templates to set the parameters of SRK
- Removing ECC_BN_P256 and ECC_BN_P638 from the SRK algorithm list since
  those two algorithms are not mentioned in the TCG EK templates
- Updating the help messages of the tpm2 module and grub-protect
- Removing the unnecessary NULL checks
- Adding the manpage for grub-protect
- Replacing grub_crypto_hash() with TPM2_Hash() in grub-protect to
  support SHA384 PCR banks
- Using 'swtpm chardev' to start swtpm instead of 'swtpm_cuse' since
  some distros may not build swtpm with cuse
- Adding a new testcase without specifying the SRK type to test the
  default SRK settings
- Amending tpm2_test to remove the duplicate error checking code
- Silencing the tpm2-tools commands in tpm2_test
- Fixing the exit trap of tpm2_test to removing the working directory
  on success

v11:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00052.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v11
- Adding the missing default: handlers in grub-core/tpm2/mu.c
- Updating the help messages and commit messages to reflect the change
  of the default SRK algorithm (RSA2048 -> ECC_NIST_P256)
- Adding the testcase for the NV index mode

v10:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00019.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v10
- Fixing the coverity issues: CID 435775, CID 435771, CID 435770, CID
  435769, CID 435767, CID 435761
  https://lists.gnu.org/archive/html/grub-devel/2024-02/txtKIuUb5lf3O.txt
  - Fixing the potential memory leak (CID 435775)
  - Removing the unnecessary grub_protect_get_grub_drive_for_file() from
    util/grub-protect.c (CID 435771)
  - Using the grub_tpm2_mu_TPM2B_*_Unmarshal functions to unmarshal the
    TPM2B structs instead of a generic grub_tpm2_mu_TPM2B_Unmarshal
    (CID 435770)
  - Fixing Null pointer dereference (CID 435769)
  - Adding bound checks to grub_tpm2_mu_TPML_DIGEST_Unmarshal()
    (CID 435767)
  - Improving the check for the return value of ftell() (CID 435761)
- Adding a quick fix for CID 435762
- Removing the empty ending line in tests/asn1_test.in
- Fixing docs/grub-dev.texi and updating the libtasn1 patches in
  grub-core/lib/libtasn1-patches/
- Merging all the TPM2 TSS stack patches into one to reduce the total
  patch number
- Switching the default asymmetric algorithm from RSA2048 to
  TPM_ECC_NIST_P256 for the faster key generation
- Adding the fallback SRK templates to try a few more SRK types in case
  grub2 failed to associate the sealed key with the SRK in the persistent
  handle or the default SRK
- Improving the test script to add tests for the persistent handle and
  the fallback SRKs

v9:
- https://lists.gnu.org/archive/html/grub-devel/2024-02/msg00007.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v9
- Introducing c-ctype.h to posix_wrap and implementing strncat
- Adding the descriptive comments to the disabled code in libtasn1
- Replacing strcat with the bound-checked _asn1_str_cat in libtasn1 and
  including c-ctype.h directly
- Integrating the asn1 testcases into "functional_test"
- Updating the libtasn1 patches mentioned in the documentation 
- Moving the key protector to a module
- Amending configure.ac to enable/disable grub-protect
- Fixing an timeout issue in the tpm2_test script by feeding the config
  through stdin

v8:
- https://lists.gnu.org/archive/html/grub-devel/2024-01/msg00013.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v8
- Introducing TPM device support to grub-emu and adding the TPM key
  unsealing testcase

v7:
- https://lists.gnu.org/archive/html/grub-devel/2023-11/msg00127.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v7
- Stopping reading SRK from the well-known persistent handle (TPM2_SRK_HANDLE,
  i.e. 0x81000001) by default since the persistent handle may be created
  by other OS and causes unsealing failure due to SRK mismatching
  - The user now has to specify the persistent handle with "--srk"
    explicitly.
- Utilizing grub_error() to print more error messages 
- Unifying the format of the error messages from TPM2 commands

v6:
- https://lists.gnu.org/archive/html/grub-devel/2023-10/msg00026.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v6
- Supporting more SRK types than RSA2048 and ECC_NIST_P256
- Documenting SHA512 as the supported PCR bank type in the tpm2
  protector
- Removing the redundant error message for grub_tpm2_protector_srk_get()
  since it may overwrite the real error message.
- Updating the supported SRK types and PCR bank types in grub-protect 
- Removing the unused type: TPM2_ECC_CURVE

v5:
- https://lists.gnu.org/archive/html/grub-devel/2023-08/msg00113.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v5
- Rebasing to the latest git HEAD and improving the commit messages
- Implementing authorized poilcy support
- Implementing NV index mode
- Improving the 'cryptomount' command to fall back to the passphrase
  mode when the key protector fails to unlock the encrypted partition
- Fixing the potential security issues

v4:
- https://lists.gnu.org/archive/html/grub-devel/2023-04/msg00104.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v4
- Improving the error condition checks in cryptodisk.c
- Moving the code to unseal with the standalone policy sequence below
  the code for authpolicy sequence
  - The standalone policy sequence was mistakenly prepended to to the
    authpolicy sequence with grub_list_push() while it should be
    appended.
- Pushing the error messages from the authpolicy sequence into the
  grub_error stack so that we can list all errors from the sequence
- Improving the error messages in the TPM2 protector
- Amending the calculation of the max string lengths of 'Policy',
  'CommandCode' and 'CommandPolicy'
- Skipping the error path in grub_tpm2key_get_authpolicy_seq() on
  success to avoid freeing the authpolicy sequence

v3:
- https://lists.gnu.org/archive/html/grub-devel/2023-04/msg00055.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v3
- Adding the document for libtasn1
- Improving the error condition checks
  ex: "if (!ptr)" ==> "if (ptr == NULL)"
      "if (err)" ==> "if (err != GRUB_ERR_NONE)"
      "if (rc)" ==> "if (rc != TPM_RC_SUCCESS)"
- Supporting the "TPMPolicy" and "TPMAuthPolicy" sequence in the TPM 2.0
  key File
- Refactoring the key recover function to support "TPMPolicy" and
  "TPMAuthPolicy" sequence
- Using TPMS_PCR_SELECTION_SelectPCR() to set the PCR bit mask
  - Also dropping TPM2_PCR_TO_SELECT() and TPM2_PCR_TO_BIT() which are
    not necessary anymore
- Removing the redundant variable, 'crd', from
  grub_cryptodisk_scan_device_real()
- Fixing the spaces/tabs in cryptodisk.c
- Fixing the comment format in cryptodisk.h
- Adding the defensive check for "cargs->protectors" in
  grub_cryptodisk_scan_device()
- Improving 'grub-protect' for the better support of TPM 2.0 Key File
- Adding more comments

v2:
- https://lists.gnu.org/archive/html/grub-devel/2023-03/msg00094.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v2

v1:
- https://lists.gnu.org/archive/html/grub-devel/2023-02/msg00130.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock

Daniel Axtens (6):
  posix_wrap: tweaks in preparation for libtasn1
  libtasn1: import libtasn1-4.19.0
  libtasn1: disable code not needed in grub
  libtasn1: changes for grub compatibility
  libtasn1: compile into asn1 module
  asn1_test: test module for libtasn1

Gary Lin (7):
  libtasn1: fix the potential buffer overrun
  libtasn1: Add the documentation
  tpm2: Support authorized policy
  cryptodisk: wipe out the cached keys from protectors
  diskfilter: look up cryptodisk devices first
  tpm2: Enable tpm2 module for grub-emu
  tests: Add tpm2_test

Hernan Gatta (5):
  key_protector: Add key protectors framework
  tpm2: Add TPM Software Stack (TSS)
  key_protector: Add TPM2 Key Protector
  cryptodisk: Support key protectors
  util/grub-protect: Add new tool

Patrick Colp (2):
  tpm2: Implement NV index
  cryptodisk: Fallback to passphrase

 .gitignore                                    |    2 +
 Makefile.util.def                             |   37 +
 configure.ac                                  |   30 +
 docs/grub-dev.texi                            |   28 +
 docs/man/grub-protect.h2m                     |    4 +
 grub-core/Makefile.am                         |    1 +
 grub-core/Makefile.core.def                   |   48 +
 grub-core/disk/cryptodisk.c                   |  183 +-
 grub-core/disk/diskfilter.c                   |   35 +-
 grub-core/disk/key_protector.c                |   78 +
 grub-core/kern/emu/main.c                     |   11 +-
 grub-core/kern/emu/misc.c                     |   51 +
 ...asn1-disable-code-not-needed-in-grub.patch |  320 +++
 ...tasn1-changes-for-grub-compatibility.patch |  135 +
 ...sn1-fix-the-potential-buffer-overrun.patch |   35 +
 grub-core/lib/libtasn1/COPYING                |   16 +
 grub-core/lib/libtasn1/README.md              |   98 +
 grub-core/lib/libtasn1/lib/coding.c           | 1433 ++++++++++
 grub-core/lib/libtasn1/lib/decoding.c         | 2503 +++++++++++++++++
 grub-core/lib/libtasn1/lib/element.c          | 1109 ++++++++
 grub-core/lib/libtasn1/lib/element.h          |   42 +
 grub-core/lib/libtasn1/lib/errors.c           |  103 +
 grub-core/lib/libtasn1/lib/gstr.c             |   74 +
 grub-core/lib/libtasn1/lib/gstr.h             |   50 +
 grub-core/lib/libtasn1/lib/int.h              |  220 ++
 grub-core/lib/libtasn1/lib/parser_aux.c       | 1178 ++++++++
 grub-core/lib/libtasn1/lib/parser_aux.h       |  172 ++
 grub-core/lib/libtasn1/lib/structure.c        | 1227 ++++++++
 grub-core/lib/libtasn1/lib/structure.h        |   46 +
 grub-core/lib/libtasn1_wrap/wrap.c            |   26 +
 grub-core/lib/posix_wrap/c-ctype.h            |  114 +
 grub-core/lib/posix_wrap/limits.h             |    1 +
 grub-core/lib/posix_wrap/stdlib.h             |    8 +
 grub-core/lib/posix_wrap/string.h             |   21 +
 grub-core/lib/posix_wrap/sys/types.h          |    1 +
 .../tests/asn1/CVE-2018-1000654-1_asn1_tab.h  |   32 +
 .../tests/asn1/CVE-2018-1000654-2_asn1_tab.h  |   36 +
 grub-core/tests/asn1/CVE-2018-1000654.c       |   58 +
 grub-core/tests/asn1/Test_overflow.c          |  134 +
 grub-core/tests/asn1/Test_simple.c            |  205 ++
 grub-core/tests/asn1/Test_strings.c           |  142 +
 grub-core/tests/asn1/asn1_test.c              |   49 +
 grub-core/tests/asn1/asn1_test.h              |   44 +
 grub-core/tests/asn1/object-id-decoding.c     |  109 +
 grub-core/tests/asn1/object-id-encoding.c     |  114 +
 grub-core/tests/asn1/octet-string.c           |  199 ++
 grub-core/tests/asn1/reproducers.c            |   80 +
 grub-core/tests/lib/functional_test.c         |    1 +
 grub-core/tpm2/args.c                         |  140 +
 grub-core/tpm2/buffer.c                       |  145 +
 grub-core/tpm2/module.c                       | 1325 +++++++++
 grub-core/tpm2/mu.c                           | 1168 ++++++++
 grub-core/tpm2/tcg2-emu.c                     |   52 +
 grub-core/tpm2/tcg2.c                         |  143 +
 grub-core/tpm2/tpm2.c                         | 1048 +++++++
 grub-core/tpm2/tpm2key.asn                    |   33 +
 grub-core/tpm2/tpm2key.c                      |  476 ++++
 grub-core/tpm2/tpm2key_asn1_tab.c             |   45 +
 include/grub/cryptodisk.h                     |   16 +
 include/grub/emu/misc.h                       |    5 +
 include/grub/key_protector.h                  |   46 +
 include/grub/libtasn1.h                       |  657 +++++
 include/grub/tpm2/buffer.h                    |   65 +
 include/grub/tpm2/internal/args.h             |   49 +
 include/grub/tpm2/internal/functions.h        |  156 +
 include/grub/tpm2/internal/structs.h          |  768 +++++
 include/grub/tpm2/internal/types.h            |  403 +++
 include/grub/tpm2/mu.h                        |  396 +++
 include/grub/tpm2/tcg2.h                      |   34 +
 include/grub/tpm2/tpm2.h                      |   34 +
 include/grub/tpm2/tpm2key.h                   |   86 +
 tests/asn1_test.in                            |   11 +
 tests/tpm2_test.in                            |  308 ++
 tests/util/grub-shell.in                      |    6 +-
 util/grub-protect.c                           | 1420 ++++++++++
 75 files changed, 19635 insertions(+), 43 deletions(-)
 create mode 100644 docs/man/grub-protect.h2m
 create mode 100644 grub-core/disk/key_protector.c
 create mode 100644 grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch
 create mode 100644 grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch
 create mode 100644 grub-core/lib/libtasn1-patches/0003-libtasn1-fix-the-potential-buffer-overrun.patch
 create mode 100644 grub-core/lib/libtasn1/COPYING
 create mode 100644 grub-core/lib/libtasn1/README.md
 create mode 100644 grub-core/lib/libtasn1/lib/coding.c
 create mode 100644 grub-core/lib/libtasn1/lib/decoding.c
 create mode 100644 grub-core/lib/libtasn1/lib/element.c
 create mode 100644 grub-core/lib/libtasn1/lib/element.h
 create mode 100644 grub-core/lib/libtasn1/lib/errors.c
 create mode 100644 grub-core/lib/libtasn1/lib/gstr.c
 create mode 100644 grub-core/lib/libtasn1/lib/gstr.h
 create mode 100644 grub-core/lib/libtasn1/lib/int.h
 create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.c
 create mode 100644 grub-core/lib/libtasn1/lib/parser_aux.h
 create mode 100644 grub-core/lib/libtasn1/lib/structure.c
 create mode 100644 grub-core/lib/libtasn1/lib/structure.h
 create mode 100644 grub-core/lib/libtasn1_wrap/wrap.c
 create mode 100644 grub-core/lib/posix_wrap/c-ctype.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654-2_asn1_tab.h
 create mode 100644 grub-core/tests/asn1/CVE-2018-1000654.c
 create mode 100644 grub-core/tests/asn1/Test_overflow.c
 create mode 100644 grub-core/tests/asn1/Test_simple.c
 create mode 100644 grub-core/tests/asn1/Test_strings.c
 create mode 100644 grub-core/tests/asn1/asn1_test.c
 create mode 100644 grub-core/tests/asn1/asn1_test.h
 create mode 100644 grub-core/tests/asn1/object-id-decoding.c
 create mode 100644 grub-core/tests/asn1/object-id-encoding.c
 create mode 100644 grub-core/tests/asn1/octet-string.c
 create mode 100644 grub-core/tests/asn1/reproducers.c
 create mode 100644 grub-core/tpm2/args.c
 create mode 100644 grub-core/tpm2/buffer.c
 create mode 100644 grub-core/tpm2/module.c
 create mode 100644 grub-core/tpm2/mu.c
 create mode 100644 grub-core/tpm2/tcg2-emu.c
 create mode 100644 grub-core/tpm2/tcg2.c
 create mode 100644 grub-core/tpm2/tpm2.c
 create mode 100644 grub-core/tpm2/tpm2key.asn
 create mode 100644 grub-core/tpm2/tpm2key.c
 create mode 100644 grub-core/tpm2/tpm2key_asn1_tab.c
 create mode 100644 include/grub/key_protector.h
 create mode 100644 include/grub/libtasn1.h
 create mode 100644 include/grub/tpm2/buffer.h
 create mode 100644 include/grub/tpm2/internal/args.h
 create mode 100644 include/grub/tpm2/internal/functions.h
 create mode 100644 include/grub/tpm2/internal/structs.h
 create mode 100644 include/grub/tpm2/internal/types.h
 create mode 100644 include/grub/tpm2/mu.h
 create mode 100644 include/grub/tpm2/tcg2.h
 create mode 100644 include/grub/tpm2/tpm2.h
 create mode 100644 include/grub/tpm2/tpm2key.h
 create mode 100644 tests/asn1_test.in
 create mode 100644 tests/tpm2_test.in
 create mode 100644 util/grub-protect.c

Range-diff against v12:
 -:  --------- >  1:  c6a7b2a55 posix_wrap: tweaks in preparation for libtasn1
 -:  --------- >  2:  c2dfb28d9 libtasn1: import libtasn1-4.19.0
 -:  --------- >  3:  f5c15acce libtasn1: disable code not needed in grub
 -:  --------- >  4:  60cf41fa3 libtasn1: changes for grub compatibility
 -:  --------- >  5:  ce32406ca libtasn1: fix the potential buffer overrun
 -:  --------- >  6:  6410e21d8 libtasn1: compile into asn1 module
 -:  --------- >  7:  a94e1b32c asn1_test: test module for libtasn1
 -:  --------- >  8:  0e6041be0 libtasn1: Add the documentation
 -:  --------- >  9:  386e4c04b key_protector: Add key protectors framework
 1:  ba40ad414 ! 10:  13117460e tpm2: Add TPM Software Stack (TSS)
    @@ Commit message
         TPM2_LoadExternal, TPM2_Hash, TPM2_VerifySignature,
         TPM2_PolicyAuthorize, TPM2_TestParms
     
    -    Cc: Stefan Berger <stefanb@linux.ibm.com>
         Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
         Signed-off-by: Gary Lin <glin@suse.com>
    +    Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
     
      ## grub-core/tpm2/buffer.c (new) ##
     @@
 2:  e40320b73 ! 11:  758d6ec5f key_protector: Add TPM2 Key Protector
    @@ Commit message
              policy      [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL
              secret      [2] EXPLICIT OCTET STRING OPTIONAL
              authPolicy  [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL
    +         description [4] EXPLICIT UTF8String OPTIONAL,
    +         rsaParent   [5] EXPLICIT BOOLEAN OPTIONAL,
              parent      INTEGER
              pubkey      OCTET STRING
              privkey     OCTET STRING
    @@ Commit message
         authorized policy, in the later commits.
     
         Cc: Stefan Berger <stefanb@linux.ibm.com>
    +    Cc: James Bottomley <jejb@linux.ibm.com>
         Signed-off-by: Hernan Gatta <hegatta@linux.microsoft.com>
         Signed-off-by: Gary Lin <glin@suse.com>
     
    @@ grub-core/tpm2/args.c (new)
     +      grub_strcasecmp (value, "ECC_NIST_P256") == 0)
     +    {
     +      srk_type->type = TPM_ALG_ECC;
    -+      srk_type->nameAlg = TPM_ALG_SHA256;
    -+      srk_type->aes_bits = 128;
     +      srk_type->detail.ecc_curve = TPM_ECC_NIST_P256;
     +    }
     +  else if (grub_strcasecmp (value, "RSA") == 0 ||
     +	   grub_strcasecmp (value, "RSA2048") == 0)
     +    {
     +      srk_type->type = TPM_ALG_RSA;
    -+      srk_type->nameAlg = TPM_ALG_SHA256;
    -+      srk_type->aes_bits = 128;
     +      srk_type->detail.rsa_bits = 2048;
     +    }
    -+  else if (grub_strcasecmp (value, "RSA3072") == 0)
    -+    {
    -+      srk_type->type = TPM_ALG_RSA;
    -+      srk_type->nameAlg = TPM_ALG_SHA384;
    -+      srk_type->aes_bits = 256;
    -+      srk_type->detail.rsa_bits = 3072;
    -+    }
    -+  else if (grub_strcasecmp (value, "RSA4096") == 0)
    -+    {
    -+      srk_type->type = TPM_ALG_RSA;
    -+      srk_type->nameAlg = TPM_ALG_SHA384;
    -+      srk_type->aes_bits = 256;
    -+      srk_type->detail.rsa_bits = 4096;
    -+    }
    -+  else if (grub_strcasecmp (value, "ECC_NIST_P384") == 0)
    -+    {
    -+      srk_type->type = TPM_ALG_ECC;
    -+      srk_type->nameAlg = TPM_ALG_SHA384;
    -+      srk_type->aes_bits = 256;
    -+      srk_type->detail.ecc_curve = TPM_ECC_NIST_P384;
    -+    }
    -+  else if (grub_strcasecmp (value, "ECC_NIST_P521") == 0)
    -+    {
    -+      srk_type->type = TPM_ALG_ECC;
    -+      srk_type->nameAlg = TPM_ALG_SHA512;
    -+      srk_type->aes_bits = 256;
    -+      srk_type->detail.ecc_curve = TPM_ECC_NIST_P521;
    -+    }
    -+  else if (grub_strcasecmp (value, "ECC_SM2_P256") == 0)
    -+    {
    -+      srk_type->type = TPM_ALG_ECC;
    -+      srk_type->nameAlg = TPM_ALG_SM3_256;
    -+      srk_type->aes_bits = 128;
    -+      srk_type->detail.ecc_curve = TPM_ECC_SM2_P256;
    -+    }
     +  else
     +    return grub_error (GRUB_ERR_OUT_OF_RANGE,
     +		       N_("Value '%s' is not a valid asymmetric key type"),
    @@ grub-core/tpm2/module.c (new)
     +      .arg      = NULL,
     +      .type     = ARG_TYPE_STRING,
     +      .doc      =
    -+	N_("In SRK mode, the type of SRK: RSA (RSA2048), RSA3072, "
    -+	   "RSA4096, ECC (ECC_NIST_P256), ECC_NIST_P384, "
    -+	   "ECC_NIST_P521, and ECC_SM2_P256. (default: ECC)"),
    ++	N_("In SRK mode, the type of SRK: RSA (RSA2048) and ECC (ECC_NIST_P256)"
    ++	   "(default: ECC)"),
     +    },
     +    /* NV Index-mode options */
     +    {
    @@ grub-core/tpm2/module.c (new)
     +					   grub_size_t sealed_key_size,
     +					   tpm2key_policy_t *policy_seq,
     +					   tpm2key_authpolicy_t *authpol_seq,
    ++					   grub_uint8_t *rsaparent,
     +					   grub_uint32_t *parent,
     +					   TPM2_SEALED_KEY *sk)
     +{
     +  asn1_node tpm2key = NULL;
    ++  grub_uint8_t rsaparent_tmp;
     +  grub_uint32_t parent_tmp;
     +  void *sealed_pub = NULL;
     +  grub_size_t sealed_pub_size;
    @@ grub-core/tpm2/module.c (new)
     +   *     policy      [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL,
     +   *     secret      [2] EXPLICIT OCTET STRING OPTIONAL,
     +   *     authPolicy  [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL,
    ++   *     description [4] EXPLICIT UTF8String OPTIONAL,
    ++   *     rsaParent   [5] EXPLICIT BOOLEAN OPTIONAL,
     +   *     parent      INTEGER,
     +   *     pubkey      OCTET STRING,
     +   *     privkey     OCTET STRING
    @@ grub-core/tpm2/module.c (new)
     +  if (err != GRUB_ERR_NONE)
     +    goto error;
     +
    ++  /* Retrieve rsaParent */
    ++  err = grub_tpm2key_get_rsaparent (tpm2key, &rsaparent_tmp);
    ++  if (err != GRUB_ERR_NONE)
    ++    goto error;
    ++
    ++  *rsaparent = rsaparent_tmp;
    ++
     +  /* Retrieve the parent handle */
     +  err = grub_tpm2key_get_parent (tpm2key, &parent_tmp);
     +  if (err != GRUB_ERR_NONE)
    @@ grub-core/tpm2/module.c (new)
     +  TPM_HANDLE tmp_handle = 0;
     +
     +  inPublic.publicArea.type = srk_type.type;
    -+  inPublic.publicArea.nameAlg = srk_type.nameAlg;
    ++  inPublic.publicArea.nameAlg = TPM_ALG_SHA256;
     +  inPublic.publicArea.objectAttributes.restricted = 1;
     +  inPublic.publicArea.objectAttributes.userWithAuth = 1;
     +  inPublic.publicArea.objectAttributes.decrypt = 1;
    @@ grub-core/tpm2/module.c (new)
     +  if (srk_type.type == TPM_ALG_RSA)
     +    {
     +      inPublic.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
    -+      inPublic.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = srk_type.aes_bits;
    ++      inPublic.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
     +      inPublic.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_CFB;
     +      inPublic.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
     +      inPublic.publicArea.parameters.rsaDetail.keyBits = srk_type.detail.rsa_bits;
    @@ grub-core/tpm2/module.c (new)
     +  else if (srk_type.type == TPM_ALG_ECC)
     +    {
     +      inPublic.publicArea.parameters.eccDetail.symmetric.algorithm = TPM_ALG_AES;
    -+      inPublic.publicArea.parameters.eccDetail.symmetric.keyBits.aes = srk_type.aes_bits;
    ++      inPublic.publicArea.parameters.eccDetail.symmetric.keyBits.aes = 128;
     +      inPublic.publicArea.parameters.eccDetail.symmetric.mode.aes = TPM_ALG_CFB;
     +      inPublic.publicArea.parameters.eccDetail.scheme.scheme = TPM_ALG_NULL;
     +      inPublic.publicArea.parameters.eccDetail.curveID = srk_type.detail.ecc_curve;
    @@ grub-core/tpm2/module.c (new)
     +	{
     +	  case 2048:
     +	    return "RSA2048";
    -+	  case 3072:
    -+	    return "RSA3072";
     +	}
     +   }
     +
    @@ grub-core/tpm2/module.c (new)
     +  grub_srk_type_t fallback_srks[] = {
     +    {
     +      .type = TPM_ALG_ECC,
    -+      .nameAlg = TPM_ALG_SHA256,
    -+      .aes_bits = 128,
     +      .detail.ecc_curve = TPM_ECC_NIST_P256,
     +    },
     +    {
     +      .type = TPM_ALG_RSA,
    -+      .nameAlg = TPM_ALG_SHA256,
    -+      .aes_bits = 128,
     +      .detail.rsa_bits = 2048,
     +    },
     +    {
    -+      .type = TPM_ALG_RSA,
    -+      .nameAlg = TPM_ALG_SHA384,
    -+      .aes_bits = 256,
    -+      .detail.rsa_bits = 3072,
    -+    },
    -+    {
     +      .type = TPM_ALG_ERROR,
     +    }
     +  };
    @@ grub-core/tpm2/module.c (new)
     +  TPM2_SEALED_KEY sealed_key = { 0 };
     +  void *file_bytes = NULL;
     +  grub_size_t file_size = 0;
    ++  grub_uint8_t rsaparent = 0;
     +  TPM_HANDLE parent_handle = 0;
     +  TPM_HANDLE srk_handle = 0;
     +  TPM_HANDLE sealed_handle = 0;
    @@ grub-core/tpm2/module.c (new)
     +						       file_size,
     +						       &policy_seq,
     +						       &authpol_seq,
    ++						       &rsaparent,
     +						       &parent_handle,
     +						       &sealed_key);
     +      if (err != GRUB_ERR_NONE)
     +	goto exit1;
    ++
    ++      if (rsaparent == 1)
    ++	{
    ++	  struct grub_tpm2_protector_context *ctx_w;
    ++
    ++	  /* Overwrite the SRK type as noted in the key */
    ++	  ctx_w = (struct grub_tpm2_protector_context *)ctx;
    ++	  ctx_w->srk_type.type = TPM_ALG_RSA;
    ++	  ctx_w->srk_type.detail.rsa_bits = 2048;
    ++	}
     +    }
     +  else
     +    {
    @@ grub-core/tpm2/module.c (new)
     +    ctx->mode = GRUB_TPM2_PROTECTOR_MODE_SRK;
     +
     +  /* Checks for SRK mode */
    -+  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK && ctx->keyfile == NULL
    -+      && ctx->tpm2key == NULL)
    ++  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK &&
    ++      (ctx->keyfile == NULL && ctx->tpm2key == NULL))
     +    return grub_error (GRUB_ERR_BAD_ARGUMENT,
     +		       N_("In SRK mode, a key file must be specified: "
     +			  "--tpm2key/-T or --keyfile/-k"));
     +
    -+  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK && ctx->keyfile
    -+      && ctx->tpm2key)
    ++  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK &&
    ++      (ctx->keyfile != NULL && ctx->tpm2key != NULL))
     +    return grub_error (GRUB_ERR_BAD_ARGUMENT,
     +		       N_("In SRK mode, please specify a key file with "
     +			  "only --tpm2key/-T or --keyfile/-k"));
     +
    -+  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK && ctx->nv)
    ++  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK && ctx->nv != 0)
     +    return grub_error (GRUB_ERR_BAD_ARGUMENT,
     +		       N_("In SRK mode, an NV Index cannot be specified"));
     +
    @@ grub-core/tpm2/module.c (new)
     +			   "--nvindex or -n"));
     +
     +  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV &&
    -+      (ctx->tpm2key || ctx->keyfile))
    ++      (ctx->tpm2key != NULL || ctx->keyfile != NULL))
     +    return grub_error (GRUB_ERR_BAD_ARGUMENT,
     +		       N_("In NV Index mode, a keyfile cannot be specified"));
     +
    -+  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV && ctx->srk)
    ++  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV && ctx->srk != 0)
     +    return grub_error (GRUB_ERR_BAD_ARGUMENT,
     +		       N_("In NV Index mode, an SRK cannot be specified"));
     +
    -+  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV && ctx->srk_type.type)
    ++  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_NV &&
    ++      ctx->srk_type.type != TPM_ALG_ERROR)
     +    return grub_error (GRUB_ERR_BAD_ARGUMENT,
     +		       N_("In NV Index mode, an asymmetric key type cannot be "
     +			  "specified"));
    @@ grub-core/tpm2/module.c (new)
     +      ctx->pcr_count = 1;
     +    }
     +
    -+  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK)
    ++  if (ctx->mode == GRUB_TPM2_PROTECTOR_MODE_SRK &&
    ++      ctx->srk_type.type == TPM_ALG_ERROR)
     +    {
    -+      if (ctx->srk_type.type == TPM_ALG_ERROR)
    -+        {
    -+	  ctx->srk_type.type = TPM_ALG_ECC;
    -+	  ctx->srk_type.nameAlg = TPM_ALG_SHA256;
    -+	  ctx->srk_type.aes_bits = 128;
    -+	  ctx->srk_type.detail.ecc_curve = TPM_ECC_NIST_P256;
    -+        }
    ++      ctx->srk_type.type = TPM_ALG_ECC;
    ++      ctx->srk_type.detail.ecc_curve = TPM_ECC_NIST_P256;
     +    }
     +
     +  return GRUB_ERR_NONE;
    @@ grub-core/tpm2/tpm2key.asn (new)
     +    policy      [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL,
     +    secret      [2] EXPLICIT OCTET STRING OPTIONAL,
     +    authPolicy  [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL,
    ++    description [4] EXPLICIT UTF8String OPTIONAL,
    ++    rsaParent   [5] EXPLICIT BOOLEAN OPTIONAL,
     +    parent      INTEGER,
     +    pubkey      OCTET STRING,
     +    privkey     OCTET STRING
    @@ grub-core/tpm2/tpm2key.c (new)
     +  grub_err_t err;
     +
     +  /*
    -+    TPMKey ::= SEQUENCE {
    -+        type        OBJECT IDENTIFIER,
    -+        emptyAuth   [0] EXPLICIT BOOLEAN OPTIONAL,
    -+        policy      [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL,
    -+        secret      [2] EXPLICIT OCTET STRING OPTIONAL,
    -+        authPolicy  [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL,
    -+        parent      INTEGER,
    -+        pubkey      OCTET STRING,
    -+        privkey     OCTET STRING
    -+    }
    -+  */
    ++   * TPMKey ::= SEQUENCE {
    ++   *    type        OBJECT IDENTIFIER,
    ++   *    emptyAuth   [0] EXPLICIT BOOLEAN OPTIONAL,
    ++   *    policy      [1] EXPLICIT SEQUENCE OF TPMPolicy OPTIONAL,
    ++   *    secret      [2] EXPLICIT OCTET STRING OPTIONAL,
    ++   *    authPolicy  [3] EXPLICIT SEQUENCE OF TPMAuthPolicy OPTIONAL,
    ++   *    description [4] EXPLICIT UTF8String OPTIONAL,
    ++   *    rsaParent   [5] EXPLICIT BOOLEAN OPTIONAL,
    ++   *    parent      INTEGER,
    ++   *    pubkey      OCTET STRING,
    ++   *    privkey     OCTET STRING
    ++   * }
    ++   */
     +  ret = asn1_array2tree (tpm2key_asn1_tab, &tpm2key_asn1, NULL);
     +  if (ret != ASN1_SUCCESS)
     +    return grub_error (GRUB_ERR_BAD_ARGUMENT,
    @@ grub-core/tpm2/tpm2key.c (new)
     +}
     +
     +grub_err_t
    ++grub_tpm2key_get_rsaparent (asn1_node tpm2key, grub_uint8_t *rsaparent)
    ++{
    ++  void *bool_str = NULL;
    ++  grub_size_t bool_str_size = 0;
    ++  int ret;
    ++
    ++  if (rsaparent == NULL)
    ++    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("NULL pointer detected"));
    ++
    ++  if (tpm2key == NULL)
    ++    return grub_error (GRUB_ERR_READ_ERROR, N_("Invalid parent node"));
    ++
    ++  ret = asn1_allocate_and_read (tpm2key, "rsaParent", &bool_str, &bool_str_size);
    ++  if (ret == ASN1_SUCCESS)
    ++    {
    ++      if (grub_strncmp ("TRUE", bool_str, bool_str_size) == 0)
    ++	*rsaparent = 1;
    ++      else
    ++	*rsaparent = 0;
    ++    }
    ++  else if (ret == ASN1_ELEMENT_NOT_FOUND)
    ++    *rsaparent = 0;
    ++  else
    ++    return grub_error (GRUB_ERR_READ_ERROR, N_("Failed to retrieve rsaParent"));
    ++
    ++  grub_free (bool_str);
    ++
    ++  return GRUB_ERR_NONE;
    ++}
    ++
    ++grub_err_t
     +grub_tpm2key_get_parent (asn1_node tpm2key, grub_uint32_t *parent)
     +{
     +  int ret;
    @@ grub-core/tpm2/tpm2key_asn1_tab.c (new)
     +  { "authPolicy", 1610637323, NULL },
     +  { NULL, 1073743880, "3"},
     +  { NULL, 2, "TPMAuthPolicy"},
    ++  { "description", 1610637346, NULL },
    ++  { NULL, 2056, "4"},
    ++  { "rsaParent", 1610637316, NULL },
    ++  { NULL, 2056, "5"},
     +  { "parent", 1073741827, NULL },
     +  { "pubkey", 1073741831, NULL },
     +  { "privkey", 7, NULL },
    @@ include/grub/tpm2/internal/args.h (new)
     +struct grub_srk_type
     +{
     +  TPMI_ALG_PUBLIC type;
    -+  TPMI_ALG_HASH nameAlg;
    -+  TPM_KEY_BITS aes_bits;
     +  union {
     +    TPM_KEY_BITS rsa_bits;
     +    TPM_ECC_CURVE ecc_curve;
    @@ include/grub/tpm2/tpm2key.h (new)
     +grub_tpm2key_end_parsing (asn1_node tpm2key);
     +
     +grub_err_t
    ++grub_tpm2key_get_rsaparent (asn1_node tpm2key, grub_uint8_t *rsaparent);
    ++
    ++grub_err_t
     +grub_tpm2key_get_parent (asn1_node tpm2key, grub_uint32_t *parent);
     +
     +grub_err_t
 3:  7e1d2591d = 12:  e3354505d cryptodisk: Support key protectors
 4:  4fe88cae7 ! 13:  9d5da3317 util/grub-protect: Add new tool
    @@ util/grub-protect.c (new)
     +      .arg   = "TYPE",
     +      .flags = 0,
     +      .doc   =
    -+	N_("The type of SRK: RSA (RSA2048), RSA3072, RSA4096, "
    -+	   "ECC (ECC_NIST_P256), ECC_NIST_P384, ECC_NIST_P521, "
    -+	   "and ECC_SM2_P256. (default: ECC)"),
    ++	N_("The type of SRK: RSA (RSA2048) and ECC (ECC_NIST_P256)."
    ++	   "(default: ECC)"),
     +      .group = 0
     +    },
     +    {
    @@ util/grub-protect.c (new)
     +  /* Create SRK */
     +  authCommand.sessionHandle = TPM_RS_PW;
     +  inPublic.publicArea.type = args->srk_type.type;
    -+  inPublic.publicArea.nameAlg  = args->srk_type.nameAlg;
    ++  inPublic.publicArea.nameAlg = TPM_ALG_SHA256;
     +  inPublic.publicArea.objectAttributes.restricted = 1;
     +  inPublic.publicArea.objectAttributes.userWithAuth = 1;
     +  inPublic.publicArea.objectAttributes.decrypt = 1;
    @@ util/grub-protect.c (new)
     +    {
     +    case TPM_ALG_RSA:
     +      inPublic.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM_ALG_AES;
    -+      inPublic.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = args->srk_type.aes_bits;
    ++      inPublic.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
     +      inPublic.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM_ALG_CFB;
     +      inPublic.publicArea.parameters.rsaDetail.scheme.scheme = TPM_ALG_NULL;
     +      inPublic.publicArea.parameters.rsaDetail.keyBits = args->srk_type.detail.rsa_bits;
    @@ util/grub-protect.c (new)
     +
     +    case TPM_ALG_ECC:
     +      inPublic.publicArea.parameters.eccDetail.symmetric.algorithm = TPM_ALG_AES;
    -+      inPublic.publicArea.parameters.eccDetail.symmetric.keyBits.aes = args->srk_type.aes_bits;
    ++      inPublic.publicArea.parameters.eccDetail.symmetric.keyBits.aes = 128;
     +      inPublic.publicArea.parameters.eccDetail.symmetric.mode.aes = TPM_ALG_CFB;
     +      inPublic.publicArea.parameters.eccDetail.scheme.scheme = TPM_ALG_NULL;
     +      inPublic.publicArea.parameters.eccDetail.curveID = args->srk_type.detail.ecc_curve;
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "type", sealed_key_oid, 1);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'type': 0x%u\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "emptyAuth", "TRUE", 1);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'emptyAuth': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "policy", "NEW", 1);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'policy': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +			  sizeof (cmd_code));
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'policy CommandCode': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +			  pol_buf.size);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'policy CommandPolicy': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "secret", NULL, 0);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to remove 'secret': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "authPolicy", NULL, 0);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to remove 'authPolicy': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
     +
    -+  /* Use the SRK handle as the parent handle if specified
    -+     Otherwise, Use TPM_RH_OWNER as the default parent handle */
    ++  /* Remove 'description' */
    ++  ret = asn1_write_value (tpm2key, "description", NULL, 0);
    ++  if (ret != ASN1_SUCCESS)
    ++    {
    ++      fprintf (stderr, _("Failed to remove 'description': 0x%x\n"), ret);
    ++      err = GRUB_ERR_BAD_ARGUMENT;
    ++      goto error;
    ++    }
    ++
    ++  /*
    ++   *  Use the SRK handle as the parent handle if specified
    ++   *  Otherwise, Use TPM_RH_OWNER as the default parent handle
    ++  */
     +  if (args->tpm2_srk != 0)
     +    parent = grub_cpu_to_be32 (args->tpm2_srk);
     +  else
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "parent", &parent, sizeof (parent));
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'parent': 0x%x\n"), ret);
    ++      err = GRUB_ERR_BAD_ARGUMENT;
    ++      goto error;
    ++    }
    ++
    ++  /*
    ++   * Set 'rsaParent' to TRUE if the RSA SRK is specified and the SRK
    ++   * handle is not persistent. Otherwise, remove 'rsaParent'.
    ++   */
    ++  if (args->tpm2_srk == 0 && args->srk_type.type == TPM_ALG_RSA)
    ++    ret = asn1_write_value (tpm2key, "rsaParent", "TRUE", 1);
    ++  else
    ++    ret = asn1_write_value (tpm2key, "rsaParent", NULL, 0);
    ++
    ++  if (ret != ASN1_SUCCESS)
    ++    {
    ++      fprintf (stderr, _("Failed to set 'rsaParent': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "pubkey", pub_buf.data, pub_buf.size);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'pubkey': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  ret = asn1_write_value (tpm2key, "privkey", priv_buf.data, priv_buf.size);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, _("Failed to set 'privkey': 0x%x\n"), ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  /* Create the DER binary */
     +  der_buf_size = 0;
     +  ret = asn1_der_coding (tpm2key, "", NULL, &der_buf_size, NULL);
    ++  if (ret != ASN1_MEM_ERROR)
    ++    {
    ++      fprintf (stderr, _("Failed to get DER size: 0x%x\n"), ret);
    ++      err = GRUB_ERR_BAD_ARGUMENT;
    ++      goto error;
    ++    }
     +
     +  der_buf = grub_malloc (der_buf_size);
     +  if (der_buf == NULL)
     +    {
    ++      fprintf (stderr, _("Failed to allocate memory for DER encoding\n"));
     +      err = GRUB_ERR_OUT_OF_MEMORY;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +  ret = asn1_der_coding (tpm2key, "", der_buf, &der_buf_size, NULL);
     +  if (ret != ASN1_SUCCESS)
     +    {
    ++      fprintf (stderr, "DER coding error: 0x%x\n", ret);
     +      err = GRUB_ERR_BAD_ARGUMENT;
     +      goto error;
     +    }
    @@ util/grub-protect.c (new)
     +      if (args->srk_type.type == TPM_ALG_ERROR)
     +	{
     +	  args->srk_type.type = TPM_ALG_ECC;
    -+	  args->srk_type.nameAlg = TPM_ALG_SHA256;
    -+	  args->srk_type.aes_bits = 128;
     +	  args->srk_type.detail.ecc_curve = TPM_ECC_NIST_P256;
     +	}
     +
 5:  0593d5901 = 14:  3207f30a9 tpm2: Support authorized policy
 6:  8ef188c56 = 15:  5f22e6397 tpm2: Implement NV index
 7:  462ad79ed ! 16:  64c386176 cryptodisk: Fallback to passphrase
    @@ Commit message
         `grub_errno` is now set. Print the existing errors out first, before
         proceeding with the passphrase.
     
    -    Cc: Stefan Berger <stefanb@linux.ibm.com>
         Signed-off-by: Patrick Colp <patrick.colp@oracle.com>
         Signed-off-by: Gary Lin <glin@suse.com>
    +    Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
     
      ## grub-core/disk/cryptodisk.c ##
     @@ grub-core/disk/cryptodisk.c: grub_cryptodisk_scan_device_real (const char *name,
 8:  1f78c792c = 17:  c65391cc2 cryptodisk: wipe out the cached keys from protectors
 9:  2952ceced ! 18:  d285e9dc1 diskfilter: look up cryptodisk devices first
    @@ Commit message
     
         Since the disk search order is based on the order of module loading, the
         attacker could insert a malicious disk with the same FS-UUID root to
    -    trick grub2 to boot into th malicious root and further dump memory to
    +    trick grub2 to boot into the malicious root and further dump memory to
         steal the unsealed key.
     
    -    To defend such attack, we can specify the hint provided by 'grub-probe'
    -    to search the encrypted partition first:
    +    Do defend against such an attack, we can specify the hint provided by
    +    'grub-probe' to search the encrypted partition first:
     
         search --fs-uuid --set=root --hint='cryptouuid/<PART-UUID>' <FS-UUID>
     
    -    However, for LVM on a encrypted partition, the search hint provided by
    +    However, for LVM on an encrypted partition, the search hint provided by
         'grub-probe' is:
     
           --hint='lvmid/<VG-UUID>/<LV-UUID>'
    @@ Commit message
         partition, so the attacker may have the chance to fool grub2 to boot
         into the malicious disk.
     
    -    To mininize the attack surface, this commit tweaks the disk device search
    +    To minimize the attack surface, this commit tweaks the disk device search
         in diskfilter to look up cryptodisk devices first and then others, so
         that the auto-unlocked disk will be found first, not the attacker's disk.
     
    -    Signed-off-by: Gary Lin <glin@suse.com>
         Cc: Fabian Vogt <fvogt@suse.com>
    +    Signed-off-by: Gary Lin <glin@suse.com>
    +    Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
     
      ## grub-core/disk/diskfilter.c ##
     @@ grub-core/disk/diskfilter.c: scan_devices (const char *arname)
10:  add1027ed = 19:  669b56fb3 tpm2: Enable tpm2 module for grub-emu
11:  a149dab00 ! 20:  0a2d6d64c tests: Add tpm2_test
    @@ tests/tpm2_test.in (new)
     +
     +mkdir -p ${tpm2statedir}
     +
    -+# Create the swtpm chardev instannce
    ++# Create the swtpm chardev instance
     +swtpm chardev --vtpm-proxy --tpmstate dir=${tpm2statedir} \
     +	--tpm2 --ctrl type=unixio,path=${tpm2ctrl} \
     +	--flags startup-clear --daemon > ${tpm2log}
    @@ tests/tpm2_test.in (new)
     +    exit $ret
     +fi
     +
    -+tpm2dev=$(grep "New TPM device" ${tpm2log} | cut -d' ' -f 4)
    -+if [ -z "${tpm2dev}" ]; then
    -+    exit QUIT
    -+fi
    -+
     +# Wait for tpm2 chardev
     +wait=3
     +while [ "${wait}" -gt 0 ]; do
    -+    if [ -c "${tpm2dev}" ]; then
    -+        break;
    ++    tpm2dev=$(grep "New TPM device" ${tpm2log} | cut -d' ' -f 4)
    ++    if [ -n "${tpm2dev}" ] && [ -c "${tpm2dev}" ]; then
    ++        break
     +    fi
     +    sleep 1
     +    ((wait--))
     +done
     +if [ "$wait" -le 0 ]; then
    ++    echo "TPM device did not appear."
     +    exit QUIT
     +fi
     +
    @@ tests/tpm2_test.in (new)
     +    ret=$?
     +    if [ "$ret" -ne 0 ]; then
     +	echo "Failed to seal the secret key"
    -+	return 1
    ++	exit 1
     +    fi
     +
     +    # Flip the asymmetric algorithm in grub.cfg to trigger fallback SRKs
-- 
2.35.3


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

             reply	other threads:[~2024-04-25  8:02 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-25  8:01 Gary Lin via Grub-devel [this message]
2024-04-25  8:01 ` [PATCH v13 01/20] posix_wrap: tweaks in preparation for libtasn1 Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 02/20] libtasn1: import libtasn1-4.19.0 Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 03/20] libtasn1: disable code not needed in grub Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 04/20] libtasn1: changes for grub compatibility Gary Lin via Grub-devel
2024-04-30 13:14   ` Stefan Berger
2024-04-25  8:01 ` [PATCH v13 05/20] libtasn1: fix the potential buffer overrun Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 06/20] libtasn1: compile into asn1 module Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 07/20] asn1_test: test module for libtasn1 Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 08/20] libtasn1: Add the documentation Gary Lin via Grub-devel
2024-04-27  8:27   ` Glenn Washburn
2024-04-29  6:21     ` Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 09/20] key_protector: Add key protectors framework Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 10/20] tpm2: Add TPM Software Stack (TSS) Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 11/20] key_protector: Add TPM2 Key Protector Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 12/20] cryptodisk: Support key protectors Gary Lin via Grub-devel
2024-04-25  8:01 ` [PATCH v13 13/20] util/grub-protect: Add new tool Gary Lin via Grub-devel
2024-04-25  8:02 ` [PATCH v13 14/20] tpm2: Support authorized policy Gary Lin via Grub-devel
2024-04-25  8:02 ` [PATCH v13 15/20] tpm2: Implement NV index Gary Lin via Grub-devel
2024-04-25  8:02 ` [PATCH v13 16/20] cryptodisk: Fallback to passphrase Gary Lin via Grub-devel
2024-04-25  8:02 ` [PATCH v13 17/20] cryptodisk: wipe out the cached keys from protectors Gary Lin via Grub-devel
2024-04-25  8:02 ` [PATCH v13 18/20] diskfilter: look up cryptodisk devices first Gary Lin via Grub-devel
2024-04-25  8:02 ` [PATCH v13 19/20] tpm2: Enable tpm2 module for grub-emu Gary Lin via Grub-devel
2024-04-25  8:02 ` [PATCH v13 20/20] tests: Add tpm2_test Gary Lin via Grub-devel
2024-04-26 22:18   ` Glenn Washburn
2024-04-29  8:10     ` Gary Lin via Grub-devel
2024-04-30  8:09       ` Gary Lin via Grub-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240425080206.23902-1-glin@suse.com \
    --to=grub-devel@gnu.org \
    --cc=christopher.co@microsoft.com \
    --cc=daniel.kiper@oracle.com \
    --cc=daniel.mihai@microsoft.com \
    --cc=development@efficientek.com \
    --cc=dja@axtens.net \
    --cc=glin@suse.com \
    --cc=hegatta@linux.microsoft.com \
    --cc=jaredz@redhat.com \
    --cc=jaskaran.khurana@microsoft.com \
    --cc=jejb@linux.ibm.com \
    --cc=mchang@suse.com \
    --cc=patrick.colp@oracle.com \
    --cc=shkhisti@microsoft.com \
    --cc=stefanb@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.