Automatic Disk Unlock with TPM2
v17 cover Automatic Disk Unlock with TPM2
GIT repo for v17: https://github.com/lcp/grub2/tree/tpm2-unlock-v17
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~4,6,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 5 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 <[email protected]>
- 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.tpm3. 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
v17:
- Fixing the missing space in strncat()
- Updating the steps to import libtasn1
- Moving libtasn1.h to grub-core/lib/libtasn1/ and fixing the affected patches
- libtasn1.h is included in tpm2key.h, so there is no need to include the header again in module.c and tpm2key.c.
- Applying the libtasn1 patches in a different way
- Instead of applying the patches directly in the grub2 source code, libtasn1 is copied to grub-core/lib/libtasn1-grub by autogen.sh and then the script applies the libtasn1 patches to libtasn1-grub.
- The patch files in patch 3, 4, and 5 are created by the following repo against the libtasn1-4.19.0-base tag.
https://github.com/lcp/grub2/tree/import-libtasn1-4.19.0
- Correcting the description of the tpm2_test commit to replace swtpm_cuse with "swtpm chardev"
v16:
- https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00093.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v16
- Rebasing patch 6, 12, and 16 to fix the conflicts with the latest master branch
- Changes in cryptodisk:
- Disallowing that both OPTION_KEYFILE and OPTION_PROTECTOR are set since the key data for "--key-file" would be overwritten by the key protectors
- Resetting the cargs key data when the key from a key protector doesn't work for the disk to ensure the passphrase prompt will be triggered later
- Adding the comment to address why grub_errno is only cleared for
cargs->key_len == 0
v15:
- https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00059.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v15
- Changes in tpm2_test
- Quoting the variables which contain file paths
- Correcting the exit code for several commands
- Writing the verification text directly into the LUKS device
- Amending the waiting loop for swtpm
- Replacing exit with return in tpm2_seal_unseal() and
tpm2_seal_unseal_nv()
- Collecting the parameters for the SRK mode testcases in an array and invoking tpm2_seal_unseal() with a for loop
- Moving the tpm2-tools commands for the NV index mode to a separate function
- Using tpm2_evictcontrol to remove the object from the NV index to match the key sealing commands
- Printing the test results
- Printing error messages to stderr
v14:
- https://lists.gnu.org/archive/html/grub-devel/2024-05/msg00011.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-v14
- Addressing the libtasn1 patches more in the document
- Various improvements in tpm2_test
- Verifying the test inside the LUKS device
- Improving the return status checks and the waiting loop for swtpm
- Fixing the portability issues
- Making all variables braced
- Renaming grub-emu-opts to --emu-opts (grub-shell)
v13:
- https://lists.gnu.org/archive/html/grub-devel/2024-04/msg00155.html
- GIT repo: https://github.com/lcp/grub2/tree/tpm2-unlock-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 (4):
posix_wrap: tweaks in preparation for libtasn1
libtasn1: import libtasn1-4.19.0
libtasn1: compile into asn1 module
asn1_test: test module for libtasn1
Gary Lin (9):
libtasn1: disable code not needed in grub
libtasn1: changes for grub compatibility
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 + autogen.sh | 16 + configure.ac | 30 + docs/grub-dev.texi | 33 + docs/man/grub-protect.h2m | 4 + grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 50 + grub-core/disk/cryptodisk.c | 262 +- 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 | 125 + ...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 | 1425 ++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2501 +++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1109 ++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 100 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ 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 | 1225 ++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + grub-core/lib/libtasn1/libtasn1.h | 643 +++++ 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 | 1324 +++++++++ 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 | 475 ++++ 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/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 | 389 +++ tests/util/grub-shell.in | 6 +- util/grub-protect.c | 1420 ++++++++++ 76 files changed, 19746 insertions(+), 75 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/libtasn1.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/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 v16:
1: bc0e42183 ! 1: 834e3b578 posix_wrap: tweaks in preparation for libtasn1
@@ Commit message
+ Reviewed-by: Daniel Kiper <[email protected]>
## grub-core/lib/posix_wrap/c-ctype.h (new) ##
@@
@@ grub-core/lib/posix_wrap/string.h: memchr (const void *s, int c, grub_size_t n)
}
+static inline char *
-+strncat(char *dest, const char *src, grub_size_t n) ++strncat (char *dest, const char *src, grub_size_t n)
+{
+ const char *end;
+ char *str = dest;
2: 6824619ec ! 2: cdf54d25a libtasn1: import libtasn1-4.19.0
@@ Commit message
Import a very trimmed-down set of libtasn1 files:
- pushd /tmp
- wget https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
- tar -xf libtasn1-4.19.0.tar.gz
- popd
- pushd grub-core/lib
- rm -rf libtasn1
- mkdir libtasn1
- cp /tmp/libtasn1-4.19.0/{README.md,COPYING} libtasn1/
- mkdir libtasn1/lib
- cp /tmp/libtasn1-4.19.0/lib/{coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h} libtasn1/lib
- cp /tmp/libtasn1-4.19.0/lib/includes/libtasn1.h ../../include/grub/
- git add libtasn1/ ../../include/grub/libtasn1.h
- popd
+ curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
+ tar xf libtasn1-4.19.0.tar.gz
+ rm -rf grub-core/lib/libtasn1/
+ mkdir -p grub-core/lib/libtasn1/lib/
+ cp libtasn1-4.19.0/{README.md,COPYING} grub-core/lib/libtasn1/
+ cp libtasn1-4.19.0/lib/{coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h} grub-core/libtasn1/lib/
+ cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1/
+ rm -rf libtasn1-4.19.0*
@@ grub-core/lib/libtasn1/lib/structure.h (new)
+
+#endif
- ## include/grub/libtasn1.h (new) ##
+ ## grub-core/lib/libtasn1/libtasn1.h (new) ## @@
+/*
+ * Copyright (C) 2002-2022 Free Software Foundation, Inc.
3: c49a0287a < -: --------- libtasn1: disable code not needed in grub
4: 068b97b14 < -: --------- libtasn1: changes for grub compatibility
5: 0fdb7ce8c < -: --------- libtasn1: fix the potential buffer overrun
8: 762704b8b ! 3: 4055c72d0 libtasn1: Add the documentation
@@ Metadata
Author: Gary Lin <[email protected]>
## Commit message ##
- libtasn1: Add the documentation
+ libtasn1: disable code not needed in grub
- Document libtasn1 in docs/grub-dev.texi and add the upgrade steps.
- Also add the patches to make libtasn1 compatible with grub code.
+ Based on the patch from "Daniel Axtens <[email protected]>"
- Signed-off-by: Gary Lin <[email protected]>
- Reviewed-by: Vladimir Serbinenko <[email protected]>
+ We don't expect to be able to write ASN.1, only read it,
+ so we can disable some code.
- ## docs/grub-dev.texi ##
-@@ docs/grub-dev.texi: to update it.
- * Gnulib::
- * jsmn::
- * minilzo::
-+* libtasn1::
- @end menu
-
- @node Gnulib
-@@ docs/grub-dev.texi: cp minilzo-2.10/*.[hc] grub-core/lib/minilzo
- rm -r minilzo-2.10*
- @end example
-
-+@node libtasn1
-+@section libtasn1
-+
-+libtasn1 is a library providing Abstract Syntax Notation One (ASN.1, as
-+specified by the X.680 ITU-T recommendation) parsing and structures management,
-+and Distinguished Encoding Rules (DER, as per X.690) encoding and decoding
-+functions.
-+
-+To upgrade to a new version of the libtasn1 library, download the release
-+tarball and copy the files into the target directory:
-+
-+@example
-+curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
-+tar -zxf libtasn1-4.19.0.tar.gz
-+rm -r grub-core/lib/libtasn1/
-+mkdir libtasn1/lib
-+mkdir -p grub-core/lib/libtasn1/lib/
-+cp libtasn1-4.19.0/@lbracechar{}README.md,COPYING@rbracechar{} grub-core/lib/libtasn1/
-+cp libtasn1-4.19.0/lib/@lbracechar{}coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h@rbracechar{} grub-core/lib/libtasn1/lib/
-+cp libtasn1-4.19.0/lib/includes/libtasn1.h include/grub/
-+rm -rf libtasn1-4.19.0
-+@end example
-+
-+After upgrading the library, it may be necessary to apply the patches in
-+@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with
-+grub. These patches were needed to use the current version of libtasn1. The
-+existing patches may not apply cleanly, apply at all, or even be needed for a
-+newer version of the library, and other patches maybe needed due to changes in
-+the newer version. If existing patches need to be refreshed to apply cleanly,
-+please include updated patches as part of the a patch set sent to the list.
-+If new patches are needed or existing patches are not needed, also please send
-+additions or removals as part of any patch set upgrading libtasn1.
-+
- @node Debugging
- @chapter Debugging
-
+ Do that with #if 0/#endif, rather than deletion. This means
+ that the difference between upstream and grub is smaller,
+ which should make updating libtasn1 easier in the future.
+
+ With these exclusions we also avoid the need for minmax.h,
+ which is convenient because it means we don't have to
+ import it from gnulib.
+
+ Signed-off-by: Gary Lin <[email protected]>
## grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch (new) ##
@@
-+From 715f65934a120730316751536194ec5ed86aed9c Mon Sep 17 00:00:00 2001
++From f95a7b9c72fe91e44a90067c9008791a15e490c0 Mon Sep 17 00:00:00 2001
+From: Daniel Axtens <[email protected]>
+Date: Fri, 1 May 2020 17:12:23 +1000
+Subject: [PATCH 1/3] libtasn1: disable code not needed in grub
@@ grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.pat
+Signed-off-by: Daniel Axtens <[email protected]>
+Signed-off-by: Gary Lin <[email protected]>
+---
-+ grub-core/lib/libtasn1/lib/coding.c | 12 ++++++++++--
-+ grub-core/lib/libtasn1/lib/decoding.c | 2 ++
-+ grub-core/lib/libtasn1/lib/element.c | 6 +++---
-+ grub-core/lib/libtasn1/lib/errors.c | 3 +++
-+ grub-core/lib/libtasn1/lib/structure.c | 10 ++++++----
-+ include/grub/libtasn1.h | 15 +++++++++++++++
++ grub-core/lib/libtasn1-grub/lib/coding.c | 12 ++++++++++--
++ grub-core/lib/libtasn1-grub/lib/decoding.c | 2 ++
++ grub-core/lib/libtasn1-grub/lib/element.c | 6 +++---
++ grub-core/lib/libtasn1-grub/lib/errors.c | 3 +++
++ grub-core/lib/libtasn1-grub/lib/structure.c | 10 ++++++----
++ grub-core/lib/libtasn1-grub/libtasn1.h | 15 +++++++++++++++
+ 6 files changed, 39 insertions(+), 9 deletions(-)
+
-+diff --git a/grub-core/lib/libtasn1/lib/coding.c b/grub-core/lib/libtasn1/lib/coding.c
++diff --git a/grub-core/lib/libtasn1-grub/lib/coding.c b/grub-core/lib/libtasn1-grub/lib/coding.c
+index ea5bc370e..5d03bca9d 100644
-+--- a/grub-core/lib/libtasn1/lib/coding.c
-++++ b/grub-core/lib/libtasn1/lib/coding.c
++--- a/grub-core/lib/libtasn1-grub/lib/coding.c
+++++ b/grub-core/lib/libtasn1-grub/lib/coding.c
+@@ -30,11 +30,11 @@
+ #include "parser_aux.h"
+ #include <gstr.h>
@@ -1423,3 +1429,5 @@ error:
+ }
++
++#endif
-+diff --git a/grub-core/lib/libtasn1/lib/decoding.c b/grub-core/lib/libtasn1/lib/decoding.c
++diff --git a/grub-core/lib/libtasn1-grub/lib/decoding.c b/grub-core/lib/libtasn1-grub/lib/decoding.c
+index b9245c486..bf9cb13ac 100644
-+--- a/grub-core/lib/libtasn1/lib/decoding.c
-++++ b/grub-core/lib/libtasn1/lib/decoding.c
++--- a/grub-core/lib/libtasn1-grub/lib/decoding.c
+++++ b/grub-core/lib/libtasn1-grub/lib/decoding.c
+@@ -1620,6 +1620,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, int ider_len,
+ return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription);
+ }
@@ -1650,6 +1651,7 @@ asn1_der_decoding_element (asn1_node * structure, const char
+
+ /**
+ * asn1_der_decoding_startEnd:
-+diff --git a/grub-core/lib/libtasn1/lib/element.c b/grub-core/lib/libtasn1/lib/element.c
++diff --git a/grub-core/lib/libtasn1-grub/lib/element.c b/grub-core/lib/libtasn1-grub/lib/element.c
+index d4c558e10..bc4c3c8d7 100644
-+--- a/grub-core/lib/libtasn1/lib/element.c
-++++ b/grub-core/lib/libtasn1/lib/element.c
++--- a/grub-core/lib/libtasn1-grub/lib/element.c
+++++ b/grub-core/lib/libtasn1-grub/lib/element.c
+@@ -118,7 +118,7 @@ _asn1_convert_integer (const unsigned char *value, unsigned char *value_out,
+ value_out[k2 - k] = val[k2];
+ }
@@ -646,7 +646,7 @@ asn1_write_value (asn1_node node_root, const char *name,
+
+ #define PUT_VALUE( ptr, ptr_size, data, data_size) \
+ *len = data_size; \
-+diff --git a/grub-core/lib/libtasn1/lib/errors.c b/grub-core/lib/libtasn1/lib/errors.c
++diff --git a/grub-core/lib/libtasn1-grub/lib/errors.c b/grub-core/lib/libtasn1-grub/lib/errors.c
+index aef5dfe6f..2b2322152 100644
-+--- a/grub-core/lib/libtasn1/lib/errors.c
-++++ b/grub-core/lib/libtasn1/lib/errors.c
++--- a/grub-core/lib/libtasn1-grub/lib/errors.c
+++++ b/grub-core/lib/libtasn1-grub/lib/errors.c
+@@ -57,6 +57,8 @@ static const libtasn1_error_entry error_algorithms[] = {
+ {0, 0}
+ };
@@ -73,6 +75,7 @@ asn1_perror (int error)
+
+ /**
+ * asn1_strerror:
-+diff --git a/grub-core/lib/libtasn1/lib/structure.c b/grub-core/lib/libtasn1/lib/structure.c
++diff --git a/grub-core/lib/libtasn1-grub/lib/structure.c b/grub-core/lib/libtasn1-grub/lib/structure.c
+index 512dd601f..f5a947d57 100644
-+--- a/grub-core/lib/libtasn1/lib/structure.c
-++++ b/grub-core/lib/libtasn1/lib/structure.c
++--- a/grub-core/lib/libtasn1-grub/lib/structure.c
+++++ b/grub-core/lib/libtasn1-grub/lib/structure.c
+@@ -76,7 +76,7 @@ _asn1_find_left (asn1_node_const node)
+ return node->left;
+ }
@@ -1207,6 +1208,7 @@ asn1_copy_node (asn1_node dst, const char *dst_name,
+
+ /**
+ * asn1_dup_node:
-+diff --git a/include/grub/libtasn1.h b/include/grub/libtasn1.h
++diff --git a/grub-core/lib/libtasn1-grub/libtasn1.h b/grub-core/lib/libtasn1-grub/libtasn1.h
+index 51cc7879f..058ab27b0 100644
-+--- a/include/grub/libtasn1.h
-++++ b/include/grub/libtasn1.h
++--- a/grub-core/lib/libtasn1-grub/libtasn1.h
+++++ b/grub-core/lib/libtasn1-grub/libtasn1.h
+@@ -318,6 +318,8 @@ extern "C"
+ /* Functions definitions */
+ /***********************************/
@@ -411,12 +422,16 @@ extern "C"
+--
+2.35.3
+
-
- ## grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch (new) ##
-@@
-+From d13793c210999b256b46970b5f76d41a57e5c8fc Mon Sep 17 00:00:00 2001
-+From: Daniel Axtens <[email protected]>
-+Date: Fri, 1 May 2020 20:44:29 +1000
-+Subject: [PATCH 2/3] libtasn1: changes for grub compatibility
-+
-+Do a few things to make libtasn1 compile as part of grub:
-+
-+ - remove _asn1_strcat and replace strcat with the bound-checked
-+ _asn1_str_cat except the one inside _asn1_str_cat. That strcat is
-+ replaced with strcpy.
-+
-+ - adjust header paths in libtasn1.h
-+
-+ - adjust header paths to "grub/libtasn1.h".
-+
-+ - replace a 64 bit division with a call to grub_divmod64, preventing
-+ creation of __udivdi3 calls on 32 bit platforms.
-+
-+Cc: Vladimir Serbinenko <[email protected]>
-+Signed-off-by: Daniel Axtens <[email protected]>
-+Signed-off-by: Gary Lin <[email protected]>
-+---
-+ grub-core/lib/libtasn1/lib/decoding.c | 8 ++++----
-+ grub-core/lib/libtasn1/lib/element.c | 2 +-
-+ grub-core/lib/libtasn1/lib/gstr.c | 2 +-
-+ grub-core/lib/libtasn1/lib/int.h | 3 +--
-+ grub-core/lib/libtasn1/lib/parser_aux.c | 2 +-
-+ include/grub/libtasn1.h | 5 ++---
-+ 6 files changed, 10 insertions(+), 12 deletions(-)
-+
-+diff --git a/grub-core/lib/libtasn1/lib/decoding.c b/grub-core/lib/libtasn1/lib/decoding.c
-+index bf9cb13ac..51859fe36 100644
-+--- a/grub-core/lib/libtasn1/lib/decoding.c
-++++ b/grub-core/lib/libtasn1/lib/decoding.c
-+@@ -2016,8 +2016,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element,
-+ (p2->type & CONST_ASSIGN))
-+ {
-+ strcpy (name, definitions->name);
-+- strcat (name, ".");
-+- strcat (name, p2->name);
-++ _asn1_str_cat (name, sizeof (name), ".");
-++ _asn1_str_cat (name, sizeof (name), p2->name);
-+
-+ len = sizeof (value);
-+ result = asn1_read_value (definitions, name, value, &len);
-+@@ -2034,8 +2034,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element,
-+ if (p2)
-+ {
-+ strcpy (name, definitions->name);
-+- strcat (name, ".");
-+- strcat (name, p2->name);
-++ _asn1_str_cat (name, sizeof (name), ".");
-++ _asn1_str_cat (name, sizeof (name), p2->name);
-+
-+ result = asn1_create_element (definitions, name, &aux);
-+ if (result == ASN1_SUCCESS)
-+diff --git a/grub-core/lib/libtasn1/lib/element.c b/grub-core/lib/libtasn1/lib/element.c
-+index bc4c3c8d7..8694fecb9 100644
-+--- a/grub-core/lib/libtasn1/lib/element.c
-++++ b/grub-core/lib/libtasn1/lib/element.c
-+@@ -688,7 +688,7 @@ asn1_write_value (asn1_node node_root, const char *name,
-+ return ASN1_MEM_ERROR; \
-+ } else { \
-+ /* this strcat is checked */ \
-+- if (ptr) _asn1_strcat (ptr, data); \
-++ if (ptr) _asn1_str_cat ((char *)ptr, ptr_size, (const char *)data); \
-+ }
-+
-+ /**
-+diff --git a/grub-core/lib/libtasn1/lib/gstr.c b/grub-core/lib/libtasn1/lib/gstr.c
-+index eef419554..a9c16f5d3 100644
-+--- a/grub-core/lib/libtasn1/lib/gstr.c
-++++ b/grub-core/lib/libtasn1/lib/gstr.c
-+@@ -36,7 +36,7 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
-+
-+ if (dest_tot_size - dest_size > str_size)
-+ {
-+- strcat (dest, src);
-++ strcpy (dest + dest_size, src);
-+ }
-+ else
-+ {
-+diff --git a/grub-core/lib/libtasn1/lib/int.h b/grub-core/lib/libtasn1/lib/int.h
-+index d94d51c8c..7409c7655 100644
-+--- a/grub-core/lib/libtasn1/lib/int.h
-++++ b/grub-core/lib/libtasn1/lib/int.h
-+@@ -35,7 +35,7 @@
-+ # include <sys/types.h>
-+ # endif
-+
-+-# include <libtasn1.h>
-++# include "grub/libtasn1.h"
-+
-+ # define ASN1_SMALL_VALUE_SIZE 16
-+
-+@@ -115,7 +115,6 @@ extern const tag_and_class_st _asn1_tags[];
-+ # define _asn1_strtoul(n,e,b) strtoul((const char *) n, e, b)
-+ # define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b)
-+ # define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b)
-+-# define _asn1_strcat(a,b) strcat((char *)a, (const char *)b)
-+
-+ # if SIZEOF_UNSIGNED_LONG_INT == 8
-+ # define _asn1_strtou64(n,e,b) strtoul((const char *) n, e, b)
-+diff --git a/grub-core/lib/libtasn1/lib/parser_aux.c b/grub-core/lib/libtasn1/lib/parser_aux.c
-+index c05bd2339..e4e4c0556 100644
-+--- a/grub-core/lib/libtasn1/lib/parser_aux.c
-++++ b/grub-core/lib/libtasn1/lib/parser_aux.c
-+@@ -632,7 +632,7 @@ _asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE])
-+ count = 0;
-+ do
-+ {
-+- d = val / 10;
-++ d = grub_divmod64(val, 10, NULL);
-+ r = val - d * 10;
-+ temp[start + count] = '0' + (char) r;
-+ count++;
-+diff --git a/include/grub/libtasn1.h b/include/grub/libtasn1.h
-+index 058ab27b0..7d64b6ab7 100644
-+--- a/include/grub/libtasn1.h
-++++ b/include/grub/libtasn1.h
-+@@ -54,9 +54,8 @@
-+ # define __LIBTASN1_PURE__
-+ # endif
-+
-+-# include <sys/types.h>
-+-# include <time.h>
-+-# include <stdio.h> /* for FILE* */
-++# include <grub/types.h>
-++# include <grub/time.h>
-+
-+ # ifdef __cplusplus
-+ extern "C"
-+--
-+2.35.3
-+
-
- ## grub-core/lib/libtasn1-patches/0003-libtasn1-fix-the-potential-buffer-overrun.patch (new) ##
-@@
-+From 8f7c3c3b28a312f77499159c52f313487fba0d08 Mon Sep 17 00:00:00 2001
-+From: Gary Lin <[email protected]>
-+Date: Mon, 8 Apr 2024 14:57:21 +0800
-+Subject: [PATCH 3/3] libtasn1: fix the potential buffer overrun
-+
-+In _asn1_tag_der(), the first while loop for the long form may end up
-+with a 'k' value with 'ASN1_MAX_TAG_SIZE' and cause the buffer overrun
-+in the second while loop. This commit tweaks the conditional check to
-+avoid producing a too large 'k'.
-+
-+This is a quick fix and may differ from the official upstream fix.
-+
-+libtasn1 issue: https://gitlab.com/gnutls/libtasn1/-/issues/49
-+
-+Signed-off-by: Gary Lin <[email protected]>
-+---
-+ grub-core/lib/libtasn1/lib/coding.c | 2 +-
-+ 1 file changed, 1 insertion(+), 1 deletion(-)
-+
-+diff --git a/grub-core/lib/libtasn1/lib/coding.c b/grub-core/lib/libtasn1/lib/coding.c
-+index 5d03bca9d..0458829a5 100644
-+--- a/grub-core/lib/libtasn1/lib/coding.c
-++++ b/grub-core/lib/libtasn1/lib/coding.c
-+@@ -143,7 +143,7 @@ _asn1_tag_der (unsigned char class, unsigned int tag_value,
-+ temp[k++] = tag_value & 0x7F;
-+ tag_value >>= 7;
-+
-+- if (k > ASN1_MAX_TAG_SIZE - 1)
-++ if (k >= ASN1_MAX_TAG_SIZE - 1)
-+ break; /* will not encode larger tags */
-+ }
-+ *ans_len = k + 1;
-+--
-+2.35.3
-+
-: --------- > 4: 11cb1c021 libtasn1: changes for grub compatibility
-: --------- > 5: ff1aacb5e libtasn1: fix the potential buffer overrun
6: dc1e882a2 ! 6: 73dfd01bd libtasn1: compile into asn1 module
@@ Commit message
Signed-off-by: Gary Lin <[email protected]>
Reviewed-by: Vladimir Serbinenko <[email protected]>
+ ## autogen.sh ##
+@@ autogen.sh: for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul
+ cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x"
+ done
+
++echo "Importing libtasn1..."
++if [ -d grub-core/lib/libtasn1-grub ]; then
++ rm -rf grub-core/lib/libtasn1-grub
++fi
++
++mkdir -p grub-core/lib/libtasn1-grub/lib
++cp grub-core/lib/libtasn1/lib/*.[ch] grub-core/lib/libtasn1-grub/lib
++cp grub-core/lib/libtasn1/libtasn1.h grub-core/lib/libtasn1-grub/
++
++for patch in \
++ 0001-libtasn1-disable-code-not-needed-in-grub.patch \
++ 0002-libtasn1-changes-for-grub-compatibility.patch \
++ 0003-libtasn1-fix-the-potential-buffer-overrun.patch ; do
++ patch -p1 -i grub-core/lib/libtasn1-patches/$patch
++done
++
+ echo "Generating Automake input..."
+
+ # Automake doesn't like including files from a path outside the project.
+
## grub-core/Makefile.core.def ##
@@ grub-core/Makefile.core.def: module = {
enable = efi;
@@ grub-core/Makefile.core.def: module = {
+
+module = {
+ name = asn1;
-+ common = lib/libtasn1/lib/decoding.c;
-+ common = lib/libtasn1/lib/coding.c;
-+ common = lib/libtasn1/lib/element.c;
-+ common = lib/libtasn1/lib/structure.c;
-+ common = lib/libtasn1/lib/parser_aux.c;
-+ common = lib/libtasn1/lib/gstr.c;
-+ common = lib/libtasn1/lib/errors.c;
++ common = lib/libtasn1-grub/lib/decoding.c;
++ common = lib/libtasn1-grub/lib/coding.c;
++ common = lib/libtasn1-grub/lib/element.c;
++ common = lib/libtasn1-grub/lib/structure.c;
++ common = lib/libtasn1-grub/lib/parser_aux.c;
++ common = lib/libtasn1-grub/lib/gstr.c;
++ common = lib/libtasn1-grub/lib/errors.c;
+ common = lib/libtasn1_wrap/wrap.c;
+ cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)';
+ // -Wno-type-limits comes from libtasn1's configure.ac
-+ cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1/lib -Wno-type-limits';
++ cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits';
+};
## grub-core/lib/libtasn1_wrap/wrap.c (new) ##
7: 141893df5 ! 7: 6dbd9060f asn1_test: test module for libtasn1
@@ Makefile.util.def: script = {
## grub-core/Makefile.core.def ##
@@ grub-core/Makefile.core.def: module = {
// -Wno-type-limits comes from libtasn1's configure.ac
- cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1/lib -Wno-type-limits';
+ cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits';
};
+
+module = {
@@ grub-core/Makefile.core.def: module = {
+ common = tests/asn1/Test_simple.c;
+ common = tests/asn1/Test_strings.c;
+ common = tests/asn1/asn1_test.c;
++ cppflags = '-I$(srcdir)/lib/libtasn1-grub';
+};
## grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h (new) ##
@@ grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h (new)
+# include "config.h"
+#endif
+
-+#include <grub/libtasn1.h>
++#include <libtasn1.h>
+
+const asn1_static_node CVE_2018_1000654_1_asn1_tab[] = {
+ { "TEST_TREE", 536875024, NULL },
@@ grub-core/tests/asn1/CVE-2018-1000654-2_asn1_tab.h (new)
+# include "config.h"
+#endif
+
-+#include <grub/libtasn1.h>
++#include <libtasn1.h>
+
+const asn1_static_node CVE_2018_1000654_2_asn1_tab[] = {
+ { "TEST_TREE", 536875024, NULL },
@@ grub-core/tests/asn1/asn1_test.h (new)
+#ifndef LIBTASN1_WRAP_TESTS_H
+#define LIBTASN1_WRAP_TESTS_H
+
-+#include <grub/libtasn1.h>
++#include <libtasn1.h>
+#include <grub/err.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
-: --------- > 8: a0b370b0b libtasn1: Add the documentation
9: 7320efe13 = 9: 46f29445b key_protector: Add key protectors framework
10: d506f4baf = 10: ba02aa532 tpm2: Add TPM Software Stack (TSS)
11: 11a7fbdab ! 11: 324bc5b0b key_protector: Add TPM2 Key Protector
@@ grub-core/Makefile.core.def: module = {
+ common = tpm2/tpm2key_asn1_tab.c;
+ efi = tpm2/tcg2.c;
+ enable = efi;
++ cppflags = '-I$(srcdir)/lib/libtasn1-grub';
+};
+
module = {
@@ grub-core/tpm2/module.c (new)
+#include <grub/dl.h>
+#include <grub/extcmd.h>
+#include <grub/file.h>
-+#include <grub/libtasn1.h>
+#include <grub/list.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
@@ grub-core/tpm2/tpm2key.c (new)
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
+ */
+
-+#include <grub/libtasn1.h>
+#include <grub/list.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
@@ grub-core/tpm2/tpm2key_asn1_tab.c (new)
+ * This file is generated by 'asn1Parser tpm2key.asn' and the '#include'
+ * headers are replaced with the ones in grub2.
+ * - 'grub/mm.h' for the definition of 'NULL'
-+ * - 'grub/libtasn1.h' for the definition of 'asn1_static_node'
++ * - 'libtasn1.h' for the definition of 'asn1_static_node'
+ */
+
+#include <grub/mm.h>
-+#include <grub/libtasn1.h>
++#include <libtasn1.h>
+
+const asn1_static_node tpm2key_asn1_tab[] = {
+ { "TPM2KEY", 536875024, NULL },
@@ include/grub/tpm2/tpm2key.h (new)
+#define GRUB_TPM2_TPM2KEY_HEADER 1
+
+#include <grub/types.h>
-+#include <grub/libtasn1.h>
++#include <libtasn1.h>
+
+/*
+ * TPMPolicy ::= SEQUENCE {
12: 3b2872215 = 12: 2ea9e229d cryptodisk: Support key protectors
13: 55f8ea964 = 13: 0b8ec0df2 util/grub-protect: Add new tool
14: 3a29b8311 = 14: 6408f8708 tpm2: Support authorized policy
15: ca6c7ce07 = 15: e1a5ace39 tpm2: Implement NV index
16: e0349bcf8 = 16: a4e688f8f cryptodisk: Fallback to passphrase
17: 0a9e710fd = 17: e8edabbb8 cryptodisk: wipe out the cached keys from protectors
18: 31d81e0e4 = 18: 06613ec8e diskfilter: look up cryptodisk devices first
19: b43b7b6f3 ! 19: 25427d5b0 tpm2: Enable tpm2 module for grub-emu
@@ grub-core/Makefile.core.def: module = {
+ emu = tpm2/tcg2-emu.c;
enable = efi;
+ enable = emu;
+ cppflags = '-I$(srcdir)/lib/libtasn1-grub';
};
- module = {
## grub-core/kern/emu/main.c ##
@@20: 304ca3e27 ! 20: a52077174 tests: Add tpm2_test
@@ Commit message
This test script firstly creates a simple LUKS image to be loaded as a loopback device in grub-emu. Then an emulated TPM device is created by
- swtpm_cuse and PCR 0 and 1 are extended.
+ "swtpm chardev" and PCR 0 and 1 are extended.
There are several test cases in the script to test various settings. Each test case uses grub-protect or tpm2-tools to seal the LUKS password
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 1/20 posix_wrap: tweaks in preparation for libtasn1
From: Daniel Axtens <[email protected]>
- Define SIZEOF_UNSIGNED_LONG_INT, it's the same as SIZEOF_UNSIGNED_LONG.
- Define WORD_BIT, the size in bits of an int. This is a defined in the Single Unix Specification and in gnulib's limits.h. gnulib assumes it's 32 bits on all our platforms, including 64 bit platforms, so we also use that value.
- Provide strto[u]l[l] preprocessor macros that resolve to grub_strto[u]l[l]. To avoid gcrypt redefining strtoul, we also define HAVE_STRTOUL here.
- Implement c-ctype.h and the functions defined in the header.
- Implement strncat in string.h.
--- 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 + 5 files changed, 145 insertions(+) create mode 100644 grub-core/lib/posix_wrap/c-ctype.h
grub-core/lib/posix_wrap/c-ctype.h new file +114 -0
@@ -0,0 +1,114 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2024 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_POSIX_C_CTYPE_H +#define GRUB_POSIX_C_CTYPE_H 1 + +#include <grub/misc.h> + +static inline bool +c_isspace (int c) +{ +return !!grub_isspace (c); +} + +static inline bool +c_isdigit (int c) +{ +return !!grub_isdigit (c); +} + +static inline bool +c_islower (int c) +{ +return !!grub_islower (c); +} + +static inline bool +c_isascii (int c) +{ +return !(c & ~0x7f ); +} + +static inline bool +c_isupper (int c) +{ +return !!grub_isupper (c); +} + +static inline bool +c_isxdigit (int c) +{ +return !!grub_isxdigit (c); +} + +static inline bool +c_isprint (int c) +{ +return !!grub_isprint (c); +} + +static inline bool +c_iscntrl (int c) +{ +return !grub_isprint (c); +} + +static inline bool +c_isgraph (int c) +{ +return grub_isprint (c) && !grub_isspace (c); +} + +static inline bool +c_isalnum (int c) +{ +return grub_isalpha (c) ||grub_isdigit (c); +} + +static inline bool +c_ispunct (int c) +{ +return grub_isprint (c) && !grub_isspace (c) && !c_isalnum (c); +} + +static inline bool +c_isalpha (int c) +{ +return !!grub_isalpha (c); +} + +static inline bool +c_isblank (int c) +{ +return c ==' ' || c =='\t' ; +} + +static inline int +c_tolower (int c) +{ +return grub_tolower (c); +} + +static inline int +c_toupper (int c) +{ +return grub_toupper (c); +} + +#endif
grub-core/lib/posix_wrap/limits.h +1 -0
@@ -41,5 +41,6 @@#define LONG_MAX GRUB_LONG_MAX#define CHAR_BIT 8 +#define WORD_BIT 32#endif
grub-core/lib/posix_wrap/stdlib.h +8 -0
@@ -64,4 +64,12 @@ abort (void) grub_abort (); } +#define strtol grub_strtol + +/* for libgcrypt */ +#define HAVE_STRTOUL +#define strtoul grub_strtoul + +#define strtoull grub_strtoull +#endif
grub-core/lib/posix_wrap/string.h +21 -0
@@ -84,6 +84,27 @@ memchr (const void *s, int c, grub_size_t n)return grub_memchr (s, c, n); } +static inline char * +strncat (char *dest,const char *src,grub_size_t n) +{ +const char *end; +char *str = dest; +grub_size_t src_len; + + dest +=grub_strlen (dest); + + end =grub_memchr (src,'\0' , n); +if (end !=NULL ) + src_len = (grub_size_t) (end - src); +else + src_len = n; + + dest[src_len] ='\0' ; +grub_memcpy (dest, src, src_len); + +return str; +} +#define memcmp grub_memcmp#define memcpy grub_memcpy#define memmove grub_memmove
grub-core/lib/posix_wrap/sys/types.h +1 -0
@@ -50,6 +50,7 @@ typedef grub_uint8_t byte;typedef grub_addr_t uintptr_t ;#define SIZEOF_UNSIGNED_LONG GRUB_CPU_SIZEOF_LONG +#define SIZEOF_UNSIGNED_LONG_INT GRUB_CPU_SIZEOF_LONG#define SIZEOF_UNSIGNED_INT 4#define SIZEOF_UNSIGNED_LONG_LONG 8#define SIZEOF_UNSIGNED_SHORT 2
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 2/20 libtasn1: import libtasn1-4.19.0
In reply to Gary Lin via Grub-develFrom: Daniel Axtens <[email protected]>
Import a very trimmed-down set of libtasn1 files:
curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
tar xf libtasn1-4.19.0.tar.gz
rm -rf grub-core/lib/libtasn1/
mkdir -p grub-core/lib/libtasn1/lib/
cp libtasn1-4.19.0/{README.md,COPYING} grub-core/lib/libtasn1/
cp libtasn1-4.19.0/lib/{coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h} grub-core/libtasn1/lib/ cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1/
rm -rf libtasn1-4.19.0*
--- grub-core/lib/libtasn1/COPYING | 16 + grub-core/lib/libtasn1/README.md | 98 + grub-core/lib/libtasn1/lib/coding.c | 1425 +++++++++++++ grub-core/lib/libtasn1/lib/decoding.c | 2501 +++++++++++++++++++++++ grub-core/lib/libtasn1/lib/element.c | 1109 ++++++++++ grub-core/lib/libtasn1/lib/element.h | 42 + grub-core/lib/libtasn1/lib/errors.c | 100 + grub-core/lib/libtasn1/lib/gstr.c | 74 + grub-core/lib/libtasn1/lib/gstr.h | 50 + grub-core/lib/libtasn1/lib/int.h | 221 ++ 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 | 1225 +++++++++++ grub-core/lib/libtasn1/lib/structure.h | 46 + grub-core/lib/libtasn1/libtasn1.h | 643 ++++++ 15 files changed, 8900 insertions(+) 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/libtasn1.h
grub-core/lib/libtasn1/COPYING new file +16 -0
@@ -0,0 +1,16 @@ +LICENSING +========= + +The libtasn1 library is released under the GNU Lesser General Public +License (LGPL) version 2.1 or later; see [COPYING.LESSER](doc/COPYING.LESSER) +for the license terms. + +The GNU LGPL applies to the main libtasn1 library, while the +included applications library are under the GNU GPL version 3. +The libtasn1 library is located in the lib directory, while the applications +in src/. + +The documentation in doc/ is under the GNU FDL license 1.3. + +For any copyright year range specified as YYYY-ZZZZ in this package +note that the range specifies every single year in that closed interval.
grub-core/lib/libtasn1/README.md new file +98 -0
98 lines not on this page · raw message
grub-core/lib/libtasn1/lib/coding.c new file +1425 -0
1,425 lines not on this page · raw message
grub-core/lib/libtasn1/lib/decoding.c new file +2501 -0
1,461 lines not on this page · raw message
grub-core/lib/libtasn1/lib/element.c new file +1109 -0
1,109 lines not on this page · raw message
grub-core/lib/libtasn1/lib/element.h new file +42 -0
42 lines not on this page · raw message
grub-core/lib/libtasn1/lib/errors.c new file +100 -0
100 lines not on this page · raw message
grub-core/lib/libtasn1/lib/gstr.c new file +74 -0
74 lines not on this page · raw message
grub-core/lib/libtasn1/lib/gstr.h new file +50 -0
50 lines not on this page · raw message
grub-core/lib/libtasn1/lib/int.h new file +221 -0
221 lines not on this page · raw message
grub-core/lib/libtasn1/lib/parser_aux.c new file +1178 -0
1,178 lines not on this page · raw message
grub-core/lib/libtasn1/lib/parser_aux.h new file +172 -0
172 lines not on this page · raw message
grub-core/lib/libtasn1/lib/structure.c new file +1225 -0
1,225 lines not on this page · raw message
grub-core/lib/libtasn1/lib/structure.h new file +46 -0
46 lines not on this page · raw message
grub-core/lib/libtasn1/libtasn1.h new file +643 -0
643 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Part of the diff is not shown here. View the raw message.
Re: [PATCH v17 02/20] libtasn1: import libtasn1-4.19.0
Quoted 13 lines · Gary Lin
From: Daniel Axtens <[email protected]>
Import a very trimmed-down set of libtasn1 files:
curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz
tar xf libtasn1-4.19.0.tar.gz
rm -rf grub-core/lib/libtasn1/
mkdir -p grub-core/lib/libtasn1/lib/
cp libtasn1-4.19.0/{README.md,COPYING} grub-core/lib/libtasn1/
cp libtasn1-4.19.0/lib/{coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h} grub-core/libtasn1/lib/ cplibtasn1-4.19.0/lib/includes/libtasn1.hgrub-core/lib/libtasn1/
rm -rf libtasn1-4.19.0*
Quoted 3 lines
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Vladimir Serbinenko <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 3/20 libtasn1: disable code not needed in grub
In reply to Gary Lin via Grub-develBased on the patch from "Daniel Axtens <[email protected]>"
We don't expect to be able to write ASN.1, only read it, so we can disable some code.
Do that with #if 0/#endif, rather than deletion. This means that the difference between upstream and grub is smaller, which should make updating libtasn1 easier in the future.
With these exclusions we also avoid the need for minmax.h, which is convenient because it means we don't have to import it from gnulib.
--- ...asn1-disable-code-not-needed-in-grub.patch | 320 ++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch
grub-core/lib/libtasn1-patches/0001-libtasn1-disable-code-not-needed-in-grub.patch new file +320 -0
@@ -0,0 +1,320 @@ +From f95a7b9c72fe91e44a90067c9008791a15e490c0 Mon Sep 17 00:00:00 2001 +From: Daniel Axtens <[email protected]> +Date: Fri, 1 May 2020 17:12:23 +1000 +Subject: [PATCH 1/3] libtasn1: disable code not needed in grub + +We don't expect to be able to write ASN.1, only read it, +so we can disable some code. + +Do that with #if 0/#endif, rather than deletion. This means +that the difference between upstream and grub is smaller, +which should make updating libtasn1 easier in the future. + +With these exclusions we also avoid the need for minmax.h, +which is convenient because it means we don't have to +import it from gnulib. + +Cc: Vladimir Serbinenko <[email protected]> +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/coding.c | 12 ++++++++++-- + grub-core/lib/libtasn1-grub/lib/decoding.c | 2 ++ + grub-core/lib/libtasn1-grub/lib/element.c | 6 +++--- + grub-core/lib/libtasn1-grub/lib/errors.c | 3 +++ + grub-core/lib/libtasn1-grub/lib/structure.c | 10 ++++++---- + grub-core/lib/libtasn1-grub/libtasn1.h | 15 +++++++++++++++ + 6 files changed, 39 insertions(+), 9 deletions(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/coding.c b/grub-core/lib/libtasn1-grub/lib/coding.c +index ea5bc370e..5d03bca9d 100644 +--- a/grub-core/lib/libtasn1-grub/lib/coding.c ++++ b/grub-core/lib/libtasn1-grub/lib/coding.c +@@ -30,11 +30,11 @@ + #include "parser_aux.h" + #include <gstr.h> + #include "element.h" +-#include "minmax.h" + #include <structure.h> + + #define MAX_TAG_LEN 16 + ++#if 0 /* GRUB SKIPPED IMPORTING */ + /******************************************************/ + /* Function : _asn1_error_description_value_not_found */ + /* Description: creates the ErrorDescription string */ +@@ -58,6 +58,7 @@ _asn1_error_description_value_not_found (asn1_node node, + Estrcat (ErrorDescription, "' not found"); + + } ++#endif + + /** + * asn1_length_der: +@@ -244,6 +245,7 @@ asn1_encode_simple_der (unsigned int etype, const unsigned char *str, + return ASN1_SUCCESS; + } + ++#if 0 /* GRUB SKIPPED IMPORTING */ + /******************************************************/ + /* Function : _asn1_time_der */ + /* Description: creates the DER coding for a TIME */ +@@ -278,7 +280,7 @@ _asn1_time_der (unsigned char *str, int str_len, unsigned char *der, + + return ASN1_SUCCESS; + } +- ++#endif + + /* + void +@@ -519,6 +521,7 @@ asn1_bit_der (const unsigned char *str, int bit_len, + } + + ++#if 0 /* GRUB SKIPPED IMPORTING */ + /******************************************************/ + /* Function : _asn1_complete_explicit_tag */ + /* Description: add the length coding to the EXPLICIT */ +@@ -595,6 +598,7 @@ _asn1_complete_explicit_tag (asn1_node node, unsigned char *der, + + return ASN1_SUCCESS; + } ++#endif + + const tag_and_class_st _asn1_tags[] = { + [ASN1_ETYPE_GENERALSTRING] = +@@ -647,6 +651,8 @@ const tag_and_class_st _asn1_tags[] = { + + unsigned int _asn1_tags_size = sizeof (_asn1_tags) / sizeof (_asn1_tags[0]); + ++ ++#if 0 /* GRUB SKIPPED IMPORTING */ + /******************************************************/ + /* Function : _asn1_insert_tag_der */ + /* Description: creates the DER coding of tags of one */ +@@ -1423,3 +1429,5 @@ error: + asn1_delete_structure (&node); + return err; + } ++ ++#endif +diff --git a/grub-core/lib/libtasn1-grub/lib/decoding.c b/grub-core/lib/libtasn1-grub/lib/decoding.c +index b9245c486..bf9cb13ac 100644 +--- a/grub-core/lib/libtasn1-grub/lib/decoding.c ++++ b/grub-core/lib/libtasn1-grub/lib/decoding.c +@@ -1620,6 +1620,7 @@ asn1_der_decoding (asn1_node * element, const void *ider, int ider_len, + return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription); + } + ++#if 0 /* GRUB SKIPPED IMPORTING */ + /** + * asn1_der_decoding_element: + * @structure: pointer to an ASN1 structure +@@ -1650,6 +1651,7 @@ asn1_der_decoding_element (asn1_node * structure, const char *elementName, + { + return asn1_der_decoding (structure, ider, len, errorDescription); + } ++#endif + + /** + * asn1_der_decoding_startEnd: +diff --git a/grub-core/lib/libtasn1-grub/lib/element.c b/grub-core/lib/libtasn1-grub/lib/element.c +index d4c558e10..bc4c3c8d7 100644 +--- a/grub-core/lib/libtasn1-grub/lib/element.c ++++ b/grub-core/lib/libtasn1-grub/lib/element.c +@@ -118,7 +118,7 @@ _asn1_convert_integer (const unsigned char *value, unsigned char *value_out, + value_out[k2 - k] = val[k2]; + } + +-#if 0 ++#if 0 /* GRUB SKIPPED IMPORTING */ + printf ("_asn1_convert_integer: valueIn=%s, lenOut=%d", value, *len); + for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++) + printf (", vOut[%d]=%d", k, value_out[k]); +@@ -191,7 +191,7 @@ _asn1_append_sequence_set (asn1_node node, struct node_tail_cache_st *pcache) + return ASN1_SUCCESS; + } + +- ++#if 0 + /** + * asn1_write_value: + * @node_root: pointer to a structure +@@ -646,7 +646,7 @@ asn1_write_value (asn1_node node_root, const char *name, + + return ASN1_SUCCESS; + } +- ++#endif + + #define PUT_VALUE( ptr, ptr_size, data, data_size) \ + *len = data_size; \ +diff --git a/grub-core/lib/libtasn1-grub/lib/errors.c b/grub-core/lib/libtasn1-grub/lib/errors.c +index aef5dfe6f..2b2322152 100644 +--- a/grub-core/lib/libtasn1-grub/lib/errors.c ++++ b/grub-core/lib/libtasn1-grub/lib/errors.c +@@ -57,6 +57,8 @@ static const libtasn1_error_entry error_algorithms[] = { + {0, 0} + }; + ++ ++#if 0 /* GRUB SKIPPED IMPORTING */ + /** + * asn1_perror: + * @error: is an error returned by a libtasn1 function. +@@ -73,6 +75,7 @@ asn1_perror (int error) + const char *str = asn1_strerror (error); + fprintf (stderr, "LIBTASN1 ERROR: %s\n", str ? str : "(null)"); + } ++#endif + + /** + * asn1_strerror: +diff --git a/grub-core/lib/libtasn1-grub/lib/structure.c b/grub-core/lib/libtasn1-grub/lib/structure.c +index 512dd601f..f5a947d57 100644 +--- a/grub-core/lib/libtasn1-grub/lib/structure.c ++++ b/grub-core/lib/libtasn1-grub/lib/structure.c +@@ -76,7 +76,7 @@ _asn1_find_left (asn1_node_const node) + return node->left; + } + +- ++#if 0 /* GRUB SKIPPED IMPORTING */ + int + _asn1_create_static_structure (asn1_node_const pointer, + char *output_file_name, char *vector_name) +@@ -155,7 +155,7 @@ _asn1_create_static_structure (asn1_node_const pointer, + + return ASN1_SUCCESS; + } +- ++#endif + + /** + * asn1_array2tree: +@@ -721,7 +721,7 @@ asn1_create_element (asn1_node_const definitions, const char *source_name, + return res; + } + +- ++#if 0 /* GRUB SKIPPED IMPORTING */ + /** + * asn1_print_structure: + * @out: pointer to the output file (e.g. stdout). +@@ -1062,7 +1062,7 @@ asn1_print_structure (FILE * out, asn1_node_const structure, const char *name, + } + } + } +- ++#endif + + + /** +@@ -1158,6 +1158,7 @@ asn1_find_structure_from_oid (asn1_node_const definitions, + return NULL; /* ASN1_ELEMENT_NOT_FOUND; */ + } + ++#if 0 /* GRUB SKIPPED IMPORTING */ + /** + * asn1_copy_node: + * @dst: Destination asn1 node. +@@ -1207,6 +1208,7 @@ asn1_copy_node (asn1_node dst, const char *dst_name, + + return result; + } ++#endif + + /** + * asn1_dup_node: +diff --git a/grub-core/lib/libtasn1-grub/libtasn1.h b/grub-core/lib/libtasn1-grub/libtasn1.h +index 51cc7879f..058ab27b0 100644 +--- a/grub-core/lib/libtasn1-grub/libtasn1.h ++++ b/grub-core/lib/libtasn1-grub/libtasn1.h +@@ -318,6 +318,8 @@ extern "C" + /* Functions definitions */ + /***********************************/ + ++/* These functions are not used in grub and should not be referenced. */ ++# if 0 /* GRUB SKIPPED IMPORTING */ + extern ASN1_API int + asn1_parser2tree (const char *file, + asn1_node * definitions, char *error_desc); +@@ -326,14 +328,17 @@ extern "C" + asn1_parser2array (const char *inputFileName, + const char *outputFileName, + const char *vectorName, char *error_desc); ++# endif + + extern ASN1_API int + asn1_array2tree (const asn1_static_node * array, + asn1_node * definitions, char *errorDescription); + ++# if 0 /* GRUB SKIPPED IMPORTING */ + extern ASN1_API void + asn1_print_structure (FILE * out, asn1_node_const structure, + const char *name, int mode); ++# endif + + extern ASN1_API int + asn1_create_element (asn1_node_const definitions, +@@ -347,9 +352,11 @@ extern "C" + extern ASN1_API int + asn1_delete_element (asn1_node structure, const char *element_name); + ++# if 0 /* GRUB SKIPPED IMPORTING */ + extern ASN1_API int + asn1_write_value (asn1_node node_root, const char *name, + const void *ivalue, int len); ++# endif + + extern ASN1_API int + asn1_read_value (asn1_node_const root, const char *name, +@@ -366,9 +373,11 @@ extern "C" + asn1_number_of_elements (asn1_node_const element, const char *name, + int *num); + ++# if 0 /* GRUB SKIPPED IMPORTING */ + extern ASN1_API int + asn1_der_coding (asn1_node_const element, const char *name, + void *ider, int *len, char *ErrorDescription); ++# endif + + extern ASN1_API int + asn1_der_decoding2 (asn1_node * element, const void *ider, +@@ -379,6 +388,7 @@ extern "C" + asn1_der_decoding (asn1_node * element, const void *ider, + int ider_len, char *errorDescription); + ++# if 0 /* GRUB SKIPPED IMPORTING */ + /* Do not use. Use asn1_der_decoding() instead. */ + extern ASN1_API int + asn1_der_decoding_element (asn1_node * structure, +@@ -386,6 +396,7 @@ extern "C" + const void *ider, int len, + char *errorDescription) + _ASN1_GCC_ATTR_DEPRECATED; ++# endif + + extern ASN1_API int + asn1_der_decoding_startEnd (asn1_node element, +@@ -411,12 +422,16 @@ extern "C" + const char + *oidValue); + ++# if 0 /* GRUB SKIPPED IMPORTING */ + __LIBTASN1_PURE__ + extern ASN1_API const char *asn1_check_version (const char *req_version); ++# endif + + __LIBTASN1_PURE__ extern ASN1_API const char *asn1_strerror (int error); + ++# if 0 /* GRUB SKIPPED IMPORTING */ + extern ASN1_API void asn1_perror (int error); ++# endif + + # define ASN1_MAX_TAG_SIZE 4 + # define ASN1_MAX_LENGTH_SIZE 9 +-- +2.35.3 +
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 03/20] libtasn1: disable code not needed in grub
Quoted 9 lines · Gary Lin
Based on the patch from "Daniel Axtens <[email protected]>"
We don't expect to be able to write ASN.1, only read it, so we can disable some code.
Do that with #if 0/#endif, rather than deletion. This means that the difference between upstream and grub is smaller, which should make updating libtasn1 easier in the future.
Quoted 5 lines
With these exclusions we also avoid the need for
minmax.h, which is convenient because it means we don't have to import it from gnulib.Signed-off-by Gary Lin <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 4/20 libtasn1: changes for grub compatibility
In reply to Gary Lin via Grub-develBased on the patch from "Daniel Axtens <[email protected]>"
Do a few things to make libtasn1 compile as part of grub:
- remove _asn1_strcat and replace strcat with the bound-checked _asn1_str_cat except the one inside _asn1_str_cat. That strcat is replaced with strcpy.
- adjust header paths in libtasn1.h
- replace a 64 bit division with a call to grub_divmod64, preventing creation of __udivdi3 calls on 32 bit platforms.
--- ...tasn1-changes-for-grub-compatibility.patch | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch
grub-core/lib/libtasn1-patches/0002-libtasn1-changes-for-grub-compatibility.patch new file +125 -0
@@ -0,0 +1,125 @@ +From 1d62aa76a833855f58261fc69f40bd7763182274 Mon Sep 17 00:00:00 2001 +From: Daniel Axtens <[email protected]> +Date: Fri, 1 May 2020 20:44:29 +1000 +Subject: [PATCH 2/3] libtasn1: changes for grub compatibility + +Do a few things to make libtasn1 compile as part of grub: + + - remove _asn1_strcat and replace strcat with the bound-checked + _asn1_str_cat except the one inside _asn1_str_cat. That strcat is + replaced with strcpy. + + - adjust header paths in libtasn1.h + + - replace a 64 bit division with a call to grub_divmod64, preventing + creation of __udivdi3 calls on 32 bit platforms. + +Cc: Vladimir Serbinenko <[email protected]> +Signed-off-by: Daniel Axtens <[email protected]> +Signed-off-by: Gary Lin <[email protected]> +Reviewed-by: Stefan Berger <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/decoding.c | 8 ++++---- + grub-core/lib/libtasn1-grub/lib/element.c | 2 +- + grub-core/lib/libtasn1-grub/lib/gstr.c | 2 +- + grub-core/lib/libtasn1-grub/lib/int.h | 1 - + grub-core/lib/libtasn1-grub/lib/parser_aux.c | 2 +- + grub-core/lib/libtasn1-grub/libtasn1.h | 5 ++--- + 6 files changed, 9 insertions(+), 11 deletions(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/decoding.c b/grub-core/lib/libtasn1-grub/lib/decoding.c +index bf9cb13ac..51859fe36 100644 +--- a/grub-core/lib/libtasn1-grub/lib/decoding.c ++++ b/grub-core/lib/libtasn1-grub/lib/decoding.c +@@ -2016,8 +2016,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element, + (p2->type & CONST_ASSIGN)) + { + strcpy (name, definitions->name); +- strcat (name, "."); +- strcat (name, p2->name); ++ _asn1_str_cat (name, sizeof (name), "."); ++ _asn1_str_cat (name, sizeof (name), p2->name); + + len = sizeof (value); + result = asn1_read_value (definitions, name, value, &len); +@@ -2034,8 +2034,8 @@ asn1_expand_octet_string (asn1_node_const definitions, asn1_node * element, + if (p2) + { + strcpy (name, definitions->name); +- strcat (name, "."); +- strcat (name, p2->name); ++ _asn1_str_cat (name, sizeof (name), "."); ++ _asn1_str_cat (name, sizeof (name), p2->name); + + result = asn1_create_element (definitions, name, &aux); + if (result == ASN1_SUCCESS) +diff --git a/grub-core/lib/libtasn1-grub/lib/element.c b/grub-core/lib/libtasn1-grub/lib/element.c +index bc4c3c8d7..8694fecb9 100644 +--- a/grub-core/lib/libtasn1-grub/lib/element.c ++++ b/grub-core/lib/libtasn1-grub/lib/element.c +@@ -688,7 +688,7 @@ asn1_write_value (asn1_node node_root, const char *name, + return ASN1_MEM_ERROR; \ + } else { \ + /* this strcat is checked */ \ +- if (ptr) _asn1_strcat (ptr, data); \ ++ if (ptr) _asn1_str_cat ((char *)ptr, ptr_size, (const char *)data); \ + } + + /** +diff --git a/grub-core/lib/libtasn1-grub/lib/gstr.c b/grub-core/lib/libtasn1-grub/lib/gstr.c +index eef419554..a9c16f5d3 100644 +--- a/grub-core/lib/libtasn1-grub/lib/gstr.c ++++ b/grub-core/lib/libtasn1-grub/lib/gstr.c +@@ -36,7 +36,7 @@ _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src) + + if (dest_tot_size - dest_size > str_size) + { +- strcat (dest, src); ++ strcpy (dest + dest_size, src); + } + else + { +diff --git a/grub-core/lib/libtasn1-grub/lib/int.h b/grub-core/lib/libtasn1-grub/lib/int.h +index d94d51c8c..cadd80df6 100644 +--- a/grub-core/lib/libtasn1-grub/lib/int.h ++++ b/grub-core/lib/libtasn1-grub/lib/int.h +@@ -115,7 +115,6 @@ extern const tag_and_class_st _asn1_tags[]; + # define _asn1_strtoul(n,e,b) strtoul((const char *) n, e, b) + # define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b) + # define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b) +-# define _asn1_strcat(a,b) strcat((char *)a, (const char *)b) + + # if SIZEOF_UNSIGNED_LONG_INT == 8 + # define _asn1_strtou64(n,e,b) strtoul((const char *) n, e, b) +diff --git a/grub-core/lib/libtasn1-grub/lib/parser_aux.c b/grub-core/lib/libtasn1-grub/lib/parser_aux.c +index c05bd2339..e4e4c0556 100644 +--- a/grub-core/lib/libtasn1-grub/lib/parser_aux.c ++++ b/grub-core/lib/libtasn1-grub/lib/parser_aux.c +@@ -632,7 +632,7 @@ _asn1_ltostr (int64_t v, char str[LTOSTR_MAX_SIZE]) + count = 0; + do + { +- d = val / 10; ++ d = grub_divmod64(val, 10, NULL); + r = val - d * 10; + temp[start + count] = '0' + (char) r; + count++; +diff --git a/grub-core/lib/libtasn1-grub/libtasn1.h b/grub-core/lib/libtasn1-grub/libtasn1.h +index 058ab27b0..7d64b6ab7 100644 +--- a/grub-core/lib/libtasn1-grub/libtasn1.h ++++ b/grub-core/lib/libtasn1-grub/libtasn1.h +@@ -54,9 +54,8 @@ + # define __LIBTASN1_PURE__ + # endif + +-# include <sys/types.h> +-# include <time.h> +-# include <stdio.h> /* for FILE* */ ++# include <grub/types.h> ++# include <grub/time.h> + + # ifdef __cplusplus + extern "C" +-- +2.35.3 +
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 04/20] libtasn1: changes for grub compatibility
Quoted 4 lines · Gary Lin
Based on the patch from "Daniel Axtens <[email protected]>"
Do a few things to make libtasn1 compile as part of grub:
Quoted 8 lines
- remove
_asn1_strcatand replace strcat with the bound-checked_asn1_str_catexcept the one inside_asn1_str_cat. That strcat is replaced with strcpy.- adjust header paths in
libtasn1.h- replace a 64 bit division with a call to
grub_divmod64, preventing creation of__udivdi3calls on 32 bit platforms.
This patch should be split into three separate patches.
I think this and other libtasn1 fixes should have "Signed-off-by: Daniel Axtens <[email protected]>" here.
Quoted 1 line
Signed-off-by Gary Lin <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 04/20] libtasn1: changes for grub compatibility
Quoted 11 lines · Daniel Kiper
Quoted 10 lines · Gary Lin
Based on the patch from "Daniel Axtens <[email protected]>"
Do a few things to make libtasn1 compile as part of grub:
- remove
_asn1_strcatand replace strcat with the bound-checked_asn1_str_catexcept the one inside_asn1_str_cat. That strcat is replaced with strcpy.- adjust header paths in
libtasn1.h
Quoted 8 lines
Quoted 2 lines
- replace a 64 bit division with a call to
grub_divmod64, preventing creation of__udivdi3calls on 32 bit platforms.This patch should be split into three separate patches.
I think this and other libtasn1 fixes should have "Signed-off-by: Daniel Axtens <[email protected]>" here.
I'll add the SOB to the patches from Daniel Axtens.
Thanks,
Gary Lin
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 5/20 libtasn1: fix the potential buffer overrun
In reply to Gary Lin via Grub-develIn _asn1_tag_der(), the first while loop for the long form may end up with a 'k' value with 'ASN1_MAX_TAG_SIZE' and cause the buffer overrun in the second while loop. This commit tweaks the conditional check to avoid producing a too large 'k'.
This is a quick fix and may differ from the official upstream fix.
libtasn1 issue: https://gitlab.com/gnutls/libtasn1/-/issues/49
--- ...sn1-fix-the-potential-buffer-overrun.patch | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 grub-core/lib/libtasn1-patches/0003-libtasn1-fix-the-potential-buffer-overrun.patch
grub-core/lib/libtasn1-patches/0003-libtasn1-fix-the-potential-buffer-overrun.patch new file +35 -0
@@ -0,0 +1,35 @@ +From 636c1cbfe1e244ba887c6e1a18fabbcb3bb8b1b8 Mon Sep 17 00:00:00 2001 +From: Gary Lin <[email protected]> +Date: Mon, 8 Apr 2024 14:57:21 +0800 +Subject: [PATCH 3/3] libtasn1: fix the potential buffer overrun + +In _asn1_tag_der(), the first while loop for the long form may end up +with a 'k' value with 'ASN1_MAX_TAG_SIZE' and cause the buffer overrun +in the second while loop. This commit tweaks the conditional check to +avoid producing a too large 'k'. + +This is a quick fix and may differ from the official upstream fix. + +libtasn1 issue: https://gitlab.com/gnutls/libtasn1/-/issues/49 + +Signed-off-by: Gary Lin <[email protected]> +--- + grub-core/lib/libtasn1-grub/lib/coding.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/grub-core/lib/libtasn1-grub/lib/coding.c b/grub-core/lib/libtasn1-grub/lib/coding.c +index 5d03bca9d..0458829a5 100644 +--- a/grub-core/lib/libtasn1-grub/lib/coding.c ++++ b/grub-core/lib/libtasn1-grub/lib/coding.c +@@ -143,7 +143,7 @@ _asn1_tag_der (unsigned char class, unsigned int tag_value, + temp[k++] = tag_value & 0x7F; + tag_value >>= 7; + +- if (k > ASN1_MAX_TAG_SIZE - 1) ++ if (k >= ASN1_MAX_TAG_SIZE - 1) + break; /* will not encode larger tags */ + } + *ans_len = k + 1; +-- +2.35.3 +
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 05/20] libtasn1: fix the potential buffer overrun
Quoted 8 lines · Gary Lin
In
_asn1_tag_der(), the first while loop for the long form may end up with a 'k' value with 'ASN1_MAX_TAG_SIZE' and cause the buffer overrun in the second while loop. This commit tweaks the conditional check to avoid producing a too large 'k'.This is a quick fix and may differ from the official upstream fix.
libtasn1 issue: https://gitlab.com/gnutls/libtasn1/-/issues/49
This patch does not need Daniel Axtens SB of course. Sorry for not being precise.
Quoted 1 line
Signed-off-by Gary Lin <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 6/20 libtasn1: compile into asn1 module
In reply to Gary Lin via Grub-develFrom: Daniel Axtens <[email protected]>
Create a wrapper file that specifies the module license. Set up the makefile so it is built.
--- autogen.sh | 16 ++++++++++++++++ grub-core/Makefile.core.def | 15 +++++++++++++++ grub-core/lib/libtasn1_wrap/wrap.c | 26 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 grub-core/lib/libtasn1_wrap/wrap.c
autogen.sh +16 -0
@@ -51,6 +51,22 @@ for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x" done +echo "Importing libtasn1..." +if [ -d grub-core/lib/libtasn1-grub ]; then + rm -rf grub-core/lib/libtasn1-grub +fi + +mkdir -p grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/lib/*.[ch] grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/libtasn1.h grub-core/lib/libtasn1-grub/ + +for patch in \ + 0001-libtasn1-disable-code-not-needed-in-grub.patch \ + 0002-libtasn1-changes-for-grub-compatibility.patch \ + 0003-libtasn1-fix-the-potential-buffer-overrun.patch ; do + patch -p1 -i grub-core/lib/libtasn1-patches/$patch +done + echo "Generating Automake input..." # Automake doesn't like including files from a path outside the project.
grub-core/Makefile.core.def +15 -0
@@ -2605,3 +2605,18 @@ module = { enable = efi; depends = part_gpt; }; + +module = { + name = asn1; + common = lib/libtasn1-grub/lib/decoding.c; + common = lib/libtasn1-grub/lib/coding.c; + common = lib/libtasn1-grub/lib/element.c; + common = lib/libtasn1-grub/lib/structure.c; + common = lib/libtasn1-grub/lib/parser_aux.c; + common = lib/libtasn1-grub/lib/gstr.c; + common = lib/libtasn1-grub/lib/errors.c; + common = lib/libtasn1_wrap/wrap.c; + cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)'; + // -Wno-type-limits comes from libtasn1's configure.ac + cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits'; +};
grub-core/lib/libtasn1_wrap/wrap.c new file +26 -0
@@ -0,0 +1,26 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020 IBM Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/dl.h> + +/* +* libtasn1 is provided under LGPL2.1+, which is compatible +* with GPL3+. As Grub as a whole is under GPL3+, this module +* is therefore under GPL3+ also. +*/ +GRUB_MOD_LICENSE ("GPLv3+" );
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 06/20] libtasn1: compile into asn1 module
Quoted 8 lines · Gary Lin
From: Daniel Axtens <[email protected]>
Create a wrapper file that specifies the module license. Set up the makefile so it is built.
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Vladimir Serbinenko <[email protected]>
Due to amount of changes to the patch I think this RB should be dropped now.
Though Reviewed-by: Daniel Kiper <[email protected]>...
Two nits below...
Quoted 45 lines
--- autogen.sh | 16 ++++++++++++++++ grub-core/Makefile.core.def | 15 +++++++++++++++ grub-core/lib/libtasn1_wrap/wrap.c | 26 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 grub-core/lib/libtasn1_wrap/wrap.cautogen.sh +16
-0@@ -51,6 +51,22 @@ for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x" done +echo "Importing libtasn1..." +if [ -d grub-core/lib/libtasn1-grub ]; then + rm -rf grub-core/lib/libtasn1-grub +fi + +mkdir -p grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/lib/*.[ch] grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/libtasn1.h grub-core/lib/libtasn1-grub/ + +for patch in \ + 0001-libtasn1-disable-code-not-needed-in-grub.patch \ + 0002-libtasn1-changes-for-grub-compatibility.patch \ + 0003-libtasn1-fix-the-potential-buffer-overrun.patch ; do + patch -p1 -i grub-core/lib/libtasn1-patches/$patch +done + echo "Generating Automake input..." # Automake doesn't like including files from a path outside the project.grub-core/Makefile.core.def +3
-0@@ -2605,3 +2605,18 @@ module = { enable = efi; depends = part_gpt; }; + +module = { + name = asn1;
grub-core/Makefile.core.def module = { · line 2620
Quoted 10 lines
grub-core/Makefile.core.def +10
-0+ common = lib/libtasn1-grub/lib/decoding.c; + common = lib/libtasn1-grub/lib/coding.c; + common = lib/libtasn1-grub/lib/element.c; + common = lib/libtasn1-grub/lib/structure.c; + common = lib/libtasn1-grub/lib/parser_aux.c; + common = lib/libtasn1-grub/lib/gstr.c; + common = lib/libtasn1-grub/lib/errors.c; + common = lib/libtasn1_wrap/wrap.c; + cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)'; + // -Wno-type-limits comes from libtasn1's configure.ac
Please use "/* ... */" for comments.
Quoted 21 lines
grub-core/Makefile.core.def +2
-0+ cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits'; +};grub-core/lib/libtasn1_wrap/wrap.c new file +13
-0@@ -0,0 +1,26 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020 IBM Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details.
grub-core/lib/libtasn1_wrap/wrap.c line 23
Quoted 10 lines
grub-core/lib/libtasn1_wrap/wrap.c new file +10
-0+* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/dl.h> + +/* +* libtasn1 is provided under LGPL2.1+, which is compatible +* with GPL3+. As Grub as a whole is under GPL3+, this module
s/Grub/GRUB/
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 06/20] libtasn1: compile into asn1 module
Quoted 12 lines · Daniel Kiper
Quoted 8 lines · Gary Lin
From: Daniel Axtens <[email protected]>
Create a wrapper file that specifies the module license. Set up the makefile so it is built.
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Vladimir Serbinenko <[email protected]>Due to amount of changes to the patch I think this RB should be dropped now.
Will drop this RB in v18.
Quoted 53 lines
Though Reviewed-by: Daniel Kiper <[email protected]>...
Two nits below...
Quoted 49 lines
--- autogen.sh | 16 ++++++++++++++++ grub-core/Makefile.core.def | 15 +++++++++++++++ grub-core/lib/libtasn1_wrap/wrap.c | 26 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 grub-core/lib/libtasn1_wrap/wrap.cautogen.sh +16
-0@@ -51,6 +51,22 @@ for x in mpi-asm-defs.h mpih-add1.c mpih-sub1.c mpih-mul1.c mpih-mul2.c mpih-mul cp grub-core/lib/libgcrypt-grub/mpi/generic/"$x" grub-core/lib/libgcrypt-grub/mpi/"$x" done +echo "Importing libtasn1..." +if [ -d grub-core/lib/libtasn1-grub ]; then + rm -rf grub-core/lib/libtasn1-grub +fi + +mkdir -p grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/lib/*.[ch] grub-core/lib/libtasn1-grub/lib +cp grub-core/lib/libtasn1/libtasn1.h grub-core/lib/libtasn1-grub/ + +for patch in \ + 0001-libtasn1-disable-code-not-needed-in-grub.patch \ + 0002-libtasn1-changes-for-grub-compatibility.patch \ + 0003-libtasn1-fix-the-potential-buffer-overrun.patch ; do + patch -p1 -i grub-core/lib/libtasn1-patches/$patch +done + echo "Generating Automake input..." # Automake doesn't like including files from a path outside the project.grub-core/Makefile.core.def +7
-0@@ -2605,3 +2605,18 @@ module = { enable = efi; depends = part_gpt; }; + +module = { + name = asn1; + common = lib/libtasn1-grub/lib/decoding.c; + common = lib/libtasn1-grub/lib/coding.c; + common = lib/libtasn1-grub/lib/element.c; + common = lib/libtasn1-grub/lib/structure.c;
Quoted 9 lines
grub-core/Makefile.core.def module = { · line 2620
Quoted 6 lines
grub-core/Makefile.core.def +6
-0+ common = lib/libtasn1-grub/lib/parser_aux.c; + common = lib/libtasn1-grub/lib/gstr.c; + common = lib/libtasn1-grub/lib/errors.c; + common = lib/libtasn1_wrap/wrap.c; + cflags = '$(CFLAGS_POSIX) $(CFLAGS_GNULIB)'; + // -Wno-type-limits comes from libtasn1's configure.acPlease use "/* ... */" for comments.
Will fix it in v18.
Quoted 25 lines
Quoted 25 lines
grub-core/Makefile.core.def +2
-0+ cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits'; +};grub-core/lib/libtasn1_wrap/wrap.c new file +17
-0@@ -0,0 +1,26 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020 IBM Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/
Quoted 9 lines
grub-core/lib/libtasn1_wrap/wrap.c line 23
Quoted 6 lines
grub-core/lib/libtasn1_wrap/wrap.c new file +6
-0+ +#include <grub/dl.h> + +/* +* libtasn1 is provided under LGPL2.1+, which is compatible +* with GPL3+. As Grub as a whole is under GPL3+, this module s/Grub/GRUB/
ditto
Thanks,
Gary Lin
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 7/20 asn1_test: test module for libtasn1
In reply to Gary Lin via Grub-develFrom: Daniel Axtens <[email protected]>
Import tests from libtasn1 that don't use functionality we don't import. This test module is integrated into functional_test so that the user can run the test in grub shell.
This doesn't test the full decoder but that will be exercised in test suites for coming patch sets.
Add testcase target in accordance with5e10be48e5 tests: Add check-native and check-nonnative make targets
--- Makefile.util.def | 6 + grub-core/Makefile.core.def | 14 ++ .../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 + tests/asn1_test.in | 11 + 16 files changed, 1234 insertions(+) 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 tests/asn1_test.in
Makefile.util.def +6 -0
@@ -1257,6 +1257,12 @@ script = { common = tests/luks2_test.in; }; +script = { + testcase = native; + name = asn1_test; + common = tests/asn1_test.in; +}; + program = { testcase = native; name = example_unit_test;
grub-core/Makefile.core.def +14 -0
@@ -2620,3 +2620,17 @@ module = { // -Wno-type-limits comes from libtasn1's configure.ac cppflags = '$(CPPFLAGS_POSIX) $(CPPFLAGS_GNULIB) -I$(srcdir)/lib/libtasn1-grub -I$(srcdir)/lib/libtasn1-grub/lib -Wno-type-limits'; }; + +module = { + name = asn1_test; + common = tests/asn1/CVE-2018-1000654.c; + common = tests/asn1/object-id-decoding.c; + common = tests/asn1/object-id-encoding.c; + common = tests/asn1/octet-string.c; + common = tests/asn1/reproducers.c; + common = tests/asn1/Test_overflow.c; + common = tests/asn1/Test_simple.c; + common = tests/asn1/Test_strings.c; + common = tests/asn1/asn1_test.c; + cppflags = '-I$(srcdir)/lib/libtasn1-grub'; +};
grub-core/tests/asn1/CVE-2018-1000654-1_asn1_tab.h new file +32 -0
@@ -0,0 +1,32 @@ +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <libtasn1.h> + +const asn1_static_node CVE_2018_1000654_1_asn1_tab[] = { + {"TEST_TREE" ,536875024 ,NULL }, + {NULL ,1610612748 ,NULL }, + {"iso" ,1073741825 ,"1" }, + {"identified-organization" ,1073741825 ,"3" }, + {"dod" ,1073741825 ,"6" }, + {"internet" ,1073741825 ,"1" }, + {"security" ,1073741825 ,"5" }, + {"mechanisms" ,1073741825 ,"5" }, + {"pkix" ,1073741825 ,"7" }, + {"id-mod" ,1073741825 ,"0" }, + {"id-pkix1-implicit-88" ,1 ,"2" }, + {"id-xnyTest" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-ix" }, + {NULL ,1073741825 ,"29" }, + {NULL ,1 ,"1" }, + {"id-ix" ,1880096780 ,"OBJECR" }, + {NULL ,1073741825 ,"id-ix" }, + {NULL ,1073741825 ,"29" }, + {NULL ,1 ,"2" }, + {"id-xnyTest" ,805306380 ,NULL }, + {NULL ,1073741825 ,"id-ix" }, + {NULL ,1073741825 ,"29" }, + {NULL ,1 ,"1" }, + {NULL ,0 ,NULL } +};
grub-core/tests/asn1/CVE-2018-1000654-2_asn1_tab.h new file +36 -0
@@ -0,0 +1,36 @@ +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <libtasn1.h> + +const asn1_static_node CVE_2018_1000654_2_asn1_tab[] = { + {"TEST_TREE" ,536875024 ,NULL }, + {NULL ,1610612748 ,NULL }, + {"iso" ,1073741825 ,"1" }, + {"identified-organization" ,1073741825 ,"3" }, + {"dod" ,1073741825 ,"6" }, + {"internet" ,1073741825 ,"1" }, + {"security" ,1073741825 ,"5" }, + {"mechanisms" ,1073741825 ,"5" }, + {"pkix" ,1073741825 ,"7" }, + {"id-mod" ,1073741825 ,"0" }, + {"id-pkix1-implicit-88" ,1 ,"2" }, + {"id-oneTest" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-two" }, + {NULL ,1073741825 ,"9" }, + {NULL ,1 ,"1" }, + {"id-two" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-three" }, + {NULL ,1073741825 ,"2" }, + {NULL ,1 ,"2" }, + {"id-three" ,1879048204 ,NULL }, + {NULL ,1073741825 ,"id-four" }, + {NULL ,1073741825 ,"3" }, + {NULL ,1 ,"3" }, + {"id-four" ,805306380 ,NULL }, + {NULL ,1073741825 ,"id-two" }, + {NULL ,1073741825 ,"3" }, + {NULL ,1 ,"3" }, + {NULL ,0 ,NULL } +};
grub-core/tests/asn1/CVE-2018-1000654.c new file +58 -0
58 lines not on this page · raw message
grub-core/tests/asn1/Test_overflow.c new file +134 -0
134 lines not on this page · raw message
grub-core/tests/asn1/Test_simple.c new file +205 -0
205 lines not on this page · raw message
grub-core/tests/asn1/Test_strings.c new file +142 -0
142 lines not on this page · raw message
grub-core/tests/asn1/asn1_test.c new file +49 -0
49 lines not on this page · raw message
grub-core/tests/asn1/asn1_test.h new file +44 -0
44 lines not on this page · raw message
grub-core/tests/asn1/object-id-decoding.c new file +109 -0
109 lines not on this page · raw message
grub-core/tests/asn1/object-id-encoding.c new file +114 -0
114 lines not on this page · raw message
grub-core/tests/asn1/octet-string.c new file +199 -0
199 lines not on this page · raw message
grub-core/tests/asn1/reproducers.c new file +80 -0
80 lines not on this page · raw message
grub-core/tests/lib/functional_test.c +1 -0
@@ -79,6 +79,7 @@ grub_functional_all_tests (grub_extcmd_context_t ctxt __attribute__ ((unused)),grub_dl_load ("cmp_test" );grub_dl_load ("mul_test" );grub_dl_load ("shift_test" ); +grub_dl_load ("asn1_test" );FOR_LIST_ELEMENTS (test, grub_test_list) ok = !grub_test_run (test) && ok;
tests/asn1_test.in new file +11 -0
@@ -0,0 +1,11 @@ +#! @BUILD_SHEBANG@ +set -e + +. "@builddir@/grub-core/modinfo.sh" + +out=`echo functional_test asn1_test | @builddir@/grub-shell` + +if [ "$(echo "$out" | tail -n 1)" != "ALL TESTS PASSED" ]; then + echo "ASN.1 test failure: $out" + exit 1 +fi
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 07/20] asn1_test: test module for libtasn1
Quoted 24 lines · Gary Lin
From: Daniel Axtens <[email protected]>
Import tests from libtasn1 that don't use functionality we don't import. This test module is integrated into
functional_testso that the user can run the test in grub shell.This doesn't test the full decoder but that will be exercised in test suites for coming patch sets.
Add testcase target in accordance with
5e10be48e5tests: Add check-native and check-nonnative make targetsCc Vladimir Serbinenko <[email protected]>Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Gary Lin <[email protected]>--- Makefile.util.def | 6 + grub-core/Makefile.core.def | 14 ++ .../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 ++++++++++++
Quoted 8 lines
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 + tests/asn1_test.in | 11 +
If most of these files come from libtasn1 then you should add a list of commands which allows us recreate them from libtasn1 source. And probably introduction of original libtasn1 test files should happen in the patch #2.
[...]
Quoted 43 lines
grub-core/tests/asn1/Test_overflow.c new file +37
-0@@ -0,0 +1,134 @@ +/* +* Copyright (C) 2012-2014 Free Software Foundation, Inc. +* +* This file is part of LIBTASN1. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +/* Written by Simon Josefsson */ + +#include "asn1_test.h" + +int +test_overflow (void ) +{ +/* Test that values larger than long are rejected. This has worked +fine with all versions of libtasn1. */ + + { +unsigned char der[] ="\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ; +long l; +int len; + + l =asn1_get_length_der (der,sizeof der, &len); +
grub-core/tests/asn1/Test_overflow.c line 47
Quoted 10 lines
grub-core/tests/asn1/Test_overflow.c new file +10
-0+if (l !=-2L ) + { +grub_printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n" , l, len); +return 1 ; + } + } + +/* Test that values larger than int but smaller than long are +rejected. This limitation was introduced with libtasn1 2.12. */ +#if (GRUB_LONG_MAX >GRUB_INT_MAX )
This change suggests it is a mixture of libtasn1 source and GRUB specific patches. Please disaggregate them.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 07/20] asn1_test: test module for libtasn1
Quoted 34 lines · Daniel Kiper
Quoted 24 lines · Gary Lin
From: Daniel Axtens <[email protected]>
Import tests from libtasn1 that don't use functionality we don't import. This test module is integrated into
functional_testso that the user can run the test in grub shell.This doesn't test the full decoder but that will be exercised in test suites for coming patch sets.
Add testcase target in accordance with
5e10be48e5tests: Add check-native and check-nonnative make targetsCc Vladimir Serbinenko <[email protected]>Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Gary Lin <[email protected]>--- Makefile.util.def | 6 + grub-core/Makefile.core.def | 14 ++ .../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 ++++++++++++Quoted 8 lines
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 + tests/asn1_test.in | 11 +
Quoted 4 lines
If most of these files come from libtasn1 then you should add a list of commands which allows us recreate them from libtasn1 source. And probably introduction of original libtasn1 test files should happen in the patch #2.
Ok, I'll import those files in patch #2 and add a follow-up patch for the grub compatibility.
Gary Lin
Quoted 59 lines
[...]
Quoted 43 lines
grub-core/tests/asn1/Test_overflow.c new file +37
-0@@ -0,0 +1,134 @@ +/* +* Copyright (C) 2012-2014 Free Software Foundation, Inc. +* +* This file is part of LIBTASN1. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +/* Written by Simon Josefsson */ + +#include "asn1_test.h" + +int +test_overflow (void ) +{ +/* Test that values larger than long are rejected. This has worked +fine with all versions of libtasn1. */ + + { +unsigned char der[] ="\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" ; +long l; +int len; + + l =asn1_get_length_der (der,sizeof der, &len); +grub-core/tests/asn1/Test_overflow.c line 47
Quoted 10 lines
grub-core/tests/asn1/Test_overflow.c new file +10
-0+if (l !=-2L ) + { +grub_printf ("ERROR: asn1_get_length_der bignum (l %ld len %d)\n" , l, len); +return 1 ; + } + } + +/* Test that values larger than int but smaller than long are +rejected. This limitation was introduced with libtasn1 2.12. */ +#if (GRUB_LONG_MAX >GRUB_INT_MAX )This change suggests it is a mixture of libtasn1 source and GRUB specific patches. Please disaggregate them.
Quoted 1 line
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 8/20 libtasn1: Add the documentation
In reply to Gary Lin via Grub-develDocument libtasn1 in docs/grub-dev.texi and add the upgrade steps. Also add the patches to make libtasn1 compatible with grub code.
--- docs/grub-dev.texi | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
docs/grub-dev.texi +33 -0
@@ -506,6 +506,7 @@ to update it. * Gnulib:: * jsmn:: * minilzo:: +* libtasn1:: @end menu @node Gnulib
@@ -596,6 +597,38 @@ cp minilzo-2.10/*.[hc] grub-core/lib/minilzo rm -r minilzo-2.10* @end example +@node libtasn1 +@section libtasn1 + +libtasn1 is a library providing Abstract Syntax Notation One (ASN.1, as +specified by the X.680 ITU-T recommendation) parsing and structures management, +and Distinguished Encoding Rules (DER, as per X.690) encoding and decoding +functions. + +To upgrade to a new version of the libtasn1 library, download the release +tarball and copy the files into the target directory: + +@example +curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz +tar xf libtasn1-4.19.0.tar.gz +rm -rf grub-core/lib/libtasn1/ +mkdir -p grub-core/lib/libtasn1/lib/ +cp libtasn1-4.19.0/@lbracechar{}README.md,COPYING@rbracechar{} grub-core/lib/libtasn1/ +cp libtasn1-4.19.0/lib/@lbracechar{}coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h@rbracechar{} grub-core/lib/libtasn1/lib/ +cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1/ +rm -rf libtasn1-4.19.0* +@end example + +After upgrading the library, it may be necessary to apply the patches in +@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with +grub. These patches were needed to use the current version of libtasn1. The +existing patches may not apply cleanly, apply at all, or even be needed for a +newer version of the library, and other patches maybe needed due to changes in +the newer version. If existing patches need to be refreshed to apply cleanly, +please include updated patches as part of the a patch set sent to the list. +If new patches are needed or existing patches are not needed, also please send +additions or removals as part of any patch set upgrading libtasn1. + @node Debugging @chapter Debugging
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 08/20] libtasn1: Add the documentation
Quoted 35 lines · Gary Lin
Document libtasn1 in docs/grub-dev.texi and add the upgrade steps. Also add the patches to make libtasn1 compatible with grub code.
Signed-off-by Gary Lin <[email protected]>Reviewed-by Vladimir Serbinenko <[email protected]>--- docs/grub-dev.texi | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)docs/grub-dev.texi +11
-0@@ -506,6 +506,7 @@ to update it. * Gnulib:: * jsmn:: * minilzo:: +* libtasn1:: @end menu @node Gnulib@@ -596,6 +597,38 @@ cp minilzo-2.10/*.[hc] grub-core/lib/minilzo rm -r minilzo-2.10* @end example +@node libtasn1 +@section libtasn1 + +libtasn1 is a library providing Abstract Syntax Notation One (ASN.1, as +specified by the X.680 ITU-T recommendation) parsing and structures management, +and Distinguished Encoding Rules (DER, as per X.690) encoding and decoding +functions. + +To upgrade to a new version of the libtasn1 library, download the release +tarball and copy the files into the target directory:
docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 619
Quoted 10 lines
docs/grub-dev.texi +10
-0+ +@example +curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz +tar xf libtasn1-4.19.0.tar.gz +rm -rf grub-core/lib/libtasn1/ +mkdir -p grub-core/lib/libtasn1/lib/ +cp libtasn1-4.19.0/@lbracechar{}README.md,COPYING@rbracechar{} grub-core/lib/libtasn1/ +cp libtasn1-4.19.0/lib/@lbracechar{}coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h@rbracechar{} grub-core/lib/libtasn1/lib/ +cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1/ +rm -rf libtasn1-4.19.0*
Please add commands for copying libtasn1 test files here too.
docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 624
Quoted 5 lines
docs/grub-dev.texi +5
-0+@end example + +After upgrading the library, it may be necessary to apply the patches in +@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with +grub. These patches were needed to use the current version of libtasn1. The
s/grub/GRUB/
docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 626
Quoted 2 lines
docs/grub-dev.texi +2
-0+existing patches may not apply cleanly, apply at all, or even be needed for a +newer version of the library, and other patches maybe needed due to changes in
s/maybe/may be/
docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 630
Quoted 4 lines
docs/grub-dev.texi +4
-0+the newer version. If existing patches need to be refreshed to apply cleanly, +please include updated patches as part of the a patch set sent to the list. +If new patches are needed or existing patches are not needed, also please send +additions or removals as part of any patch set upgrading libtasn1.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 08/20] libtasn1: Add the documentation
Quoted 62 lines · Daniel Kiper
Quoted 35 lines · Gary Lin
Document libtasn1 in docs/grub-dev.texi and add the upgrade steps. Also add the patches to make libtasn1 compatible with grub code.
Signed-off-by Gary Lin <[email protected]>Reviewed-by Vladimir Serbinenko <[email protected]>--- docs/grub-dev.texi | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)docs/grub-dev.texi +11
-0@@ -506,6 +506,7 @@ to update it. * Gnulib:: * jsmn:: * minilzo:: +* libtasn1:: @end menu @node Gnulib@@ -596,6 +597,38 @@ cp minilzo-2.10/*.[hc] grub-core/lib/minilzo rm -r minilzo-2.10* @end example +@node libtasn1 +@section libtasn1 + +libtasn1 is a library providing Abstract Syntax Notation One (ASN.1, as +specified by the X.680 ITU-T recommendation) parsing and structures management, +and Distinguished Encoding Rules (DER, as per X.690) encoding and decoding +functions. + +To upgrade to a new version of the libtasn1 library, download the release +tarball and copy the files into the target directory:docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 619
Quoted 10 lines
docs/grub-dev.texi +10
-0+ +@example +curl -L -O https://ftp.gnu.org/gnu/libtasn1/libtasn1-4.19.0.tar.gz +tar xf libtasn1-4.19.0.tar.gz +rm -rf grub-core/lib/libtasn1/ +mkdir -p grub-core/lib/libtasn1/lib/ +cp libtasn1-4.19.0/@lbracechar{}README.md,COPYING@rbracechar{} grub-core/lib/libtasn1/ +cp libtasn1-4.19.0/lib/@lbracechar{}coding.c,decoding.c,element.c,element.h,errors.c,gstr.c,gstr.h,int.h,parser_aux.c,parser_aux.h,structure.c,structure.h@rbracechar{} grub-core/lib/libtasn1/lib/ +cp libtasn1-4.19.0/lib/includes/libtasn1.h grub-core/lib/libtasn1/ +rm -rf libtasn1-4.19.0*Please add commands for copying libtasn1 test files here too.
docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 624
Quoted 5 lines
docs/grub-dev.texi +5
-0+@end example + +After upgrading the library, it may be necessary to apply the patches in +@file{grub-core/lib/libtasn1-patches/} to adjust the code to be compatible with +grub. These patches were needed to use the current version of libtasn1. Thes/grub/GRUB/
docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 626
Quoted 2 lines
docs/grub-dev.texi +2
-0+existing patches may not apply cleanly, apply at all, or even be needed for a +newer version of the library, and other patches maybe needed due to changes ins/maybe/may be/
Quoted 5 lines
docs/grub-dev.texi cp minilzo-2.10/*.[hc] grub-core/lib/minilzo · line 630
Quoted 4 lines
docs/grub-dev.texi +4
-0+the newer version. If existing patches need to be refreshed to apply cleanly, +please include updated patches as part of the a patch set sent to the list. +If new patches are needed or existing patches are not needed, also please send +additions or removals as part of any patch set upgrading libtasn1.
Thanks. Will update the commands and fix the typos in v18.
Gary Lin
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 9/20 key_protector: Add key protectors framework
In reply to Gary Lin via Grub-develFrom: Hernan Gatta <[email protected]>
A key protector encapsulates functionality to retrieve an unlocking key for a fully-encrypted disk from a specific source. A key protector module registers itself with the key protectors framework when it is loaded and unregisters when unloaded. Additionally, a key protector may accept parameters that describe how it should operate.
The key protectors framework, besides offering registration and unregistration functions, also offers a one-stop routine for finding and invoking a key protector by name. If a key protector with the specified name exists and if an unlocking key is successfully retrieved by it, the function returns to the caller the retrieved key and its length.
--- grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 5 +++ grub-core/disk/key_protector.c | 78 ++++++++++++++++++++++++++++++++++ include/grub/key_protector.h | 46 ++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 grub-core/disk/key_protector.c create mode 100644 include/grub/key_protector.h
grub-core/Makefile.am +1 -0
@@ -90,6 +90,7 @@ endif KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/mm.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/parser.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/partition.h +KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/key_protector.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/stack_protector.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/term.h KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/time.h
grub-core/Makefile.core.def +5 -0
@@ -1282,6 +1282,11 @@ module = { common = disk/raid6_recover.c; }; +module = { + name = key_protector; + common = disk/key_protector.c; +}; + module = { name = scsi; common = disk/scsi.c;
grub-core/disk/key_protector.c new file +78 -0
78 lines not on this page · raw message
include/grub/key_protector.h new file +46 -0
46 lines not on this page · raw message
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 09/20] key_protector: Add key protectors framework
Quoted 14 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A key protector encapsulates functionality to retrieve an unlocking key for a fully-encrypted disk from a specific source. A key protector module registers itself with the key protectors framework when it is loaded and unregisters when unloaded. Additionally, a key protector may accept parameters that describe how it should operate.
The key protectors framework, besides offering registration and unregistration functions, also offers a one-stop routine for finding and invoking a key protector by name. If a key protector with the specified name exists and if an unlocking key is successfully retrieved by it, the function returns to the caller the retrieved key and its length.
Quoted 4 lines
Cc Vladimir Serbinenko <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 10/20 tpm2: Add TPM Software Stack (TSS)
In reply to Gary Lin via Grub-develFrom: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions: TPM2_Create, TPM2_CreatePrimary, TPM2_EvictControl, TPM2_FlushContext, TPM2_Load, TPM2_PCR_Read, TPM2_PolicyGetDigest, TPM2_PolicyPCR, TPM2_ReadPublic, TPM2_StartAuthSession, TPM2_Unseal, TPM2_LoadExternal, TPM2_Hash, TPM2_VerifySignature, TPM2_PolicyAuthorize, TPM2_TestParms
--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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
grub-core/tpm2/buffer.c new file +145 -0
@@ -0,0 +1,145 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/misc.h> +#include <grub/tpm2/buffer.h> + +void grub_tpm2_buffer_init (grub_tpm2_buffer_t buffer) +{ +grub_memset (buffer->data,0xDD ,sizeof (buffer->data)); + buffer->size =0 ; + buffer->offset =0 ; + buffer->cap =sizeof (buffer->data); + buffer->error =0 ; +} + +void +grub_tpm2_buffer_pack (grub_tpm2_buffer_t buffer,const void * data, +grub_size_t size) +{ +grub_uint32_t r = buffer->cap - buffer->size; + +if (buffer->error) +return ; + +if (size > r) + { + buffer->error =1 ; +return ; + } + +grub_memcpy (&buffer->data[buffer->size], (void *) data, size); + buffer->size += size; +} + +void +grub_tpm2_buffer_pack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t value) +{ +grub_tpm2_buffer_pack (buffer, (const char *) &value,sizeof (value)); +} + +void +grub_tpm2_buffer_pack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t value) +{ +grub_uint16_t tmp =grub_cpu_to_be16 (value); +grub_tpm2_buffer_pack (buffer, (const char *) &tmp,sizeof (tmp)); +} + +void +grub_tpm2_buffer_pack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t value) +{ +grub_uint32_t tmp =grub_cpu_to_be32 (value); +grub_tpm2_buffer_pack (buffer, (const char *) &tmp,sizeof (tmp)); +} + +void +grub_tpm2_buffer_unpack (grub_tpm2_buffer_t buffer,void * data, +grub_size_t size) +{ +grub_uint32_t r = buffer->size - buffer->offset; + +if (buffer->error) +return ; + +if (size > r) + { + buffer->error =1 ; +return ; + } + +grub_memcpy (data, &buffer->data[buffer->offset], size); + buffer->offset += size; +} + +void +grub_tpm2_buffer_unpack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t * value) +{ +grub_uint32_t r = buffer->size - buffer->offset; + +if (buffer->error) +return ; + +if (sizeof (*value) > r) + { + buffer->error =1 ; +return ; + } + +grub_memcpy (value, &buffer->data[buffer->offset],sizeof (*value)); + buffer->offset +=sizeof (*value); +} + +void +grub_tpm2_buffer_unpack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t * value) +{ +grub_uint16_t tmp; +grub_uint32_t r = buffer->size - buffer->offset; + +if (buffer->error) +return ; + +if (sizeof (tmp) > r) + { + buffer->error =1 ; +return ; + } + +grub_memcpy (&tmp, &buffer->data[buffer->offset],sizeof (tmp)); + buffer->offset +=sizeof (tmp); + *value =grub_be_to_cpu16 (tmp); +} + +void +grub_tpm2_buffer_unpack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t * value) +{ +grub_uint32_t tmp; +grub_uint32_t r = buffer->size - buffer->offset; + +if (buffer->error) +return ; + +if (sizeof (tmp) > r) + { + buffer->error =1 ; +return ; + } + +grub_memcpy (&tmp, &buffer->data[buffer->offset],sizeof (tmp)); + buffer->offset +=sizeof (tmp); + *value =grub_be_to_cpu32 (tmp); +}
grub-core/tpm2/mu.c new file +1168 -0
1,168 lines not on this page · raw message
grub-core/tpm2/tcg2.c new file +143 -0
143 lines not on this page · raw message
grub-core/tpm2/tpm2.c new file +1048 -0
1,048 lines not on this page · raw message
include/grub/tpm2/buffer.h new file +65 -0
65 lines not on this page · raw message
include/grub/tpm2/internal/functions.h new file +156 -0
156 lines not on this page · raw message
include/grub/tpm2/internal/structs.h new file +768 -0
275 lines not on this page · raw message
include/grub/tpm2/internal/types.h new file +403 -0
403 lines not on this page · raw message
include/grub/tpm2/mu.h new file +396 -0
396 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Part of the diff is not shown here. View the raw message.
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
grub-core/tpm2/buffer.c
Quoted 62 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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.hgrub-core/tpm2/buffer.c new file +0
-0
I think this together with other TPM2 driver files should go to the grub-core/commands/efi/tpm2 directory.
Quoted 15 lines
grub-core/tpm2/buffer.c new file +14
-0@@ -0,0 +1,145 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*
grub-core/tpm2/buffer.c line 24
Quoted 10 lines
grub-core/tpm2/buffer.c new file +10
-0+* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/misc.h> +#include <grub/tpm2/buffer.h> + +void grub_tpm2_buffer_init (grub_tpm2_buffer_t buffer) +{ +grub_memset (buffer->data,0xDD ,sizeof (buffer->data));
If you init the buffer->data with 0xDD instead of 0 then it begs for a comment. And s/0xDD/0xdd/...
Quoted 25 lines
grub-core/tpm2/buffer.c new file +25
-0+ buffer->size =0 ; + buffer->offset =0 ; + buffer->cap =sizeof (buffer->data); + buffer->error =0 ; +} + +void +grub_tpm2_buffer_pack (grub_tpm2_buffer_t buffer,const void * data, +grub_size_t size) +{ +grub_uint32_t r = buffer->cap - buffer->size; + +if (buffer->error) +return ; + +if (size > r) + { + buffer->error =1 ; +return ; + } + +grub_memcpy (&buffer->data[buffer->size], (void *) data, size); + buffer->size += size; +} +
grub-core/tpm2/buffer.c line 59
Quoted 10 lines
grub-core/tpm2/buffer.c new file +10
-0+void +grub_tpm2_buffer_pack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t value) +{ +grub_tpm2_buffer_pack (buffer, (const char *) &value,sizeof (value)); +} + +void +grub_tpm2_buffer_pack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t value) +{ +grub_uint16_t tmp =grub_cpu_to_be16 (value);
I would add an empty line here.
grub-core/tpm2/buffer.c line 66
Quoted 7 lines
grub-core/tpm2/buffer.c new file +7
-0+grub_tpm2_buffer_pack (buffer, (const char *) &tmp,sizeof (tmp)); +} + +void +grub_tpm2_buffer_pack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t value) +{ +grub_uint32_t tmp =grub_cpu_to_be32 (value);
Ditto and below please if needed...
Quoted 2 lines
grub-core/tpm2/buffer.c new file +2
-0+grub_tpm2_buffer_pack (buffer, (const char *) &tmp,sizeof (tmp)); +}
[...]
grub-core/tpm2/mu.c
Quoted 5 lines
grub-core/tpm2/mu.c new file +0
-0
I can imagine where it comes from but I think it should be efi.c instead of mu.c.
Quoted 15 lines
grub-core/tpm2/mu.c new file +14
-0@@ -0,0 +1,1168 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*
grub-core/tpm2/mu.c line 24
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/misc.h> +#include <grub/tpm2/mu.h> + +void +grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_AUTH_COMMAND * authCommand)
s/grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal/grub_efi_tpm2_TPMS_AUTH_COMMAND_Marshal/
I would change "grub_tpm2_mu_" prefix to "grub_efi_tpm2_" everywhere.
Quoted 19 lines
grub-core/tpm2/mu.c new file +19
-0+{ +grub_uint32_t start; +grub_uint32_t tmp; + +grub_tpm2_buffer_pack_u32 (buffer,0 ); + start = buffer->size; + +grub_tpm2_buffer_pack_u32 (buffer, authCommand->sessionHandle); + +grub_tpm2_buffer_pack_u16 (buffer, authCommand->nonce.size); +grub_tpm2_buffer_pack (buffer, authCommand->nonce.buffer, + authCommand->nonce.size); + +grub_tpm2_buffer_pack_u8 (buffer, + *((const grub_uint8_t *) &authCommand->sessionAttributes)); + +grub_tpm2_buffer_pack_u16 (buffer, authCommand->hmac.size); +grub_tpm2_buffer_pack (buffer, authCommand->hmac.buffer, + authCommand->hmac.size);
grub-core/tpm2/mu.c line 53
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+ + tmp =grub_cpu_to_be32 (buffer->size - start); +grub_memcpy (&buffer->data[start -sizeof (grub_uint32_t)], &tmp, +sizeof (tmp)); +} + +void +grub_tpm2_mu_TPM2B_Marshal (grub_tpm2_buffer_t buffer, +const grub_uint16_t size, +const grub_uint8_t * b)
s/grub_tpm2_mu_TPM2B_Marshal/grub_efi_tpm2_TPM2B_Marshal/ and similar changes below please...
Quoted 6 lines
grub-core/tpm2/mu.c new file +6
-0+{ +grub_tpm2_buffer_pack_u16 (buffer, size); + +for (grub_uint16_t i =0 ; i < size; i++) +grub_tpm2_buffer_pack_u8 (buffer, b[i]); +}
[...]
grub-core/tpm2/mu.c line 577
Quoted 3 lines
grub-core/tpm2/mu.c new file +3
-0+static void +__tpm2_mu_TPM2B_BUFFER_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B * p,grub_uint16_t bound)
I think this...
Quoted 4 lines
grub-core/tpm2/mu.c new file +4
-0+{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->size); + +if (p->size > bound)
grub-core/tpm2/mu.c line 591
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+ { + buffer->error =1 ; +return ; + } + +grub_tpm2_buffer_unpack (buffer, &p->buffer, p->size); +} + +#define TPM2B_BUFFER_UNMARSHAL (buffer, type, data) \ + __tpm2_mu_TPM2B_BUFFER_Unmarshal(buffer, (TPM2B *)data, sizeof(type) - sizeof(grub_uint16_t))
... and this...
grub-core/tpm2/mu.c line 595
Quoted 4 lines
grub-core/tpm2/mu.c new file +4
-0+ +void +grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_AUTH_RESPONSE * p)
... should go behind this...
Quoted 32 lines
grub-core/tpm2/mu.c new file +32
-0+{ +grub_uint8_t tmp; +grub_uint32_t tmp32; + +grub_tpm2_buffer_unpack_u16 (buffer, &p->nonce.size); + +if (p->nonce.size) +grub_tpm2_buffer_unpack (buffer, &p->nonce.buffer, p->nonce.size); + +grub_tpm2_buffer_unpack_u8 (buffer, &tmp); + tmp32 = tmp; +grub_memcpy (&p->sessionAttributes, &tmp32,sizeof (grub_uint32_t)); + +grub_tpm2_buffer_unpack_u16 (buffer, &p->hmac.size); + +if (p->hmac.size) +grub_tpm2_buffer_unpack (buffer, &p->hmac.buffer, p->hmac.size); +} + +void +grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_DIGEST * digest) +{ +TPM2B_BUFFER_UNMARSHAL (buffer,TPM2B_DIGEST , digest); +} + +void +grub_tpm2_mu_TPM2B_NONCE_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_NONCE * nonce) +{ +TPM2B_BUFFER_UNMARSHAL (buffer,TPM2B_NONCE , nonce); +}
[...]
Quoted 26 lines
grub-core/tpm2/tcg2.c new file +20
-0@@ -0,0 +1,143 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/efi/api.h> +#include <grub/efi/efi.h>
grub-core/tpm2/tcg2.c line 30
Quoted 10 lines
grub-core/tpm2/tcg2.c new file +10
-0+#include <grub/efi/tpm.h> +#include <grub/mm.h> +#include <grub/tpm2/tcg2.h> + +static grub_err_t +grub_tcg2_get_caps (grub_efi_tpm2_protocol_t *protocol,int *tpm2, +grub_size_t *max_output_size) +{ +grub_efi_status_t status; +
I would drop this empty line. Please fix similar things in the other places too.
Quoted 6 lines
grub-core/tpm2/tcg2.c new file +6
-0+static int has_caps =0 ; +static EFI_TCG2_BOOT_SERVICE_CAPABILITY caps = + { + .Size = (grub_uint8_t )sizeof (caps) + }; +
grub-core/tpm2/tcg2.c line 46
Quoted 10 lines
grub-core/tpm2/tcg2.c new file +10
-0+if (has_caps) + goto exit; + + status = protocol->get_capability (protocol, &caps); +if (status !=GRUB_EFI_SUCCESS || !caps.TPMPresentFlag) +return GRUB_ERR_FILE_NOT_FOUND ; + + has_caps =1 ; + +exit:
Missing space before label. Please fix similar things in the other places too.
grub-core/tpm2/tcg2.c line 47
Quoted 1 line
grub-core/tpm2/tcg2.c new file +1
-0+if (tpm2)
I prefer
if (tpm2 != NULL)
grub-core/tpm2/tcg2.c line 49
Quoted 2 lines
grub-core/tpm2/tcg2.c new file +2
-0+ *tpm2 = caps.TPMPresentFlag; +if (max_output_size)
Ditto... Especially here short form can be confusing... Please fix similar problems everywhere.
Quoted 46 lines
grub-core/tpm2/tcg2.c new file +46
-0+ *max_output_size = caps.MaxResponseSize; + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_tcg2_get_protocol (grub_efi_tpm2_protocol_t **protocol) +{ +static grub_guid_t tpm2_guid =EFI_TPM2_GUID ; +static grub_efi_tpm2_protocol_t *tpm2_protocol =NULL ; + +int tpm2; +grub_efi_handle_t *handles; +grub_efi_uintn_t num_handles; +grub_efi_handle_t tpm2_handle; +grub_err_t err =GRUB_ERR_FILE_NOT_FOUND ; + +if (tpm2_protocol) + { + *protocol = tpm2_protocol; +return GRUB_ERR_NONE ; + } + + handles =grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL , &tpm2_guid,NULL , + &num_handles); +if (!handles || !num_handles) +return err; + + tpm2_handle = handles[0 ]; + + tpm2_protocol =grub_efi_open_protocol (tpm2_handle, &tpm2_guid, +GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL ); +if (!tpm2_protocol) + goto exit; + + err =grub_tcg2_get_caps (tpm2_protocol, &tpm2,NULL ); +if (err || !tpm2) + goto exit; + + *protocol = tpm2_protocol; + err =GRUB_ERR_NONE ; + +exit: +grub_free (handles); +return err; +}
[...]
Quoted 28 lines
include/grub/tpm2/buffer.h new file +22
-0@@ -0,0 +1,65 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_BUFFER_HEADER +#define GRUB_TPM2_BUFFER_HEADER 1 + +#include <grub/types.h>
include/grub/tpm2/buffer.h line 32
Quoted 10 lines
include/grub/tpm2/buffer.h new file +10
-0+ +#define GRUB_TPM2_BUFFER_CAPACITY 4096 + +struct grub_tpm2_buffer +{ +grub_uint8_t data[GRUB_TPM2_BUFFER_CAPACITY ]; +grub_size_t size; +grub_size_t offset; +grub_size_t cap; +int error;
The int does not look right for me here.
Quoted 21 lines
include/grub/tpm2/buffer.h new file +21
-0+}; +typedef struct grub_tpm2_buffer *grub_tpm2_buffer_t ; + +void +grub_tpm2_buffer_init (grub_tpm2_buffer_t buffer); + +void +grub_tpm2_buffer_pack (grub_tpm2_buffer_t buffer,const void * data, +grub_size_t size); + +void +grub_tpm2_buffer_pack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t value); + +void +grub_tpm2_buffer_pack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t value); + +void +grub_tpm2_buffer_pack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t value); + +void +grub_tpm2_buffer_unpack (grub_tpm2_buffer_t buffer, void* data,
include/grub/tpm2/buffer.h line 63
Quoted 10 lines
include/grub/tpm2/buffer.h new file +10
-0+ grub_size_t size); + +void +grub_tpm2_buffer_unpack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t * value); + +void +grub_tpm2_buffer_unpack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t * value); + +void +grub_tpm2_buffer_unpack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t * value);
Is not all that stuff internal thing? If yes it should not go to the include/grub/tpm2 dir.
[...]
Quoted 29 lines
include/grub/tpm2/internal/structs.h new file +23
-0@@ -0,0 +1,768 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_INTERNAL_STRUCTS_HEADER +#define GRUB_TPM2_INTERNAL_STRUCTS_HEADER 1 + +#include <grub/tpm2/internal/types.h> +
[...]
include/grub/tpm2/internal/structs.h line 133
Quoted 11 lines
include/grub/tpm2/internal/structs.h new file +11
-0+/* TPMA_SESSION Structure */ +struct TPMA_SESSION +{ +unsigned int continueSession:1 ; +unsigned int auditExclusive:1 ; +unsigned int auditReset:1 ; +unsigned int reserved1:2 ; +unsigned int decrypt:1 ; +unsigned int encrypt:1 ; +unsigned int audit:1 ; +unsigned int reserved:24 ;
s/unsigned int/grub_uint32_t/?
Quoted 2 lines
include/grub/tpm2/internal/structs.h new file +2
-0+}; +typedef struct TPMA_SESSION TPMA_SESSION ;
[...]
Quoted 9 lines
include/grub/tpm2/internal/structs.h new file +9
-0+/* TPMA_OBJECT Structure */ +struct TPMA_OBJECT +{ +unsigned int reserved1:1 ; +unsigned int fixedTPM:1 ; +unsigned int stClear:1 ; +unsigned int reserved2:1 ; +unsigned int fixedParent:1 ; +unsigned int sensitiveDataOrigin:1 ;
include/grub/tpm2/internal/structs.h line 201
Quoted 10 lines
include/grub/tpm2/internal/structs.h new file +10
-0+ unsigned int userWithAuth:1; + unsigned int adminWithPolicy:1; + unsigned int reserved3:2; + unsigned int noDA:1; + unsigned int encryptedDuplication:1; + unsigned int reserved4:4; + unsigned int restricted:1; + unsigned int decrypt:1; + unsigned int sign:1; + unsigned int reserved5:13;
s/unsigned int/grub_uint64_t/?
Quoted 2 lines
include/grub/tpm2/internal/structs.h new file +2
-0+}; +typedef struct TPMA_OBJECT TPMA_OBJECT ;
[...]
include/grub/tpm2/internal/structs.h line 501
Quoted 9 lines
include/grub/tpm2/internal/structs.h new file +9
-0+/* TPMA_LOCALITY Structure */ +struct TPMA_LOCALITY +{ +unsigned char TPM_LOC_ZERO:1 ; +unsigned char TPM_LOC_ONE:1 ; +unsigned char TPM_LOC_TWO:1 ; +unsigned char TPM_LOC_THREE:1 ; +unsigned char TPM_LOC_FOUR:1 ; +unsigned char Extended:3 ;
s/unsigned char/grub_uint8_t/?
And similar things below...
Quoted 2 lines
include/grub/tpm2/internal/structs.h new file +2
-0+}; +typedef struct TPMA_LOCALITY TPMA_LOCALITY ;
[...]
Quoted 46 lines
include/grub/tpm2/tcg2.h new file +34
-0@@ -0,0 +1,34 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_TCG2_HEADER +#define GRUB_TPM2_TCG2_HEADER 1 + +#include <grub/err.h> +#include <grub/types.h> + +grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size); + +grub_err_t +grub_tcg2_submit_command (grub_size_t input_size, +grub_uint8_t *input, +grub_size_t output_size, +grub_uint8_t *output); + +#endif /* ! GRUB_TPM2_TCG2_HEADER */ include/grub/tpm2/tpm2.h new file +0
-0
include/grub/tpm2/tpm2.h line 3
Quoted 3 lines
include/grub/tpm2/tpm2.h new file +3
-0@@ -0,0 +1,34 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation
I think you should add
Copyright (C) 2024 Free Software Foundation, Inc.
everywhere in this patch. Maybe in the others too.
Quoted 10 lines
include/grub/tpm2/tpm2.h new file +10
-0+* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*
include/grub/tpm2/tpm2.h line 24
Quoted 10 lines
include/grub/tpm2/tpm2.h new file +10
-0+* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_TPM2_HEADER +#define GRUB_TPM2_TPM2_HEADER 1 + +#include <grub/tpm2/internal/types.h> +#include <grub/tpm2/internal/structs.h> +#include <grub/tpm2/internal/functions.h>
You put all three headers into "internal" directory and then you make all that stuff public. Something is off here. Please export to the rest of the GRUB only minimal set of TPM2 functions. Additionally, all names, functions, types, structs, variables, constants, etc. exported outside of a compilation unit should be prefixed with "grub_" or "GRUB_" respectively. All typedefs should end with "_t". In general please be in line with the GRUB coding style.
include/grub/tpm2/tpm2.h line 29
Quoted 4 lines
include/grub/tpm2/tpm2.h new file +4
-0+/* Well-Known Windows SRK handle */ +#define TPM2_SRK_HANDLE 0x81000001 + +typedef struct TPM2_SEALED_KEY {
Please be inline with the GRUB coding style. So, define struct first and then typedef.
include/grub/tpm2/tpm2.h line 32
Quoted 3 lines
include/grub/tpm2/tpm2.h new file +3
-0+TPM2B_PUBLIC public; +TPM2B_PRIVATE private; +}TPM2_SEALED_KEY ;
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
Quoted 58 lines · Daniel Kiper
Quoted 57 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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
Quoted 9 lines
grub-core/tpm2/buffer.c
Quoted 5 lines
grub-core/tpm2/buffer.c new file +0
-0I think this together with other TPM2 driver files should go to the grub-core/commands/efi/tpm2 directory.
The TPM2 stack is not EFI only. The only EFI related code is in grub-core/tpm2/tcg2.c which mainly implements how the TPM2 commands to be submitted. I'd propose to move them to grub-core/commands/tpm2 and rename tcg2.c to tcg2-efi.c.
Quoted 20 lines
Quoted 20 lines
grub-core/tpm2/buffer.c new file +19
-0@@ -0,0 +1,145 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/misc.h>
Quoted 9 lines
grub-core/tpm2/buffer.c line 24
Quoted 5 lines
grub-core/tpm2/buffer.c new file +5
-0+#include <grub/tpm2/buffer.h> + +void grub_tpm2_buffer_init (grub_tpm2_buffer_t buffer) +{ +grub_memset (buffer->data,0xDD ,sizeof (buffer->data));If you init the
buffer->datawith0xDDinstead of 0 then it begs for a comment. And s/0xDD/0xdd/...
It should be 0. I'll fix in v18.
Quoted 38 lines
Quoted 25 lines
grub-core/tpm2/buffer.c new file +25
-0+ buffer->size =0 ; + buffer->offset =0 ; + buffer->cap =sizeof (buffer->data); + buffer->error =0 ; +} + +void +grub_tpm2_buffer_pack (grub_tpm2_buffer_t buffer,const void * data, +grub_size_t size) +{ +grub_uint32_t r = buffer->cap - buffer->size; + +if (buffer->error) +return ; + +if (size > r) + { + buffer->error =1 ; +return ; + } + +grub_memcpy (&buffer->data[buffer->size], (void *) data, size); + buffer->size += size; +} +grub-core/tpm2/buffer.c line 59
Quoted 10 lines
grub-core/tpm2/buffer.c new file +10
-0+void +grub_tpm2_buffer_pack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t value) +{ +grub_tpm2_buffer_pack (buffer, (const char *) &value,sizeof (value)); +} + +void +grub_tpm2_buffer_pack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t value) +{ +grub_uint16_t tmp =grub_cpu_to_be16 (value);I would add an empty line here.
Quoted 10 lines
grub-core/tpm2/buffer.c line 66
Quoted 7 lines
grub-core/tpm2/buffer.c new file +7
-0+grub_tpm2_buffer_pack (buffer, (const char *) &tmp,sizeof (tmp)); +} + +void +grub_tpm2_buffer_pack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t value) +{ +grub_uint32_t tmp =grub_cpu_to_be32 (value);Ditto and below please if needed...
I'll go through the whole patch and fix the similar issue.
Quoted 5 lines
Quoted 2 lines
grub-core/tpm2/buffer.c new file +2
-0+grub_tpm2_buffer_pack (buffer, (const char *) &tmp,sizeof (tmp)); +}[...]
Quoted 9 lines
grub-core/tpm2/mu.c
Quoted 5 lines
grub-core/tpm2/mu.c new file +0
-0I can imagine where it comes from but I think it should be
efi.cinstead ofmu.c.
No, it's not from the MU firmware but stands for Marshal/Unmarshal. The similar naming policy from tpm2-tss:
https://github.com/tpm2-software/tpm2-tss/blob/master/include/tss2/tss2_mu.h
Quoted 28 lines
Quoted 15 lines
grub-core/tpm2/mu.c new file +14
-0@@ -0,0 +1,1168 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* grub-core/tpm2/mu.c line 24
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/misc.h> +#include <grub/tpm2/mu.h> + +void +grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_AUTH_COMMAND * authCommand)s/grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal/grub_efi_tpm2_TPMS_AUTH_COMMAND_Marshal/
Quoted 2 lines
I would change "
grub_tpm2_mu_" prefix to "grub_efi_tpm2_" everywhere.
As mentioned above, it's not EFI related, so the renaming is not necessary.
Quoted 138 lines
Quoted 19 lines
grub-core/tpm2/mu.c new file +19
-0+{ +grub_uint32_t start; +grub_uint32_t tmp; + +grub_tpm2_buffer_pack_u32 (buffer,0 ); + start = buffer->size; + +grub_tpm2_buffer_pack_u32 (buffer, authCommand->sessionHandle); + +grub_tpm2_buffer_pack_u16 (buffer, authCommand->nonce.size); +grub_tpm2_buffer_pack (buffer, authCommand->nonce.buffer, + authCommand->nonce.size); + +grub_tpm2_buffer_pack_u8 (buffer, + *((const grub_uint8_t *) &authCommand->sessionAttributes)); + +grub_tpm2_buffer_pack_u16 (buffer, authCommand->hmac.size); +grub_tpm2_buffer_pack (buffer, authCommand->hmac.buffer, + authCommand->hmac.size);grub-core/tpm2/mu.c line 53
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+ + tmp =grub_cpu_to_be32 (buffer->size - start); +grub_memcpy (&buffer->data[start -sizeof (grub_uint32_t)], &tmp, +sizeof (tmp)); +} + +void +grub_tpm2_mu_TPM2B_Marshal (grub_tpm2_buffer_t buffer, +const grub_uint16_t size, +const grub_uint8_t * b)s/grub_tpm2_mu_TPM2B_Marshal/grub_efi_tpm2_TPM2B_Marshal/ and similar changes below please...
Quoted 6 lines
grub-core/tpm2/mu.c new file +6
-0+{ +grub_tpm2_buffer_pack_u16 (buffer, size); + +for (grub_uint16_t i =0 ; i < size; i++) +grub_tpm2_buffer_pack_u8 (buffer, b[i]); +}[...]
grub-core/tpm2/mu.c line 577
Quoted 3 lines
grub-core/tpm2/mu.c new file +3
-0+static void +__tpm2_mu_TPM2B_BUFFER_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B * p,grub_uint16_t bound)I think this...
Quoted 4 lines
grub-core/tpm2/mu.c new file +4
-0+{ +grub_tpm2_buffer_unpack_u16 (buffer, &p->size); + +if (p->size > bound)grub-core/tpm2/mu.c line 591
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+ { + buffer->error =1 ; +return ; + } + +grub_tpm2_buffer_unpack (buffer, &p->buffer, p->size); +} + +#define TPM2B_BUFFER_UNMARSHAL (buffer, type, data) \ + __tpm2_mu_TPM2B_BUFFER_Unmarshal(buffer, (TPM2B *)data, sizeof(type) - sizeof(grub_uint16_t))... and this...
grub-core/tpm2/mu.c line 595
Quoted 4 lines
grub-core/tpm2/mu.c new file +4
-0+ +void +grub_tpm2_mu_TPMS_AUTH_RESPONSE_Unmarshal (grub_tpm2_buffer_t buffer, +TPMS_AUTH_RESPONSE * p)... should go behind this...
Quoted 32 lines
grub-core/tpm2/mu.c new file +32
-0+{ +grub_uint8_t tmp; +grub_uint32_t tmp32; + +grub_tpm2_buffer_unpack_u16 (buffer, &p->nonce.size); + +if (p->nonce.size) +grub_tpm2_buffer_unpack (buffer, &p->nonce.buffer, p->nonce.size); + +grub_tpm2_buffer_unpack_u8 (buffer, &tmp); + tmp32 = tmp; +grub_memcpy (&p->sessionAttributes, &tmp32,sizeof (grub_uint32_t)); + +grub_tpm2_buffer_unpack_u16 (buffer, &p->hmac.size); + +if (p->hmac.size) +grub_tpm2_buffer_unpack (buffer, &p->hmac.buffer, p->hmac.size); +} + +void +grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_DIGEST * digest) +{ +TPM2B_BUFFER_UNMARSHAL (buffer,TPM2B_DIGEST , digest); +} + +void +grub_tpm2_mu_TPM2B_NONCE_Unmarshal (grub_tpm2_buffer_t buffer, +TPM2B_NONCE * nonce) +{ +TPM2B_BUFFER_UNMARSHAL (buffer,TPM2B_NONCE , nonce); +}[...]
Quoted 31 lines
grub-core/tpm2/tcg2.c new file +25
-0@@ -0,0 +1,143 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/efi/api.h> +#include <grub/efi/efi.h> +#include <grub/efi/tpm.h> +#include <grub/mm.h> +#include <grub/tpm2/tcg2.h> + +static grub_err_t
Quoted 9 lines
grub-core/tpm2/tcg2.c line 30
Quoted 5 lines
grub-core/tpm2/tcg2.c new file +5
-0+grub_tcg2_get_caps (grub_efi_tpm2_protocol_t *protocol, int *tpm2, + grub_size_t *max_output_size) +{ +grub_efi_status_t status; +I would drop this empty line. Please fix similar things in the other places too.
Sure. I'll check the patch to remove the unnecessary lines.
Quoted 11 lines
Quoted 11 lines
grub-core/tpm2/tcg2.c new file +11
-0+static int has_caps =0 ; +static EFI_TCG2_BOOT_SERVICE_CAPABILITY caps = + { + .Size = (grub_uint8_t )sizeof (caps) + }; + +if (has_caps) + goto exit; + + status = protocol->get_capability (protocol, &caps); +if (status !=GRUB_EFI_SUCCESS || !caps.TPMPresentFlag)
Quoted 9 lines
grub-core/tpm2/tcg2.c line 46
Quoted 5 lines
grub-core/tpm2/tcg2.c new file +5
-0+return GRUB_ERR_FILE_NOT_FOUND ; + + has_caps =1 ; + +exit:Missing space before label. Please fix similar things in the other places too.
Ok, I'll add the missing spaces to the labels.
Quoted 11 lines
grub-core/tpm2/tcg2.c line 47
Quoted 1 line
grub-core/tpm2/tcg2.c new file +1
-0+if (tpm2)I prefer
if (tpm2 != NULL)grub-core/tpm2/tcg2.c line 49
Quoted 2 lines
grub-core/tpm2/tcg2.c new file +2
-0+ *tpm2 = caps.TPMPresentFlag; +if (max_output_size)Ditto... Especially here short form can be confusing... Please fix similar problems everywhere.
Got it.
Quoted 81 lines
Quoted 46 lines
grub-core/tpm2/tcg2.c new file +46
-0+ *max_output_size = caps.MaxResponseSize; + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_tcg2_get_protocol (grub_efi_tpm2_protocol_t **protocol) +{ +static grub_guid_t tpm2_guid =EFI_TPM2_GUID ; +static grub_efi_tpm2_protocol_t *tpm2_protocol =NULL ; + +int tpm2; +grub_efi_handle_t *handles; +grub_efi_uintn_t num_handles; +grub_efi_handle_t tpm2_handle; +grub_err_t err =GRUB_ERR_FILE_NOT_FOUND ; + +if (tpm2_protocol) + { + *protocol = tpm2_protocol; +return GRUB_ERR_NONE ; + } + + handles =grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL , &tpm2_guid,NULL , + &num_handles); +if (!handles || !num_handles) +return err; + + tpm2_handle = handles[0 ]; + + tpm2_protocol =grub_efi_open_protocol (tpm2_handle, &tpm2_guid, +GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL ); +if (!tpm2_protocol) + goto exit; + + err =grub_tcg2_get_caps (tpm2_protocol, &tpm2,NULL ); +if (err || !tpm2) + goto exit; + + *protocol = tpm2_protocol; + err =GRUB_ERR_NONE ; + +exit: +grub_free (handles); +return err; +}[...]
Quoted 32 lines
include/grub/tpm2/buffer.h new file +26
-0@@ -0,0 +1,65 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_BUFFER_HEADER +#define GRUB_TPM2_BUFFER_HEADER 1 + +#include <grub/types.h> + +#define GRUB_TPM2_BUFFER_CAPACITY 4096 + +struct grub_tpm2_buffer
Quoted 8 lines
include/grub/tpm2/buffer.h line 32
Quoted 6 lines
include/grub/tpm2/buffer.h new file +6
-0+{ +grub_uint8_t data[GRUB_TPM2_BUFFER_CAPACITY ]; +grub_size_t size; +grub_size_t offset; +grub_size_t cap; +int error;The int does not look right for me here.
Yeah, grub_bool_t is better for this variable.
Quoted 27 lines
Quoted 27 lines
include/grub/tpm2/buffer.h new file +26
-0+}; +typedef struct grub_tpm2_buffer *grub_tpm2_buffer_t ; + +void +grub_tpm2_buffer_init (grub_tpm2_buffer_t buffer); + +void +grub_tpm2_buffer_pack (grub_tpm2_buffer_t buffer,const void * data, +grub_size_t size); + +void +grub_tpm2_buffer_pack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t value); + +void +grub_tpm2_buffer_pack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t value); + +void +grub_tpm2_buffer_pack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t value); + +void +grub_tpm2_buffer_unpack (grub_tpm2_buffer_t buffer,void * data, +grub_size_t size); + +void +grub_tpm2_buffer_unpack_u8 (grub_tpm2_buffer_t buffer,grub_uint8_t * value); +
Quoted 9 lines
include/grub/tpm2/buffer.h line 63
Quoted 5 lines
include/grub/tpm2/buffer.h new file +5
-0+void +grub_tpm2_buffer_unpack_u16 (grub_tpm2_buffer_t buffer,grub_uint16_t * value); + +void +grub_tpm2_buffer_unpack_u32 (grub_tpm2_buffer_t buffer,grub_uint32_t * value);Is not all that stuff internal thing? If yes it should not go to the
include/grub/tpm2dir.
Currently, the tpm2 module is the only user. I'll move them to the tpm2 dir.
Quoted 34 lines
[...]
Quoted 29 lines
include/grub/tpm2/internal/structs.h new file +23
-0@@ -0,0 +1,768 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_INTERNAL_STRUCTS_HEADER +#define GRUB_TPM2_INTERNAL_STRUCTS_HEADER 1 + +#include <grub/tpm2/internal/types.h> +[...]
Quoted 14 lines
include/grub/tpm2/internal/structs.h line 133
Quoted 11 lines
include/grub/tpm2/internal/structs.h new file +11
-0+/* TPMA_SESSION Structure */ +struct TPMA_SESSION +{ +unsigned int continueSession:1 ; +unsigned int auditExclusive:1 ; +unsigned int auditReset:1 ; +unsigned int reserved1:2 ; +unsigned int decrypt:1 ; +unsigned int encrypt:1 ; +unsigned int audit:1 ; +unsigned int reserved:24 ;s/unsigned int/grub_uint32_t/?
Hmmm, I overlooked this struct. Per TPM2 spec, TPMA_SESSION is actually UINT8. However, the unpack function covered the mistake so the TPM2 commands still work. Will fix it in v18.
Quoted 18 lines
Quoted 2 lines
include/grub/tpm2/internal/structs.h new file +2
-0+}; +typedef struct TPMA_SESSION TPMA_SESSION ;[...]
Quoted 13 lines
include/grub/tpm2/internal/structs.h new file +13
-0+/* TPMA_OBJECT Structure */ +struct TPMA_OBJECT +{ +unsigned int reserved1:1 ; +unsigned int fixedTPM:1 ; +unsigned int stClear:1 ; +unsigned int reserved2:1 ; +unsigned int fixedParent:1 ; +unsigned int sensitiveDataOrigin:1 ; +unsigned int userWithAuth:1 ; +unsigned int adminWithPolicy:1 ; +unsigned int reserved3:2 ; +unsigned int noDA:1 ;
Quoted 9 lines
include/grub/tpm2/internal/structs.h line 201
Quoted 6 lines
include/grub/tpm2/internal/structs.h new file +6
-0+ unsigned int encryptedDuplication:1; + unsigned int reserved4:4; + unsigned int restricted:1; + unsigned int decrypt:1; + unsigned int sign:1; + unsigned int reserved5:13;s/unsigned int/grub_uint64_t/?
It's UINT32. Will fix it in v18.
Quoted 17 lines
Quoted 2 lines
include/grub/tpm2/internal/structs.h new file +2
-0+}; +typedef struct TPMA_OBJECT TPMA_OBJECT ;[...]
include/grub/tpm2/internal/structs.h line 501
Quoted 9 lines
include/grub/tpm2/internal/structs.h new file +9
-0+/* TPMA_LOCALITY Structure */ +struct TPMA_LOCALITY +{ +unsigned char TPM_LOC_ZERO:1 ; +unsigned char TPM_LOC_ONE:1 ; +unsigned char TPM_LOC_TWO:1 ; +unsigned char TPM_LOC_THREE:1 ; +unsigned char TPM_LOC_FOUR:1 ; +unsigned char Extended:3 ;s/unsigned char/grub_uint8_t/?
Quoted 2 lines
And similar things below...
Ok, I'll check the header to fix those types...
Quoted 50 lines
Quoted 2 lines
include/grub/tpm2/internal/structs.h new file +2
-0+}; +typedef struct TPMA_LOCALITY TPMA_LOCALITY ;[...]
Quoted 45 lines
include/grub/tpm2/tcg2.h new file +34
-0@@ -0,0 +1,34 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_TCG2_HEADER +#define GRUB_TPM2_TCG2_HEADER 1 + +#include <grub/err.h> +#include <grub/types.h> + +grub_err_t +grub_tcg2_get_max_output_size (grub_size_t *size); + +grub_err_t +grub_tcg2_submit_command (grub_size_t input_size, +grub_uint8_t *input, +grub_size_t output_size, +grub_uint8_t *output); + +#endif /* ! GRUB_TPM2_TCG2_HEADER */ include/grub/tpm2/tpm2.h new file +0
-0
Quoted 9 lines
include/grub/tpm2/tpm2.h line 3
Quoted 4 lines
include/grub/tpm2/tpm2.h new file +3
-0@@ -0,0 +1,34 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation I think you should add
Copyright (C) 2024 Free Software Foundation, Inc.
everywhere in this patch. Maybe in the others too.
Will do that in v18.
Quoted 29 lines
Quoted 10 lines
include/grub/tpm2/tpm2.h new file +10
-0+* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* include/grub/tpm2/tpm2.h line 24
Quoted 10 lines
include/grub/tpm2/tpm2.h new file +10
-0+* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef GRUB_TPM2_TPM2_HEADER +#define GRUB_TPM2_TPM2_HEADER 1 + +#include <grub/tpm2/internal/types.h> +#include <grub/tpm2/internal/structs.h> +#include <grub/tpm2/internal/functions.h> You put all three headers into "internal" directory and then you make all that stuff public. Something is off here. Please export to the rest of the GRUB only minimal set of TPM2 functions. Additionally, all names, functions, types, structs, variables, constants, etc. exported outside of a compilation unit should be prefixed with "
grub_" or "GRUB_" respectively. All typedefs should end with "_t". In general please be in line with the GRUB coding style.
Quoted 8 lines
include/grub/tpm2/tpm2.h line 29
Quoted 4 lines
include/grub/tpm2/tpm2.h new file +4
-0+/* Well-Known Windows SRK handle */ +#define TPM2_SRK_HANDLE 0x81000001 + +typedef struct TPM2_SEALED_KEY {Please be inline with the GRUB coding style. So, define struct first and then typedef.
Ok, I'll fix them.
Gary Lin
Quoted 5 lines
include/grub/tpm2/tpm2.h line 32
Quoted 3 lines
include/grub/tpm2/tpm2.h new file +3
-0+TPM2B_PUBLIC public; +TPM2B_PRIVATE private; +}TPM2_SEALED_KEY ;Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
Quoted 68 lines · Gary Lin
Quoted 58 lines · Daniel Kiper
Quoted 57 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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.hQuoted 9 lines
grub-core/tpm2/buffer.c
Quoted 5 lines
grub-core/tpm2/buffer.c new file +0
-0I think this together with other TPM2 driver files should go to the grub-core/commands/efi/tpm2 directory.
Quoted 1 line
The TPM2 stack is not EFI only. The only EFI related code is in
Ah, right... Then I think we should have two GRUB modules. One TPM2 generic and one strictly EFI which depends on generic one.
Quoted 3 lines
grub-core/tpm2/tcg2.cwhich mainly implements how the TPM2 commands to be submitted. I'd propose to move them to grub-core/commands/tpm2 and renametcg2.ctotcg2-efi.c.
One should land in the grub-core/commands/tss2 directory and another in the grub-core/commands/efi or grub-core/commands/efi/tmp2 if needed.
[...]
Quoted 9 lines
Quoted 9 lines
grub-core/tpm2/mu.c
Quoted 5 lines
grub-core/tpm2/mu.c new file +0
-0I can imagine where it comes from but I think it should be
efi.cinstead ofmu.c.
Quoted 4 lines
No, it's not from the MU firmware but stands for Marshal/Unmarshal. The similar naming policy from tpm2-tss:
https://github.com/tpm2-software/tpm2-tss/blob/master/include/tss2/tss2_mu.h
Then I would rename mu.c file to tss2_mu.c and replace "_tpm2_mu_" with "_Tss2_MU_" in function names.
Quoted 28 lines
Quoted 28 lines
Quoted 15 lines
grub-core/tpm2/mu.c new file +14
-0@@ -0,0 +1,1168 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* grub-core/tpm2/mu.c line 24
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/misc.h> +#include <grub/tpm2/mu.h> + +void +grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_AUTH_COMMAND * authCommand)s/grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal/grub_efi_tpm2_TPMS_AUTH_COMMAND_Marshal/
Quoted 4 lines
Quoted 2 lines
I would change "
grub_tpm2_mu_" prefix to "grub_efi_tpm2_" everywhere.As mentioned above, it's not EFI related, so the renaming is not necessary.
Please look above...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
Quoted 74 lines · Daniel Kiper
Quoted 68 lines · Gary Lin
Quoted 58 lines · Daniel Kiper
Quoted 57 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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.hQuoted 9 lines
grub-core/tpm2/buffer.c
Quoted 5 lines
grub-core/tpm2/buffer.c new file +0
-0I think this together with other TPM2 driver files should go to the grub-core/commands/efi/tpm2 directory.
Quoted 1 line
The TPM2 stack is not EFI only. The only EFI related code is in
Ah, right... Then I think we should have two GRUB modules. One TPM2 generic and one strictly EFI which depends on generic one.
Quoted 9 lines
Quoted 3 lines
grub-core/tpm2/tcg2.cwhich mainly implements how the TPM2 commands to be submitted. I'd propose to move them to grub-core/commands/tpm2 and renametcg2.ctotcg2-efi.c.One should land in the grub-core/commands/tss2 directory and another in the grub-core/commands/efi or grub-core/commands/efi/tmp2 if needed.
[...]
Ok, I'll move most of files to grub-core/commands/tss2 and tcg2.c to grub-core/commands/efi.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
grub-core/tpm2/mu.c
Quoted 5 lines
grub-core/tpm2/mu.c new file +0
-0I can imagine where it comes from but I think it should be
efi.cinstead ofmu.c.
Quoted 8 lines
Quoted 4 lines
No, it's not from the MU firmware but stands for Marshal/Unmarshal. The similar naming policy from tpm2-tss:
https://github.com/tpm2-software/tpm2-tss/blob/master/include/tss2/tss2_mu.h
Then I would rename
mu.cfile totss2_mu.cand replace "_tpm2_mu_" with "_Tss2_MU_" in function names.
Then I think mu.h should be renamed too.
Gary Lin
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 15 lines
grub-core/tpm2/mu.c new file +14
-0@@ -0,0 +1,1168 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* grub-core/tpm2/mu.c line 24
Quoted 10 lines
grub-core/tpm2/mu.c new file +10
-0+* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/misc.h> +#include <grub/tpm2/mu.h> + +void +grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal (grub_tpm2_buffer_t buffer, +const TPMS_AUTH_COMMAND * authCommand)s/grub_tpm2_mu_TPMS_AUTH_COMMAND_Marshal/grub_efi_tpm2_TPMS_AUTH_COMMAND_Marshal/
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
I would change "
grub_tpm2_mu_" prefix to "grub_efi_tpm2_" everywhere.As mentioned above, it's not EFI related, so the renaming is not necessary.
Please look above...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
Quoted 99 lines · Gary Lin
Quoted 74 lines · Daniel Kiper
Quoted 68 lines · Gary Lin
Quoted 58 lines · Daniel Kiper
Quoted 57 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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.hQuoted 9 lines
grub-core/tpm2/buffer.c
Quoted 5 lines
grub-core/tpm2/buffer.c new file +0
-0I think this together with other TPM2 driver files should go to the grub-core/commands/efi/tpm2 directory.
Quoted 1 line
The TPM2 stack is not EFI only. The only EFI related code is in
Ah, right... Then I think we should have two GRUB modules. One TPM2 generic and one strictly EFI which depends on generic one.
Quoted 9 lines
Quoted 3 lines
grub-core/tpm2/tcg2.cwhich mainly implements how the TPM2 commands to be submitted. I'd propose to move them to grub-core/commands/tpm2 and renametcg2.ctotcg2-efi.c.One should land in the grub-core/commands/tss2 directory and another in the grub-core/commands/efi or grub-core/commands/efi/tmp2 if needed.
[...]
Ok, I'll move most of files to grub-core/commands/tss2 and
tcg2.cto grub-core/commands/efi.Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
grub-core/tpm2/mu.c
Quoted 5 lines
grub-core/tpm2/mu.c new file +0
-0I can imagine where it comes from but I think it should be
efi.cinstead ofmu.c.No, it's not from the MU firmware but stands for Marshal/Unmarshal. The similar naming policy from tpm2-tss:
Quoted 6 lines
Quoted 5 lines
Then I would rename
mu.cfile totss2_mu.cand replace "_tpm2_mu_" with "_Tss2_MU_" in function names.Then I think
mu.hshould be renamed too.
Yep!
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
In reply to Gary Lin via Grub-develQuoted 48 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c
Quoted 8 lines
create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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
And I think this patch can be broken up to smaller parts...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
Quoted 58 lines · Daniel Kiper
Quoted 48 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.cQuoted 8 lines
create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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
Quoted 2 lines
And I think this patch can be broken up to smaller parts...
Then I'll try to split the patch to 3 patches: buffer, mu/structs, and TPM2 commands.
Gary Lin
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 10/20] tpm2: Add TPM Software Stack (TSS)
Quoted 59 lines · Gary Lin
Quoted 58 lines · Daniel Kiper
Quoted 56 lines · Gary Lin
From: Hernan Gatta <[email protected]>
A Trusted Platform Module (TPM) Software Stack (TSS) provides logic to compose and submit TPM commands and parse reponses.
A limited number of TPM commands may be accessed via the EFI TCG2 protocol. This protocol exposes functionality that is primarily geared toward TPM usage within the context of Secure Boot. For all other TPM commands, however, such as sealing and unsealing, this protocol does not provide any help, with the exception of passthrough command submission.
The SubmitCommand method allows a caller to send raw commands to the system's TPM and to receive the corresponding response. These command/response pairs are formatted using the TPM wire protocol. To construct commands in this way, and to parse the TPM's response, it is necessary to, first, possess knowledge of the various TPM structures, and, second, of the TPM wire protocol itself.
As such, this patch includes a set of header files that define the necessary TPM structures and TSS functions, implementations of various TPM2_* functions (inventoried below), and logic to write and read command and response buffers, respectively, using the TPM wire protocol.
Functions:
TPM2_Create,TPM2_CreatePrimary,TPM2_EvictControl,TPM2_FlushContext,TPM2_Load,TPM2_PCR_Read,TPM2_PolicyGetDigest,TPM2_PolicyPCR,TPM2_ReadPublic,TPM2_StartAuthSession,TPM2_Unseal,TPM2_LoadExternal,TPM2_Hash,TPM2_VerifySignature,TPM2_PolicyAuthorize,TPM2_TestParmsSigned-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>Reviewed-by Stefan Berger <[email protected]>--- grub-core/tpm2/buffer.c | 145 +++ grub-core/tpm2/mu.c | 1168 ++++++++++++++++++++++++ grub-core/tpm2/tcg2.c | 143 +++ grub-core/tpm2/tpm2.c | 1048 +++++++++++++++++++++ include/grub/tpm2/buffer.h | 65 ++ 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 + 11 files changed, 4360 insertions(+) create mode 100644 grub-core/tpm2/buffer.c create mode 100644 grub-core/tpm2/mu.c create mode 100644 grub-core/tpm2/tcg2.c create mode 100644 grub-core/tpm2/tpm2.c create mode 100644 include/grub/tpm2/buffer.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
Quoted 4 lines
Quoted 2 lines
And I think this patch can be broken up to smaller parts...
Then I'll try to split the patch to 3 patches: buffer, mu/structs, and TPM2 commands.
Makes sense for me...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 11/20 key_protector: Add TPM2 Key Protector
In reply to Gary Lin via Grub-develFrom: Hernan Gatta <[email protected]>
The TPM2 key protector is a module that enables the automatic retrieval of a fully-encrypted disk's unlocking key from a TPM 2.0.
The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile/tpm2key parameter, which is mandatory. There are two supported key formats:
1. Raw Sealed Key (--keyfile)
When sealing a key with TPM2_Create, the public portion of the sealed key is stored in TPM2B_PUBLIC, and the private portion is in TPM2B_PRIVATE. The raw sealed key glues the fully marshalled TPM2B_PUBLIC and TPM2B_PRIVATE into one file.
2. TPM 2.0 Key (--tpm2key)
The following is the ASN.1 definition of TPM 2.0 Key File:
TPMPolicy ::= SEQUENCE {
CommandCode [0] EXPLICIT INTEGER
CommandPolicy [1] EXPLICIT OCTET STRING
}
TPMAuthPolicy ::= SEQUENCE {
Name [0] EXPLICIT UTF8STRING OPTIONAL
Policy [1] EXPLICIT SEQUENCE OF TPMPolicy
}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
}
The TPM2 key protector only expects a "sealed" key in DER encoding, so 'type' is always 2.23.133.10.1.5, 'emptyAuth' is 'TRUE', and 'secret' is empty. 'policy' and 'authPolicy' are the possible policy command sequences to construst the policy digest to unseal the key. Similar to the raw sealed key, the public portion (TPM2B_PUBLIC) of the sealed key is stored in 'pubkey', and the private portion (TPM2B_PRIVATE) is in 'privkey'.
For more details: https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
This sealed key file is created via the grub-protect tool. The tool utilizes the TPM's sealing functionality to seal (i.e., encrypt) an unlocking key using a Storage Root Key (SRK) to the values of various Platform Configuration Registers (PCRs). These PCRs reflect the state of the system as it boots. If the values are as expected, the system may be considered trustworthy, at which point the TPM allows for a caller to utilize the private component of the SRK to unseal (i.e., decrypt) the sealed key file. The caller, in this case, is this key protector.
The TPM2 key protector registers two commands:
- tpm2_key_protector_init: Initializes the state of the TPM2 key protector for later usage, clearing any previous state, too, if any.
- tpm2_key_protector_clear: Clears any state set by tpm2_key_protector_init.
The way this is expected to be used requires the user to, either interactively or, normally, via a boot script, initialize/configure the key protector and then specify that it be used by the 'cryptomount' command (modifications to this command are in a different patch).
For instance, to unseal the raw sealed key file:
tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-1.key cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init --keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2
Or, to unseal the TPM 2.0 Key file:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub2/sealed-1.tpm cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2
If a user does not initialize the key protector and attempts to use it anyway, the protector returns an error.
Before unsealing the key, the TPM2 key protector follows the "TPMPolicy" sequences to enforce the TPM policy commands to construct a valid policy digest to unseal the key.
For the TPM 2.0 Key files, 'authPolicy' may contain multiple "TPMPolicy" sequences, the TPM2 key protector iterates 'authPolicy' to find a valid sequence to unseal key. If 'authPolicy' is empty or all sequences in 'authPolicy' fail, the protector tries the one from 'policy'. In case 'policy' is also empty, the protector creates a "TPMPolicy" sequence based on the given PCR selection.
For the raw sealed key, the TPM2 key protector treats the key file as a TPM 2.0 Key file without 'authPolicy' and 'policy', so the "TPMPolicy" sequence is always based on the PCR selection from the command parameters.
This commit only supports one policy command: TPM2_PolicyPCR. The command set will be extended to support advanced features, such as authorized policy, in the later commits.
--- grub-core/Makefile.core.def | 14 + grub-core/tpm2/args.c | 140 ++++ grub-core/tpm2/module.c | 1225 +++++++++++++++++++++++++++++ grub-core/tpm2/tpm2key.asn | 33 + grub-core/tpm2/tpm2key.c | 475 +++++++++++ grub-core/tpm2/tpm2key_asn1_tab.c | 45 ++ include/grub/tpm2/internal/args.h | 49 ++ include/grub/tpm2/tpm2key.h | 86 ++ 8 files changed, 2067 insertions(+) create mode 100644 grub-core/tpm2/args.c create mode 100644 grub-core/tpm2/module.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/tpm2/internal/args.h create mode 100644 include/grub/tpm2/tpm2key.h
grub-core/Makefile.core.def +14 -0
@@ -2566,6 +2566,20 @@ module = { enable = efi; }; +module = { + name = tpm2; + common = tpm2/args.c; + common = tpm2/buffer.c; + common = tpm2/module.c; + common = tpm2/mu.c; + common = tpm2/tpm2.c; + common = tpm2/tpm2key.c; + common = tpm2/tpm2key_asn1_tab.c; + efi = tpm2/tcg2.c; + enable = efi; + cppflags = '-I$(srcdir)/lib/libtasn1-grub'; +}; + module = { name = tr; common = commands/tr.c;
grub-core/tpm2/args.c new file +140 -0
140 lines not on this page · raw message
grub-core/tpm2/module.c new file +1225 -0
1,225 lines not on this page · raw message
grub-core/tpm2/tpm2key.asn new file +33 -0
@@ -0,0 +1,33 @@ +-- +-- TPM 2.0 key file format +-- To generate tpm2key_asn1_tab.c: asn1Parser tpm2key.asn +-- +TPM2KEY {} +DEFINITIONS IMPLICIT TAGS ::= + +BEGIN + +TPMPolicy ::= SEQUENCE { + CommandCode [0] EXPLICIT INTEGER, + CommandPolicy [1] EXPLICIT OCTET STRING +} + +TPMAuthPolicy ::= SEQUENCE { + Name [0] EXPLICIT UTF8String OPTIONAL, + Policy [1] EXPLICIT SEQUENCE OF TPMPolicy +} + +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 +} + +END
grub-core/tpm2/tpm2key.c new file +475 -0
475 lines not on this page · raw message
grub-core/tpm2/tpm2key_asn1_tab.c new file +45 -0
45 lines not on this page · raw message
include/grub/tpm2/internal/args.h new file +49 -0
49 lines not on this page · raw message
include/grub/tpm2/tpm2key.h new file +86 -0
86 lines not on this page · raw message
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 11/20] key_protector: Add TPM2 Key Protector
Quoted 148 lines · Gary Lin
From: Hernan Gatta <[email protected]>
The TPM2 key protector is a module that enables the automatic retrieval of a fully-encrypted disk's unlocking key from a TPM 2.0.
The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile/tpm2key parameter, which is mandatory. There are two supported key formats:
1. Raw Sealed Key (--keyfile)
When sealing a key withTPM2_Create, the public portion of the sealed key is stored inTPM2B_PUBLIC, and the private portion is inTPM2B_PRIVATE. The raw sealed key glues the fully marshalledTPM2B_PUBLICandTPM2B_PRIVATEinto one file.2. TPM 2.0 Key (--tpm2key)
The following is the ASN.1 definition of TPM 2.0 Key File:TPMPolicy ::= SEQUENCE { CommandCode [0] EXPLICIT INTEGER CommandPolicy [1] EXPLICIT OCTET STRING } TPMAuthPolicy ::= SEQUENCE { Name [0] EXPLICIT UTF8STRING OPTIONAL Policy [1] EXPLICIT SEQUENCE OF TPMPolicy }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 OPTIONALdescription [4] EXPLICIT UTF8String OPTIONAL,
rsaParent [5] EXPLICIT BOOLEAN OPTIONAL,
parent INTEGER
pubkey OCTET STRING
privkey OCTET STRING
}The TPM2 key protector only expects a "sealed" key in DER encoding, so 'type' is always 2.23.133.10.1.5, 'emptyAuth' is 'TRUE', and 'secret' is empty. 'policy' and 'authPolicy' are the possible policy command sequences to construst the policy digest to unseal the key. Similar to the raw sealed key, the public portion (
TPM2B_PUBLIC) of the sealed key is stored in 'pubkey', and the private portion (TPM2B_PRIVATE) is in 'privkey'.For more details: https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
This sealed key file is created via the grub-protect tool. The tool utilizes the TPM's sealing functionality to seal (i.e., encrypt) an unlocking key using a Storage Root Key (SRK) to the values of various Platform Configuration Registers (PCRs). These PCRs reflect the state of the system as it boots. If the values are as expected, the system may be considered trustworthy, at which point the TPM allows for a caller to utilize the private component of the SRK to unseal (i.e., decrypt) the sealed key file. The caller, in this case, is this key protector.
The TPM2 key protector registers two commands:
-
tpm2_key_protector_init: Initializes the state of the TPM2 key protector for later usage, clearing any previous state, too, if any.-
tpm2_key_protector_clear: Clears any state set bytpm2_key_protector_init.The way this is expected to be used requires the user to, either interactively or, normally, via a boot script, initialize/configure the key protector and then specify that it be used by the 'cryptomount' command (modifications to this command are in a different patch).
For instance, to unseal the raw sealed key file:
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub2/sealed-1.key cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2Or, to unseal the TPM 2.0 Key file:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub2/sealed-1.tpm cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2If a user does not initialize the key protector and attempts to use it anyway, the protector returns an error.
Before unsealing the key, the TPM2 key protector follows the "TPMPolicy" sequences to enforce the TPM policy commands to construct a valid policy digest to unseal the key.
For the TPM 2.0 Key files, 'authPolicy' may contain multiple "TPMPolicy" sequences, the TPM2 key protector iterates 'authPolicy' to find a valid sequence to unseal key. If 'authPolicy' is empty or all sequences in 'authPolicy' fail, the protector tries the one from 'policy'. In case 'policy' is also empty, the protector creates a "TPMPolicy" sequence based on the given PCR selection.
For the raw sealed key, the TPM2 key protector treats the key file as a TPM 2.0 Key file without 'authPolicy' and 'policy', so the "TPMPolicy" sequence is always based on the PCR selection from the command parameters.
This commit only supports one policy command:
TPM2_PolicyPCR. The command set will be extended to support advanced features, such as authorized policy, in the later commits.Cc Stefan Berger <[email protected]>Cc James Bottomley <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- grub-core/Makefile.core.def | 14 + grub-core/tpm2/args.c | 140 ++++ grub-core/tpm2/module.c | 1225 +++++++++++++++++++++++++++++ grub-core/tpm2/tpm2key.asn | 33 + grub-core/tpm2/tpm2key.c | 475 +++++++++++ grub-core/tpm2/tpm2key_asn1_tab.c | 45 ++ include/grub/tpm2/internal/args.h | 49 ++ include/grub/tpm2/tpm2key.h | 86 ++ 8 files changed, 2067 insertions(+) create mode 100644 grub-core/tpm2/args.c create mode 100644 grub-core/tpm2/module.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/tpm2/internal/args.h create mode 100644 include/grub/tpm2/tpm2key.hgrub-core/Makefile.core.def +4
-0@@ -2566,6 +2566,20 @@ module = { enable = efi; }; +module = { + name = tpm2; + common = tpm2/args.c; + common = tpm2/buffer.c;
grub-core/Makefile.core.def module = { · line 2582
Quoted 10 lines
grub-core/Makefile.core.def +10
-0+ common = tpm2/module.c; + common = tpm2/mu.c; + common = tpm2/tpm2.c; + common = tpm2/tpm2key.c; + common = tpm2/tpm2key_asn1_tab.c; + efi = tpm2/tcg2.c; + enable = efi; + cppflags = '-I$(srcdir)/lib/libtasn1-grub'; +}; +
I think the TPM2 key protector should be in separate GRUB module, i.e. not integrated with tss2 and tpm2 modules.
Quoted 47 lines
grub-core/Makefile.core.def +0
-0module = { name = tr; common = commands/tr.c;grub-core/tpm2/args.c new file +38
-0@@ -0,0 +1,140 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/err.h> +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/tpm2/internal/args.h> + +grub_err_t +grub_tpm2_protector_parse_pcrs (char *value,grub_uint8_t *pcrs, +grub_uint8_t *pcr_count) +{ +char *current_pcr = value; +char *next_pcr; +unsigned long pcr; +grub_uint8_t i; + +if (grub_strlen (value) ==0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + *pcr_count =0 ; +for (i =0 ; i <TPM_MAX_PCRS ; i++) + {
grub-core/tpm2/args.c line 48
Quoted 10 lines
grub-core/tpm2/args.c new file +10
-0+ next_pcr =grub_strchr (current_pcr,',' ); +if (next_pcr == current_pcr) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Empty entry in PCR list" )); +if (next_pcr) + *next_pcr ='\0' ; + + grub_errno =GRUB_ERR_NONE ; + pcr =grub_strtoul (current_pcr,NULL ,10 ); +if (grub_errno !=GRUB_ERR_NONE )
This check is unreliable. Please take a look at commit ac8a37dda (net/http: Allow use of non-standard TCP/IP ports) how is should be done properly.
grub-core/tpm2/args.c line 59
Quoted 11 lines
grub-core/tpm2/args.c new file +11
-0+return grub_error (grub_errno, +N_ ("Entry '%s' in PCR list is not a number" ), + current_pcr); + +if (pcr >TPM_MAX_PCRS ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Entry %lu in PCR list is too large to be a PCR " +"number, PCR numbers range from 0 to %u" ), + pcr,TPM_MAX_PCRS ); + + pcrs[i] = (grub_uint8_t )pcr;
Missing space after ")".
grub-core/tpm2/args.c line 60
Quoted 1 line
grub-core/tpm2/args.c new file +1
-0+ *pcr_count +=1 ;
++(*pcr_count); ???
Quoted 58 lines
grub-core/tpm2/args.c new file +58
-0+ +if (next_pcr ==NULL ) +break ; + + current_pcr = next_pcr +1 ; +if (*current_pcr =='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Trailing comma at the end of PCR list" )); + } + +if (i ==TPM_MAX_PCRS ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Too many PCRs in PCR list, the maximum number of " +"PCRs is %u" ),TPM_MAX_PCRS ); + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tpm2_protector_parse_asymmetric (const char *value, +grub_srk_type_t *srk_type) +{ +if (grub_strcasecmp (value,"ECC" ) ==0 || +grub_strcasecmp (value,"ECC_NIST_P256" ) ==0 ) + { + srk_type->type =TPM_ALG_ECC ; + 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->detail.rsa_bits =2048 ; + } +else +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Value '%s' is not a valid asymmetric key type" ), + value); + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tpm2_protector_parse_bank (const char *value,TPM_ALG_ID *bank) +{ +if (grub_strcasecmp (value,"SHA1" ) ==0 ) + *bank =TPM_ALG_SHA1 ; +else if (grub_strcasecmp (value,"SHA256" ) ==0 ) + *bank =TPM_ALG_SHA256 ; +else if (grub_strcasecmp (value,"SHA384" ) ==0 ) + *bank =TPM_ALG_SHA384 ; +else if (grub_strcasecmp (value,"SHA512" ) ==0 ) + *bank =TPM_ALG_SHA512 ; +else +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Value '%s' is not a valid PCR bank" ), value); + +return GRUB_ERR_NONE ;
grub-core/tpm2/args.c line 128
Quoted 10 lines
grub-core/tpm2/args.c new file +10
-0+} + +grub_err_t +grub_tpm2_protector_parse_tpm_handle (const char *value,TPM_HANDLE *handle) +{ +unsigned long num; + + grub_errno =GRUB_ERR_NONE ; + num =grub_strtoul (value,NULL ,0 ); +if (grub_errno !=GRUB_ERR_NONE )
Again, please fix this check.
grub-core/tpm2/args.c line 137
Quoted 9 lines
grub-core/tpm2/args.c new file +9
-0+return grub_error (grub_errno,N_ ("TPM handle value '%s' is not a number" ), + value); + +if (num >GRUB_UINT_MAX ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Value %lu is too large to be a TPM handle, TPM " +"handles are unsigned 32-bit integers" ), num); + + *handle = (TPM_HANDLE )num;
All casts should have space after last ")". Please fix this.
Quoted 157 lines
grub-core/tpm2/args.c new file +3
-0+ +return GRUB_ERR_NONE ; +}grub-core/tpm2/module.c new file +148
-0@@ -0,0 +1,1225 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/dl.h> +#include <grub/extcmd.h> +#include <grub/file.h> +#include <grub/list.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/key_protector.h> +#include <grub/tpm2/buffer.h> +#include <grub/tpm2/internal/args.h> +#include <grub/tpm2/internal/types.h> +#include <grub/tpm2/mu.h> +#include <grub/tpm2/tpm2.h> +#include <grub/tpm2/tpm2key.h> + +GRUB_MOD_LICENSE ("GPLv3+" ); + +typedef enum grub_tpm2_protector_mode +{ +GRUB_TPM2_PROTECTOR_MODE_UNSET , +GRUB_TPM2_PROTECTOR_MODE_SRK , +GRUB_TPM2_PROTECTOR_MODE_NV +}grub_tpm2_protector_mode_t ; + +enum grub_tpm2_protector_options +{ +OPTION_MODE , +OPTION_PCRS , +OPTION_BANK , +OPTION_TPM2KEY , +OPTION_KEYFILE , +OPTION_SRK , +OPTION_ASYMMETRIC , +OPTION_NVINDEX +}; + +struct grub_tpm2_protector_context +{ +grub_tpm2_protector_mode_t mode; +grub_uint8_t pcrs[TPM_MAX_PCRS ]; +grub_uint8_t pcr_count; +grub_srk_type_t srk_type; +TPM_ALG_ID bank; +const char *tpm2key; +const char *keyfile; +TPM_HANDLE srk; +TPM_HANDLE nv; +}; + +static const struct grub_arg_option grub_tpm2_protector_init_cmd_options[] = + { +/* Options for all modes */ + { + .longarg ="mode" , + .shortarg ='m' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("Unseal key using SRK ('srk') (default) or retrieve it from an NV " +"Index ('nv')." ), + }, + { + .longarg ="pcrs" , + .shortarg ='p' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("Comma-separated list of PCRs used to authorize key release " +"e.g., '7,11'. (default: 7)" ), + }, + { + .longarg ="bank" , + .shortarg ='b' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("Bank of PCRs used to authorize key release: " +"SHA1, SHA256, SHA384 or SHA512. (default: SHA256)" ), + }, +/* SRK-mode options */ + { + .longarg ="tpm2key" , + .shortarg ='T' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, path to the key file in the TPM 2.0 Key File format " +"to unseal using the TPM (e.g., (hd0,gpt1)/boot/grub2/sealed.tpm)." ), + }, + { + .longarg ="keyfile" , + .shortarg ='k' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, path to the key file in the raw format to unseal " +"using the TPM (e.g., (hd0,gpt1)/boot/grub2/sealed.key). " +"(Mainly for backward compatibility. Please use '--tpm2key'.)" ), + }, + { + .longarg ="srk" , + .shortarg ='s' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, the SRK handle if the SRK is persistent." ), + }, + { + .longarg ="asymmetric" , + .shortarg ='a' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, the type of SRK: RSA (RSA2048) and ECC (ECC_NIST_P256)" +"(default: ECC)" ), + }, +/* NV Index-mode options */ + { + .longarg ="nvindex" , + .shortarg ='n' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc =
grub-core/tpm2/module.c line 158
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+N_ ("Required in NV Index mode, the NV handle to read which must " +"readily exist on the TPM and which contains the key." ), + }, + /* End of list */ + {0, 0, 0, 0, 0, 0} + }; + +static grub_extcmd_t grub_tpm2_protector_init_cmd; +static grub_extcmd_t grub_tpm2_protector_clear_cmd; +static struct grub_tpm2_protector_context grub_tpm2_protector_ctx = {0 };
s/ 0 /0/
Quoted 39 lines
grub-core/tpm2/module.c new file +39
-0+ +static grub_err_t +grub_tpm2_protector_srk_read_file (const char *filepath,void **buffer, +grub_size_t *buffer_size) +{ +grub_file_t file; +grub_off_t file_size; +void *read_buffer; +grub_off_t read_n; +grub_err_t err; + +/* Using GRUB_FILE_TYPE_SIGNATURE ensures we do not hash the keyfile into PCR9 +* otherwise we'll never be able to predict the value of PCR9 at unseal time */ + file =grub_file_open (filepath,GRUB_FILE_TYPE_SIGNATURE ); +if (file ==NULL ) + { +/* Push errno from grub_file_open() into the error message stack */ +grub_error_push (); + err =grub_error (GRUB_ERR_FILE_NOT_FOUND , +N_ ("Could not open file: %s\n" ), + filepath); + goto error; + } + + file_size =grub_file_size (file); +if (file_size ==0 ) + { + err =grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Could not read file size: %s" ), + filepath); + goto error; + } + + read_buffer =grub_malloc (file_size); +if (read_buffer ==NULL ) + { + err =grub_error (GRUB_ERR_OUT_OF_MEMORY , +N_ ("Could not allocate buffer for %s" ), + filepath);
grub-core/tpm2/module.c line 207
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+ goto error; + } + + read_n =grub_file_read (file, read_buffer, file_size); +if (read_n != file_size) + { +grub_free (read_buffer); + err =grub_error (GRUB_ERR_FILE_READ_ERROR , +N_ ("Could not retrieve file contents: %s" ), + filepath);
I would avoid wrapping lines in many places to increase readability of this patch set, e.g.:
err = grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Could not retrieve file contents: %s"), filepath);In general I am OK with lines (a bit) longer than 80.
Quoted 175 lines
grub-core/tpm2/module.c new file +175
-0+ goto error; + } + + *buffer = read_buffer; + *buffer_size = file_size; + + err =GRUB_ERR_NONE ; + +error: +if (file !=NULL ) +grub_file_close (file); + +return err; +} + +static grub_err_t +grub_tpm2_protector_srk_unmarshal_keyfile (void *sealed_key, +grub_size_t sealed_key_size, +TPM2_SEALED_KEY *sk) +{ +struct grub_tpm2_buffer buf; + +grub_tpm2_buffer_init (&buf); +if (sealed_key_size > buf.cap) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Sealed key larger than %" PRIuGRUB_SIZE" bytes" ), + buf.cap); + +grub_memcpy (buf.data, sealed_key, sealed_key_size); + buf.size = sealed_key_size; + +grub_tpm2_mu_TPM2B_PUBLIC_Unmarshal (&buf, &sk->public); +grub_tpm2_mu_TPM2B_PRIVATE_Unmarshal (&buf, &sk->private); + +if (buf.error) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Malformed TPM wire key file" )); + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_tpm2_protector_srk_unmarshal_tpm2key (void *sealed_key, +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; +void *sealed_priv =NULL ; +grub_size_t sealed_priv_size; +struct grub_tpm2_buffer buf; +grub_err_t err; + +/* +* Start to parse the tpm2key file +* 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 +* } +*/ + err =grub_tpm2key_start_parsing (&tpm2key, sealed_key, sealed_key_size); +if (err !=GRUB_ERR_NONE ) +return err; + +/* +* Retrieve the policy sequence from 'policy' +* policy_seq will be NULL when 'policy' is not available +*/ + err =grub_tpm2key_get_policy_seq (tpm2key, policy_seq); +if (err !=GRUB_ERR_NONE ) + goto error; + +/* +* Retrieve the authpolicy sequence from 'authPolicy' +* authpol_seq will be NULL when 'authPolicy' is not available +*/ + err =grub_tpm2key_get_authpolicy_seq (tpm2key, authpol_seq); +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 ) + goto error; + +/* The parent handle should be either PERMANENT or PERSISTENT. */ +if (!TPM_HT_IS_PERMANENT (parent_tmp) && !TPM_HT_IS_PERSISTENT (parent_tmp)) + { + err =GRUB_ERR_OUT_OF_RANGE ; + goto error; + } + + *parent = parent_tmp; + +/* Retrieve the public part of the sealed key */ + err =grub_tpm2key_get_pubkey (tpm2key, &sealed_pub, &sealed_pub_size); +if (err !=GRUB_ERR_NONE ) + goto error; + +/* Retrieve the private part of the sealed key */ + err =grub_tpm2key_get_privkey (tpm2key, &sealed_priv, &sealed_priv_size); +if (err !=GRUB_ERR_NONE ) + goto error; + +/* Unmarshal the sealed key */ +grub_tpm2_buffer_init (&buf); +if (sealed_pub_size + sealed_priv_size > buf.cap) + { + err =grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Sealed key larger than %" PRIuGRUB_SIZE" bytes" ), + buf.cap); + goto error; + } + +grub_tpm2_buffer_pack (&buf, sealed_pub, sealed_pub_size); +grub_tpm2_buffer_pack (&buf, sealed_priv, sealed_priv_size); + + buf.offset =0 ; + +grub_tpm2_mu_TPM2B_PUBLIC_Unmarshal (&buf, &sk->public); +grub_tpm2_mu_TPM2B_PRIVATE_Unmarshal (&buf, &sk->private); + +if (buf.error) + { + err =grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Malformed TPM 2.0 key file" )); + goto error; + } + + err =GRUB_ERR_NONE ; + +error: +/* End the parsing */ +grub_tpm2key_end_parsing (tpm2key); +grub_free (sealed_pub); +grub_free (sealed_priv); + +return err; +} + +/* Check if the SRK exists in the specified handle */ +static grub_err_t +grub_tpm2_protector_srk_check (const TPM_HANDLE srk_handle) +{ +TPM_RC rc; +TPM2B_PUBLIC public; + +/* Find SRK */ + rc =TPM2_ReadPublic (srk_handle,NULL , &public); +if (rc ==TPM_RC_SUCCESS ) +return GRUB_ERR_NONE ; + +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Failed to retrieve SRK from 0x%x (TPM2_ReadPublic: 0x%x)" ), + srk_handle, rc);
grub-core/tpm2/module.c line 392
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+} + +/* Get the SRK with the template */ +static grub_err_t +grub_tpm2_protector_srk_get (const grub_srk_type_t srk_type, +const TPM_HANDLE parent, +TPM_HANDLE *srk_handle) +{ +TPM_RC rc; +TPMT_PUBLIC_PARMS parms = {0 };
s/ 0 /0/ and in general everywhere please...
Quoted 67 lines
grub-core/tpm2/module.c new file +67
-0+TPMS_AUTH_COMMAND authCommand = {0 }; +TPM2B_SENSITIVE_CREATE inSensitive = {0 }; +TPM2B_PUBLIC inPublic = {0 }; +TPM2B_DATA outsideInfo = {0 }; +TPML_PCR_SELECTION creationPcr = {0 }; +TPM2B_PUBLIC outPublic = {0 }; +TPM2B_CREATION_DATA creationData = {0 }; +TPM2B_DIGEST creationHash = {0 }; +TPMT_TK_CREATION creationTicket = {0 }; +TPM2B_NAME srkName = {0 }; +TPM_HANDLE tmp_handle =0 ; + + inPublic.publicArea.type = srk_type.type; + inPublic.publicArea.nameAlg =TPM_ALG_SHA256 ; + inPublic.publicArea.objectAttributes.restricted =1 ; + inPublic.publicArea.objectAttributes.userWithAuth =1 ; + inPublic.publicArea.objectAttributes.decrypt =1 ; + inPublic.publicArea.objectAttributes.fixedTPM =1 ; + inPublic.publicArea.objectAttributes.fixedParent =1 ; + inPublic.publicArea.objectAttributes.sensitiveDataOrigin =1 ; + inPublic.publicArea.objectAttributes.noDA =1 ; + +if (srk_type.type ==TPM_ALG_RSA ) + { + inPublic.publicArea.parameters.rsaDetail.symmetric.algorithm =TPM_ALG_AES ; + 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; + inPublic.publicArea.parameters.rsaDetail.exponent =0 ; + } +else if (srk_type.type ==TPM_ALG_ECC ) + { + inPublic.publicArea.parameters.eccDetail.symmetric.algorithm =TPM_ALG_AES ; + 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; + inPublic.publicArea.parameters.eccDetail.kdf.scheme =TPM_ALG_NULL ; + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Unknown SRK algorithm" )); + +/* Test the parameters before SRK generation */ + parms.type = srk_type.type; +grub_memcpy (&parms.parameters, &inPublic.publicArea.parameters, +sizeof (TPMU_PUBLIC_PARMS )); + + rc =TPM2_TestParms (&parms,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Unsupported SRK template (TPM2_TestParms: 0x%x)" ), + rc); + +/* Create SRK */ + authCommand.sessionHandle =TPM_RS_PW ; + rc =TPM2_CreatePrimary (parent, &authCommand, &inSensitive, &inPublic, + &outsideInfo, &creationPcr, &tmp_handle, &outPublic, + &creationData, &creationHash, &creationTicket, + &srkName,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Could not create SRK (TPM2_CreatePrimary: 0x%x)" ), + rc); + + *srk_handle = tmp_handle; +
grub-core/tpm2/module.c line 469
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+return GRUB_ERR_NONE ; +} + +/* Load the SRK from the persistent handle or create one with a given type of +template, and then associate the sealed key with the SRK +Return values: +* GRUB_ERR_NONE: Everything is fine. +* GRUB_ERR_BAD_ARGUMENT: The SRK doesn't match. Try another one. +* Other: Something went wrong. +*/
Wrong coding style for comments. Please take a look here [1].
Quoted 24 lines
grub-core/tpm2/module.c new file +24
-0+static grub_err_t +grub_tpm2_protector_srk_load (const grub_srk_type_t srk_type, +const TPM2_SEALED_KEY *sealed_key, +const TPM_HANDLE parent, +TPM_HANDLE *sealed_handle, +TPM_HANDLE *srk_handle) +{ +TPMS_AUTH_COMMAND authCmd = {0 }; +TPM2B_NAME name = {0 }; +TPM_RC rc; +grub_err_t err; + +if (srk_handle ==NULL ) +return GRUB_ERR_BUG ; + +if (*srk_handle !=0 ) + { + err =grub_tpm2_protector_srk_check (*srk_handle); +if (err !=GRUB_ERR_NONE ) +return err; + } +else + { + err =grub_tpm2_protector_srk_get (srk_type, parent, srk_handle);
grub-core/tpm2/module.c line 503
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+if (err !=GRUB_ERR_NONE ) +return err; + } + +/* Load the sealed key and associate it with the SRK */ + authCmd.sessionHandle =TPM_RS_PW ; + rc =TPM2_Load (*srk_handle, &authCmd, &sealed_key->private, &sealed_key->public, + sealed_handle, &name,NULL ); +/* If TPM2_Load returns (TPM_RC_INTEGRITY | TPM_RC_P | TPM_RC_1), then it +implies the wrong SRK is used. */
Ditto.
Quoted 22 lines
grub-core/tpm2/module.c new file +22
-0+if (rc == (TPM_RC_INTEGRITY |TPM_RC_P |TPM_RC_1 )) + { + err =grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("SRK not matched" )); + goto error; + } +else if (rc !=TPM_RC_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Failed to load sealed key (TPM2_Load: 0x%x)" ), + rc); + goto error; + } + +return GRUB_ERR_NONE ; + +error: +if (!TPM_HT_IS_PERSISTENT (*srk_handle)) +TPM2_FlushContext (*srk_handle); + +return err; +} +
grub-core/tpm2/module.c line 535
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+static const char * +srk_type_to_name (grub_srk_type_t srk_type) +{ +if (srk_type.type ==TPM_ALG_ECC ) + { +switch (srk_type.detail.ecc_curve) + { +case TPM_ECC_NIST_P256 : +return "ECC_NIST_P256" ; + }
I would use "if" instead of "switch" here.
grub-core/tpm2/module.c line 542
Quoted 7 lines
grub-core/tpm2/module.c new file +7
-0+ } +else if (srk_type.type ==TPM_ALG_RSA ) + { +switch (srk_type.detail.rsa_bits) + { +case 2048 : +return "RSA2048" ;
Ditto.
Quoted 5 lines
grub-core/tpm2/module.c new file +5
-0+ } + } + +return "Unknown" ; +}
[...]
Quoted 21 lines
grub-core/tpm2/module.c new file +21
-0+static grub_err_t +grub_tpm2_protector_simple_policy_seq (const struct grub_tpm2_protector_context *ctx, +tpm2key_policy_t *policy_seq) +{ +tpm2key_policy_t policy =NULL ; +struct grub_tpm2_buffer buf; +TPML_PCR_SELECTION pcr_sel = { + .count =1 , + .pcrSelections = { + { + .hash = ctx->bank, + .sizeOfSelect =3 , + .pcrSelect = {0 } + }, + } + }; +grub_uint8_t i; +grub_err_t err; + +if (policy_seq ==NULL ) +return GRUB_ERR_BAD_ARGUMENT ;
grub-core/tpm2/module.c line 726
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+ +grub_tpm2_buffer_init (&buf); + +for (i =0 ; i < ctx->pcr_count; i++) +TPMS_PCR_SELECTION_SelectPCR (&pcr_sel.pcrSelections[0 ], ctx->pcrs[i]); + +grub_tpm2_buffer_pack_u16 (&buf,0 ); +grub_tpm2_mu_TPML_PCR_SELECTION_Marshal (&buf, &pcr_sel); + +if (buf.error)
If you define buf.error as a bool please use "if (buf.error == true)", etc. for checks.
Quoted 27 lines
grub-core/tpm2/module.c new file +27
-0+return GRUB_ERR_BAD_ARGUMENT ; + + policy =grub_malloc (sizeof (struct tpm2key_policy )); +if (policy ==NULL ) + { + err =GRUB_ERR_OUT_OF_MEMORY ; + goto error; + } + policy->cmd_code = TPM_CC_PolicyPCR; + policy->cmd_policy =grub_malloc (buf.size); +if (policy->cmd_policy ==NULL ) + { + err =GRUB_ERR_OUT_OF_MEMORY ; + goto error; + } +grub_memcpy (policy->cmd_policy, buf.data, buf.size); + policy->cmd_policy_len = buf.size; + +grub_list_push (GRUB_AS_LIST_P (policy_seq),GRUB_AS_LIST (policy)); + +return GRUB_ERR_NONE ; + +error: +grub_free (policy); + +return err; +}
[...]
grub-core/tpm2/module.c line 955
Quoted 7 lines
grub-core/tpm2/module.c new file +7
-0+static grub_err_t +grub_tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx, +grub_uint8_t **key,grub_size_t *key_size) +{ + (void )ctx; + (void )key; + (void )key_size;
"__attribute__ ((unused)" is your friend.
Quoted 4 lines
grub-core/tpm2/module.c new file +4
-0+ +return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET , +N_ ("NV Index mode is not implemented yet" )); +}
[...]
grub-core/tpm2/module.c line 1220
Quoted 4 lines
grub-core/tpm2/module.c new file +4
-0+GRUB_MOD_FINI (tpm2) +{ +grub_free ((void *) grub_tpm2_protector_ctx.keyfile); +grub_memset (&grub_tpm2_protector_ctx,0 ,sizeof (grub_tpm2_protector_ctx));
I think this grub_memset() is redundant just before module unload.
grub-core/tpm2/tpm2key.asn
Quoted 10 lines
grub-core/tpm2/module.c new file +4
-0+grub_key_protector_unregister (&grub_tpm2_key_protector); +grub_unregister_extcmd (grub_tpm2_protector_clear_cmd); +grub_unregister_extcmd (grub_tpm2_protector_init_cmd); +}grub-core/tpm2/tpm2key.asn new file +0
-0@@ -0,0 +1,33 @@
Missing license...
Quoted 98 lines
grub-core/tpm2/tpm2key.asn new file +33
-0+-- +-- TPM 2.0 key file format +-- To generate tpm2key_asn1_tab.c: asn1Parser tpm2key.asn +-- +TPM2KEY {} +DEFINITIONS IMPLICIT TAGS ::= + +BEGIN + +TPMPolicy ::= SEQUENCE { + CommandCode [0] EXPLICIT INTEGER, + CommandPolicy [1] EXPLICIT OCTET STRING +} + +TPMAuthPolicy ::= SEQUENCE { + Name [0] EXPLICIT UTF8String OPTIONAL, + Policy [1] EXPLICIT SEQUENCE OF TPMPolicy +} + +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 +} + +ENDgrub-core/tpm2/tpm2key.c new file +59
-0@@ -0,0 +1,475 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2023 SUSE LLC +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/list.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/tpm2/buffer.h> +#include <grub/tpm2/tpm2key.h> + +extern asn1_static_node tpm2key_asn1_tab[]; +const char *sealed_key_oid ="2.23.133.10.1.5" ; + +static int +asn1_allocate_and_read (asn1_node node,const char *name,void **content,grub_size_t *content_size) +{ +grub_uint8_t *tmpstr =NULL ; +int tmpstr_size =0 ; +int ret; + +if (content ==NULL ) +return ASN1_MEM_ERROR ; + + ret =asn1_read_value (node, name,NULL , &tmpstr_size); +if (ret !=ASN1_MEM_ERROR ) +return ret; + + tmpstr =grub_malloc (tmpstr_size); +if (tmpstr ==NULL ) +return ASN1_MEM_ERROR ; + + ret =asn1_read_value (node, name, tmpstr, &tmpstr_size); +if (ret !=ASN1_SUCCESS ) +return ret; + + *content = tmpstr; + *content_size = tmpstr_size; + +return ASN1_SUCCESS ; +} + +static int +asn1_read_uint32 (asn1_node node,const char *name,grub_uint32_t *out) +{ +grub_uint32_t tmp =0 ;
grub-core/tpm2/tpm2key.c line 69
Quoted 10 lines
grub-core/tpm2/tpm2key.c new file +10
-0+grub_uint8_t *ptr; +void *data =NULL ; +grub_size_t data_size; +int ret; + + ret =asn1_allocate_and_read (node, name, &data, &data_size); +if (ret !=ASN1_SUCCESS ) +return ret; + +if (data_size >4 )
Is it possible to get 3 or less here? If yes then we should check for this too. Or s/>/!=/...
grub-core/tpm2/tpm2key.c line 77
Quoted 8 lines
grub-core/tpm2/tpm2key.c new file +8
-0+ { + ret =ASN1_MEM_ERROR ; + goto error; + } + +/* convert the big-endian integer to host uint32 */ + ptr = (grub_uint8_t *)&tmp + (4 - data_size); +grub_memcpy (ptr, data, data_size);
Could you explain this? Why grub_be_to_cpu32() is not enough? Is it related to alignment? If yes you could use grub_get_unaligned32().
Quoted 8 lines
grub-core/tpm2/tpm2key.c new file +8
-0+ tmp =grub_be_to_cpu32 (tmp); + + *out = tmp; +error: +if (data) +grub_free (data); +return ret; +}
[...]
Quoted 13 lines
grub-core/tpm2/tpm2key.c new file +13
-0+grub_err_t +grub_tpm2key_get_authpolicy_seq (asn1_node tpm2key,tpm2key_authpolicy_t *authpol_seq) +{ +tpm2key_authpolicy_t tmp_seq =NULL ; +tpm2key_authpolicy_t authpol =NULL ; +int authpol_n; +char authpol_pol[AUTHPOLICY_POL_MAX ]; +int i; +int ret; +grub_err_t err; + + ret =asn1_number_of_elements (tpm2key,"authPolicy" , &authpol_n); +if (ret ==ASN1_ELEMENT_NOT_FOUND )
grub-core/tpm2/tpm2key.c line 415
Quoted 10 lines
grub-core/tpm2/tpm2key.c new file +10
-0+ { +/* "authPolicy" is optional, so it may not be available */ + *authpol_seq =NULL ; +return GRUB_ERR_NONE ; + } + else if (ret != ASN1_SUCCESS) + return grub_error (GRUB_ERR_READ_ERROR, N_("Failed to retrieve authPolicy")); + +/* Limit the number of authPolicy elements to two digits (99) */ +if (authpol_n >100 || authpol_n <1 )
I would define high/low policy limits as constants and use them everywhere.
grub-core/tpm2/tpm2key_asn1_tab.c
Quoted 66 lines
grub-core/tpm2/tpm2key.c new file +60
-0+return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Invalid number of authPolicy elements" )); + +/* +* Iterate the authPolicy elements backwards since grub_list_push() prepends +* the item into the list. +*/ +for (i = authpol_n; i >=1 ; i--) { + authpol =grub_zalloc (sizeof (struct tpm2key_authpolicy )); +if (authpol ==NULL ) + { + err =grub_error (GRUB_ERR_OUT_OF_MEMORY , +N_ ("Failed to allocate memory for authPolicy" )); + goto error; + } +grub_snprintf (authpol_pol,AUTHPOLICY_POL_MAX ,"authPolicy.?%d.Policy" , i); + + ret =tpm2key_get_policy_seq (tpm2key, authpol_pol, &authpol->policy_seq); +if (ret !=ASN1_SUCCESS ) + { + err =grub_error (GRUB_ERR_READ_ERROR , +N_ ("Failed to retrieve policy from authPolicy" )); + goto error; + } + +/* Prepend the authPolicy element into the sequence */ +grub_list_push (GRUB_AS_LIST_P (&tmp_seq),GRUB_AS_LIST (authpol)); + } + + *authpol_seq = tmp_seq; + +return GRUB_ERR_NONE ; + +error: +if (authpol) + { +grub_tpm2key_free_policy_seq (authpol->policy_seq); +grub_free (authpol); + } + +grub_tpm2key_free_authpolicy_seq (tmp_seq); + +return err; +} + +void +grub_tpm2key_free_authpolicy_seq (tpm2key_authpolicy_t authpol_seq) +{ +tpm2key_authpolicy_t authpol; +tpm2key_authpolicy_t next; + +if (authpol_seq ==NULL ) +return ; + + FOR_LIST_ELEMENTS_SAFE (authpol, next, authpol_seq) + { +grub_tpm2key_free_policy_seq (authpol->policy_seq); +grub_free (authpol); + } +}grub-core/tpm2/tpm2key_asn1_tab.c new file +0
-0@@ -0,0 +1,45 @@
Missing license here.
Quoted 35 lines
grub-core/tpm2/tpm2key_asn1_tab.c new file +35
-0+/* +* This file is generated by 'asn1Parser tpm2key.asn' and the '#include' +* headers are replaced with the ones in grub2. +* - 'grub/mm.h' for the definition of 'NULL' +* - 'libtasn1.h' for the definition of 'asn1_static_node' +*/ + +#include <grub/mm.h> +#include <libtasn1.h> + +const asn1_static_node tpm2key_asn1_tab[] = { + {"TPM2KEY" ,536875024 ,NULL }, + {NULL ,1073741836 ,NULL }, + {"TPMPolicy" ,1610612741 ,NULL }, + {"CommandCode" ,1610620931 ,NULL }, + {NULL ,2056 ,"0" }, + {"CommandPolicy" ,536879111 ,NULL }, + {NULL ,2056 ,"1" }, + {"TPMAuthPolicy" ,1610612741 ,NULL }, + {"Name" ,1610637346 ,NULL }, + {NULL ,2056 ,"0" }, + {"Policy" ,536879115 ,NULL }, + {NULL ,1073743880 ,"1" }, + {NULL ,2 ,"TPMPolicy" }, + {"TPMKey" ,536870917 ,NULL }, + {"type" ,1073741836 ,NULL }, + {"emptyAuth" ,1610637316 ,NULL }, + {NULL ,2056 ,"0" }, + {"policy" ,1610637323 ,NULL }, + {NULL ,1073743880 ,"1" }, + {NULL ,2 ,"TPMPolicy" }, + {"secret" ,1610637319 ,NULL }, + {NULL ,2056 ,"2" }, + {"authPolicy" ,1610637323 ,NULL }, + {NULL ,1073743880 ,"3" },
grub-core/tpm2/tpm2key_asn1_tab.c line 45
Quoted 10 lines
grub-core/tpm2/tpm2key_asn1_tab.c new file +10
-0+ { NULL, 2, "TPMAuthPolicy"}, + { "description", 1610637346, NULL }, + { NULL, 2056, "4"}, + { "rsaParent", 1610637316, NULL }, + { NULL, 2056, "5"}, + { "parent", 1073741827, NULL }, + { "pubkey", 1073741831, NULL }, + { "privkey", 7, NULL }, + { NULL, 0, NULL } +};
Daniel
[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 11/20] key_protector: Add TPM2 Key Protector
Quoted 154 lines · Daniel Kiper
Quoted 153 lines · Gary Lin
From: Hernan Gatta <[email protected]>
The TPM2 key protector is a module that enables the automatic retrieval of a fully-encrypted disk's unlocking key from a TPM 2.0.
The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile/tpm2key parameter, which is mandatory. There are two supported key formats:
1. Raw Sealed Key (--keyfile)
When sealing a key withTPM2_Create, the public portion of the sealed key is stored inTPM2B_PUBLIC, and the private portion is inTPM2B_PRIVATE. The raw sealed key glues the fully marshalledTPM2B_PUBLICandTPM2B_PRIVATEinto one file.2. TPM 2.0 Key (--tpm2key)
The following is the ASN.1 definition of TPM 2.0 Key File:TPMPolicy ::= SEQUENCE { CommandCode [0] EXPLICIT INTEGER CommandPolicy [1] EXPLICIT OCTET STRING } TPMAuthPolicy ::= SEQUENCE { Name [0] EXPLICIT UTF8STRING OPTIONAL Policy [1] EXPLICIT SEQUENCE OF TPMPolicy }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 OPTIONALdescription [4] EXPLICIT UTF8String OPTIONAL,
rsaParent [5] EXPLICIT BOOLEAN OPTIONAL,
parent INTEGER
pubkey OCTET STRING
privkey OCTET STRING
}The TPM2 key protector only expects a "sealed" key in DER encoding, so 'type' is always 2.23.133.10.1.5, 'emptyAuth' is 'TRUE', and 'secret' is empty. 'policy' and 'authPolicy' are the possible policy command sequences to construst the policy digest to unseal the key. Similar to the raw sealed key, the public portion (
TPM2B_PUBLIC) of the sealed key is stored in 'pubkey', and the private portion (TPM2B_PRIVATE) is in 'privkey'.For more details: https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
This sealed key file is created via the grub-protect tool. The tool utilizes the TPM's sealing functionality to seal (i.e., encrypt) an unlocking key using a Storage Root Key (SRK) to the values of various Platform Configuration Registers (PCRs). These PCRs reflect the state of the system as it boots. If the values are as expected, the system may be considered trustworthy, at which point the TPM allows for a caller to utilize the private component of the SRK to unseal (i.e., decrypt) the sealed key file. The caller, in this case, is this key protector.
The TPM2 key protector registers two commands:
-
tpm2_key_protector_init: Initializes the state of the TPM2 key protector for later usage, clearing any previous state, too, if any.-
tpm2_key_protector_clear: Clears any state set bytpm2_key_protector_init.The way this is expected to be used requires the user to, either interactively or, normally, via a boot script, initialize/configure the key protector and then specify that it be used by the 'cryptomount' command (modifications to this command are in a different patch).
For instance, to unseal the raw sealed key file:
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub2/sealed-1.key cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2Or, to unseal the TPM 2.0 Key file:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub2/sealed-1.tpm cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2If a user does not initialize the key protector and attempts to use it anyway, the protector returns an error.
Before unsealing the key, the TPM2 key protector follows the "TPMPolicy" sequences to enforce the TPM policy commands to construct a valid policy digest to unseal the key.
For the TPM 2.0 Key files, 'authPolicy' may contain multiple "TPMPolicy" sequences, the TPM2 key protector iterates 'authPolicy' to find a valid sequence to unseal key. If 'authPolicy' is empty or all sequences in 'authPolicy' fail, the protector tries the one from 'policy'. In case 'policy' is also empty, the protector creates a "TPMPolicy" sequence based on the given PCR selection.
For the raw sealed key, the TPM2 key protector treats the key file as a TPM 2.0 Key file without 'authPolicy' and 'policy', so the "TPMPolicy" sequence is always based on the PCR selection from the command parameters.
This commit only supports one policy command:
TPM2_PolicyPCR. The command set will be extended to support advanced features, such as authorized policy, in the later commits.Cc Stefan Berger <[email protected]>Cc James Bottomley <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- grub-core/Makefile.core.def | 14 + grub-core/tpm2/args.c | 140 ++++ grub-core/tpm2/module.c | 1225 +++++++++++++++++++++++++++++ grub-core/tpm2/tpm2key.asn | 33 + grub-core/tpm2/tpm2key.c | 475 +++++++++++ grub-core/tpm2/tpm2key_asn1_tab.c | 45 ++ include/grub/tpm2/internal/args.h | 49 ++ include/grub/tpm2/tpm2key.h | 86 ++ 8 files changed, 2067 insertions(+) create mode 100644 grub-core/tpm2/args.c create mode 100644 grub-core/tpm2/module.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/tpm2/internal/args.h create mode 100644 include/grub/tpm2/tpm2key.hgrub-core/Makefile.core.def +9
-0@@ -2566,6 +2566,20 @@ module = { enable = efi; }; +module = { + name = tpm2; + common = tpm2/args.c; + common = tpm2/buffer.c; + common = tpm2/module.c; + common = tpm2/mu.c; + common = tpm2/tpm2.c; + common = tpm2/tpm2key.c; + common = tpm2/tpm2key_asn1_tab.c;
Quoted 9 lines
grub-core/Makefile.core.def module = { · line 2582
Quoted 5 lines
grub-core/Makefile.core.def +5
-0+ efi = tpm2/tcg2.c; + enable = efi; + cppflags = '-I$(srcdir)/lib/libtasn1-grub'; +}; +I think the TPM2 key protector should be in separate GRUB module, i.e. not integrated with tss2 and tpm2 modules.
Hmmm, I'm thinking about putting the tss2 parts in grub-core/lib/ since buffer.c/mu.c/tpm2.c/tcg2 do not provide any grub commands and are more like a library.
Quoted 53 lines
Quoted 53 lines
grub-core/Makefile.core.def +0
-0module = { name = tr; common = commands/tr.c;grub-core/tpm2/args.c new file +44
-0@@ -0,0 +1,140 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/err.h> +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/tpm2/internal/args.h> + +grub_err_t +grub_tpm2_protector_parse_pcrs (char *value,grub_uint8_t *pcrs, +grub_uint8_t *pcr_count) +{ +char *current_pcr = value; +char *next_pcr; +unsigned long pcr; +grub_uint8_t i; + +if (grub_strlen (value) ==0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + *pcr_count =0 ; +for (i =0 ; i <TPM_MAX_PCRS ; i++) + { + next_pcr =grub_strchr (current_pcr,',' ); +if (next_pcr == current_pcr) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Empty entry in PCR list" )); +if (next_pcr) + *next_pcr ='\0' ;
Quoted 9 lines
grub-core/tpm2/args.c line 48
Quoted 4 lines
grub-core/tpm2/args.c new file +4
-0+ + grub_errno =GRUB_ERR_NONE ; + pcr =grub_strtoul (current_pcr,NULL ,10 ); +if (grub_errno !=GRUB_ERR_NONE )This check is unreliable. Please take a look at commit
ac8a37dda(net/http: Allow use of non-standard TCP/IP ports) how is should be done properly.
Thanks for the hint. Will improve the check in v18.
Quoted 5 lines
Quoted 5 lines
grub-core/tpm2/args.c new file +5
-0+return grub_error (grub_errno, +N_ ("Entry '%s' in PCR list is not a number" ), + current_pcr); + +if (pcr >TPM_MAX_PCRS )
Quoted 9 lines
grub-core/tpm2/args.c line 59
Quoted 6 lines
grub-core/tpm2/args.c new file +6
-0+return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Entry %lu in PCR list is too large to be a PCR " +"number, PCR numbers range from 0 to %u" ), + pcr,TPM_MAX_PCRS ); + + pcrs[i] = (grub_uint8_t )pcr;Missing space after ")".
Will fix it in v18.
Quoted 4 lines
grub-core/tpm2/args.c line 60
Quoted 1 line
grub-core/tpm2/args.c new file +1
-0+ *pcr_count +=1 ;++(*pcr_count); ???
Those look the same to me here. Is there any functional difference?
Quoted 62 lines
Quoted 62 lines
grub-core/tpm2/args.c new file +62
-0+ +if (next_pcr ==NULL ) +break ; + + current_pcr = next_pcr +1 ; +if (*current_pcr =='\0' ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Trailing comma at the end of PCR list" )); + } + +if (i ==TPM_MAX_PCRS ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Too many PCRs in PCR list, the maximum number of " +"PCRs is %u" ),TPM_MAX_PCRS ); + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tpm2_protector_parse_asymmetric (const char *value, +grub_srk_type_t *srk_type) +{ +if (grub_strcasecmp (value,"ECC" ) ==0 || +grub_strcasecmp (value,"ECC_NIST_P256" ) ==0 ) + { + srk_type->type =TPM_ALG_ECC ; + 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->detail.rsa_bits =2048 ; + } +else +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Value '%s' is not a valid asymmetric key type" ), + value); + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tpm2_protector_parse_bank (const char *value,TPM_ALG_ID *bank) +{ +if (grub_strcasecmp (value,"SHA1" ) ==0 ) + *bank =TPM_ALG_SHA1 ; +else if (grub_strcasecmp (value,"SHA256" ) ==0 ) + *bank =TPM_ALG_SHA256 ; +else if (grub_strcasecmp (value,"SHA384" ) ==0 ) + *bank =TPM_ALG_SHA384 ; +else if (grub_strcasecmp (value,"SHA512" ) ==0 ) + *bank =TPM_ALG_SHA512 ; +else +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Value '%s' is not a valid PCR bank" ), value); + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tpm2_protector_parse_tpm_handle (const char *value,TPM_HANDLE *handle)
Quoted 9 lines
grub-core/tpm2/args.c line 128
Quoted 6 lines
grub-core/tpm2/args.c new file +6
-0+{ +unsigned long num; + + grub_errno =GRUB_ERR_NONE ; + num =grub_strtoul (value,NULL ,0 ); +if (grub_errno !=GRUB_ERR_NONE )Again, please fix this check.
Got it.
Quoted 12 lines
grub-core/tpm2/args.c line 137
Quoted 9 lines
grub-core/tpm2/args.c new file +9
-0+return grub_error (grub_errno,N_ ("TPM handle value '%s' is not a number" ), + value); + +if (num >GRUB_UINT_MAX ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Value %lu is too large to be a TPM handle, TPM " +"handles are unsigned 32-bit integers" ), num); + + *handle = (TPM_HANDLE )num;All casts should have space after last ")". Please fix this.
Ok.
Quoted 161 lines
Quoted 161 lines
grub-core/tpm2/args.c new file +3
-0+ +return GRUB_ERR_NONE ; +}grub-core/tpm2/module.c new file +152
-0@@ -0,0 +1,1225 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/dl.h> +#include <grub/extcmd.h> +#include <grub/file.h> +#include <grub/list.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/key_protector.h> +#include <grub/tpm2/buffer.h> +#include <grub/tpm2/internal/args.h> +#include <grub/tpm2/internal/types.h> +#include <grub/tpm2/mu.h> +#include <grub/tpm2/tpm2.h> +#include <grub/tpm2/tpm2key.h> + +GRUB_MOD_LICENSE ("GPLv3+" ); + +typedef enum grub_tpm2_protector_mode +{ +GRUB_TPM2_PROTECTOR_MODE_UNSET , +GRUB_TPM2_PROTECTOR_MODE_SRK , +GRUB_TPM2_PROTECTOR_MODE_NV +}grub_tpm2_protector_mode_t ; + +enum grub_tpm2_protector_options +{ +OPTION_MODE , +OPTION_PCRS , +OPTION_BANK , +OPTION_TPM2KEY , +OPTION_KEYFILE , +OPTION_SRK , +OPTION_ASYMMETRIC , +OPTION_NVINDEX +}; + +struct grub_tpm2_protector_context +{ +grub_tpm2_protector_mode_t mode; +grub_uint8_t pcrs[TPM_MAX_PCRS ]; +grub_uint8_t pcr_count; +grub_srk_type_t srk_type; +TPM_ALG_ID bank; +const char *tpm2key; +const char *keyfile; +TPM_HANDLE srk; +TPM_HANDLE nv; +}; + +static const struct grub_arg_option grub_tpm2_protector_init_cmd_options[] = + { +/* Options for all modes */ + { + .longarg ="mode" , + .shortarg ='m' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("Unseal key using SRK ('srk') (default) or retrieve it from an NV " +"Index ('nv')." ), + }, + { + .longarg ="pcrs" , + .shortarg ='p' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("Comma-separated list of PCRs used to authorize key release " +"e.g., '7,11'. (default: 7)" ), + }, + { + .longarg ="bank" , + .shortarg ='b' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("Bank of PCRs used to authorize key release: " +"SHA1, SHA256, SHA384 or SHA512. (default: SHA256)" ), + }, +/* SRK-mode options */ + { + .longarg ="tpm2key" , + .shortarg ='T' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, path to the key file in the TPM 2.0 Key File format " +"to unseal using the TPM (e.g., (hd0,gpt1)/boot/grub2/sealed.tpm)." ), + }, + { + .longarg ="keyfile" , + .shortarg ='k' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, path to the key file in the raw format to unseal " +"using the TPM (e.g., (hd0,gpt1)/boot/grub2/sealed.key). " +"(Mainly for backward compatibility. Please use '--tpm2key'.)" ), + }, + { + .longarg ="srk" , + .shortarg ='s' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, the SRK handle if the SRK is persistent." ), + }, + { + .longarg ="asymmetric" , + .shortarg ='a' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("In SRK mode, the type of SRK: RSA (RSA2048) and ECC (ECC_NIST_P256)" +"(default: ECC)" ), + }, +/* NV Index-mode options */ + { + .longarg ="nvindex" , + .shortarg ='n' , + .flags =0 , + .arg =NULL , + .type =ARG_TYPE_STRING , + .doc = +N_ ("Required in NV Index mode, the NV handle to read which must " +"readily exist on the TPM and which contains the key." ), + }, +/* End of list */
Quoted 8 lines
grub-core/tpm2/module.c line 158
Quoted 6 lines
grub-core/tpm2/module.c new file +6
-0+ {0, 0, 0, 0, 0, 0} + }; + +static grub_extcmd_t grub_tpm2_protector_init_cmd; +static grub_extcmd_t grub_tpm2_protector_clear_cmd; +static struct grub_tpm2_protector_context grub_tpm2_protector_ctx = {0 };s/ 0 /0/
Will fix it in v18.
Quoted 54 lines
Quoted 40 lines
grub-core/tpm2/module.c new file +39
-0+ +static grub_err_t +grub_tpm2_protector_srk_read_file (const char *filepath,void **buffer, +grub_size_t *buffer_size) +{ +grub_file_t file; +grub_off_t file_size; +void *read_buffer; +grub_off_t read_n; +grub_err_t err; + +/* Using GRUB_FILE_TYPE_SIGNATURE ensures we do not hash the keyfile into PCR9 +* otherwise we'll never be able to predict the value of PCR9 at unseal time */ + file =grub_file_open (filepath,GRUB_FILE_TYPE_SIGNATURE ); +if (file ==NULL ) + { +/* Push errno from grub_file_open() into the error message stack */ +grub_error_push (); + err =grub_error (GRUB_ERR_FILE_NOT_FOUND , +N_ ("Could not open file: %s\n" ), + filepath); + goto error; + } + + file_size =grub_file_size (file); +if (file_size ==0 ) + { + err =grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Could not read file size: %s" ), + filepath); + goto error; + } + + read_buffer =grub_malloc (file_size); +if (read_buffer ==NULL ) + { + err =grub_error (GRUB_ERR_OUT_OF_MEMORY , +N_ ("Could not allocate buffer for %s" ), + filepath);grub-core/tpm2/module.c line 207
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+ goto error; + } + + read_n =grub_file_read (file, read_buffer, file_size); +if (read_n != file_size) + { +grub_free (read_buffer); + err =grub_error (GRUB_ERR_FILE_READ_ERROR , +N_ ("Could not retrieve file contents: %s" ), + filepath);I would avoid wrapping lines in many places to increase readability of this patch set, e.g.:
Quoted 4 lines
err = grub_error (GRUB_ERR_FILE_READ_ERROR, N_("Could not retrieve file contents: %s"), filepath);In general I am OK with lines (a bit) longer than 80.
Alright. I'll check the wrapping lines.
Quoted 179 lines
Quoted 179 lines
grub-core/tpm2/module.c new file +179
-0+ goto error; + } + + *buffer = read_buffer; + *buffer_size = file_size; + + err =GRUB_ERR_NONE ; + +error: +if (file !=NULL ) +grub_file_close (file); + +return err; +} + +static grub_err_t +grub_tpm2_protector_srk_unmarshal_keyfile (void *sealed_key, +grub_size_t sealed_key_size, +TPM2_SEALED_KEY *sk) +{ +struct grub_tpm2_buffer buf; + +grub_tpm2_buffer_init (&buf); +if (sealed_key_size > buf.cap) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Sealed key larger than %" PRIuGRUB_SIZE" bytes" ), + buf.cap); + +grub_memcpy (buf.data, sealed_key, sealed_key_size); + buf.size = sealed_key_size; + +grub_tpm2_mu_TPM2B_PUBLIC_Unmarshal (&buf, &sk->public); +grub_tpm2_mu_TPM2B_PRIVATE_Unmarshal (&buf, &sk->private); + +if (buf.error) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Malformed TPM wire key file" )); + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_tpm2_protector_srk_unmarshal_tpm2key (void *sealed_key, +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; +void *sealed_priv =NULL ; +grub_size_t sealed_priv_size; +struct grub_tpm2_buffer buf; +grub_err_t err; + +/* +* Start to parse the tpm2key file +* 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 +* } +*/ + err =grub_tpm2key_start_parsing (&tpm2key, sealed_key, sealed_key_size); +if (err !=GRUB_ERR_NONE ) +return err; + +/* +* Retrieve the policy sequence from 'policy' +* policy_seq will be NULL when 'policy' is not available +*/ + err =grub_tpm2key_get_policy_seq (tpm2key, policy_seq); +if (err !=GRUB_ERR_NONE ) + goto error; + +/* +* Retrieve the authpolicy sequence from 'authPolicy' +* authpol_seq will be NULL when 'authPolicy' is not available +*/ + err =grub_tpm2key_get_authpolicy_seq (tpm2key, authpol_seq); +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 ) + goto error; + +/* The parent handle should be either PERMANENT or PERSISTENT. */ +if (!TPM_HT_IS_PERMANENT (parent_tmp) && !TPM_HT_IS_PERSISTENT (parent_tmp)) + { + err =GRUB_ERR_OUT_OF_RANGE ; + goto error; + } + + *parent = parent_tmp; + +/* Retrieve the public part of the sealed key */ + err =grub_tpm2key_get_pubkey (tpm2key, &sealed_pub, &sealed_pub_size); +if (err !=GRUB_ERR_NONE ) + goto error; + +/* Retrieve the private part of the sealed key */ + err =grub_tpm2key_get_privkey (tpm2key, &sealed_priv, &sealed_priv_size); +if (err !=GRUB_ERR_NONE ) + goto error; + +/* Unmarshal the sealed key */ +grub_tpm2_buffer_init (&buf); +if (sealed_pub_size + sealed_priv_size > buf.cap) + { + err =grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Sealed key larger than %" PRIuGRUB_SIZE" bytes" ), + buf.cap); + goto error; + } + +grub_tpm2_buffer_pack (&buf, sealed_pub, sealed_pub_size); +grub_tpm2_buffer_pack (&buf, sealed_priv, sealed_priv_size); + + buf.offset =0 ; + +grub_tpm2_mu_TPM2B_PUBLIC_Unmarshal (&buf, &sk->public); +grub_tpm2_mu_TPM2B_PRIVATE_Unmarshal (&buf, &sk->private); + +if (buf.error) + { + err =grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Malformed TPM 2.0 key file" )); + goto error; + } + + err =GRUB_ERR_NONE ; + +error: +/* End the parsing */ +grub_tpm2key_end_parsing (tpm2key); +grub_free (sealed_pub); +grub_free (sealed_priv); + +return err; +} + +/* Check if the SRK exists in the specified handle */ +static grub_err_t +grub_tpm2_protector_srk_check (const TPM_HANDLE srk_handle) +{ +TPM_RC rc; +TPM2B_PUBLIC public; + +/* Find SRK */ + rc =TPM2_ReadPublic (srk_handle,NULL , &public); +if (rc ==TPM_RC_SUCCESS ) +return GRUB_ERR_NONE ; + +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Failed to retrieve SRK from 0x%x (TPM2_ReadPublic: 0x%x)" ), + srk_handle, rc); +} + +/* Get the SRK with the template */ +static grub_err_t
Quoted 9 lines
grub-core/tpm2/module.c line 392
Quoted 6 lines
grub-core/tpm2/module.c new file +6
-0+grub_tpm2_protector_srk_get (const grub_srk_type_t srk_type, + const TPM_HANDLE parent, + TPM_HANDLE *srk_handle) +{ + TPM_RC rc; +TPMT_PUBLIC_PARMS parms = {0 };s/ 0 /0/ and in general everywhere please...
Ok.
Quoted 71 lines
Quoted 71 lines
grub-core/tpm2/module.c new file +71
-0+TPMS_AUTH_COMMAND authCommand = {0 }; +TPM2B_SENSITIVE_CREATE inSensitive = {0 }; +TPM2B_PUBLIC inPublic = {0 }; +TPM2B_DATA outsideInfo = {0 }; +TPML_PCR_SELECTION creationPcr = {0 }; +TPM2B_PUBLIC outPublic = {0 }; +TPM2B_CREATION_DATA creationData = {0 }; +TPM2B_DIGEST creationHash = {0 }; +TPMT_TK_CREATION creationTicket = {0 }; +TPM2B_NAME srkName = {0 }; +TPM_HANDLE tmp_handle =0 ; + + inPublic.publicArea.type = srk_type.type; + inPublic.publicArea.nameAlg =TPM_ALG_SHA256 ; + inPublic.publicArea.objectAttributes.restricted =1 ; + inPublic.publicArea.objectAttributes.userWithAuth =1 ; + inPublic.publicArea.objectAttributes.decrypt =1 ; + inPublic.publicArea.objectAttributes.fixedTPM =1 ; + inPublic.publicArea.objectAttributes.fixedParent =1 ; + inPublic.publicArea.objectAttributes.sensitiveDataOrigin =1 ; + inPublic.publicArea.objectAttributes.noDA =1 ; + +if (srk_type.type ==TPM_ALG_RSA ) + { + inPublic.publicArea.parameters.rsaDetail.symmetric.algorithm =TPM_ALG_AES ; + 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; + inPublic.publicArea.parameters.rsaDetail.exponent =0 ; + } +else if (srk_type.type ==TPM_ALG_ECC ) + { + inPublic.publicArea.parameters.eccDetail.symmetric.algorithm =TPM_ALG_AES ; + 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; + inPublic.publicArea.parameters.eccDetail.kdf.scheme =TPM_ALG_NULL ; + } +else +return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Unknown SRK algorithm" )); + +/* Test the parameters before SRK generation */ + parms.type = srk_type.type; +grub_memcpy (&parms.parameters, &inPublic.publicArea.parameters, +sizeof (TPMU_PUBLIC_PARMS )); + + rc =TPM2_TestParms (&parms,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Unsupported SRK template (TPM2_TestParms: 0x%x)" ), + rc); + +/* Create SRK */ + authCommand.sessionHandle =TPM_RS_PW ; + rc =TPM2_CreatePrimary (parent, &authCommand, &inSensitive, &inPublic, + &outsideInfo, &creationPcr, &tmp_handle, &outPublic, + &creationData, &creationHash, &creationTicket, + &srkName,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Could not create SRK (TPM2_CreatePrimary: 0x%x)" ), + rc); + + *srk_handle = tmp_handle; + +return GRUB_ERR_NONE ; +} + +/* Load the SRK from the persistent handle or create one with a given type of
Quoted 9 lines
grub-core/tpm2/module.c line 469
Quoted 6 lines
grub-core/tpm2/module.c new file +6
-0+template, and then associate the sealed key with the SRK +Return values: +* GRUB_ERR_NONE: Everything is fine. +* GRUB_ERR_BAD_ARGUMENT: The SRK doesn't match. Try another one. +* Other: Something went wrong. +*/ Wrong coding style for comments. Please take a look here [1].
Will fix it in v18.
Quoted 63 lines
Quoted 24 lines
grub-core/tpm2/module.c new file +24
-0+static grub_err_t +grub_tpm2_protector_srk_load (const grub_srk_type_t srk_type, +const TPM2_SEALED_KEY *sealed_key, +const TPM_HANDLE parent, +TPM_HANDLE *sealed_handle, +TPM_HANDLE *srk_handle) +{ +TPMS_AUTH_COMMAND authCmd = {0 }; +TPM2B_NAME name = {0 }; +TPM_RC rc; +grub_err_t err; + +if (srk_handle ==NULL ) +return GRUB_ERR_BUG ; + +if (*srk_handle !=0 ) + { + err =grub_tpm2_protector_srk_check (*srk_handle); +if (err !=GRUB_ERR_NONE ) +return err; + } +else + { + err =grub_tpm2_protector_srk_get (srk_type, parent, srk_handle);grub-core/tpm2/module.c line 503
Quoted 10 lines
grub-core/tpm2/module.c new file +10
-0+if (err !=GRUB_ERR_NONE ) +return err; + } + +/* Load the sealed key and associate it with the SRK */ + authCmd.sessionHandle =TPM_RS_PW ; + rc =TPM2_Load (*srk_handle, &authCmd, &sealed_key->private, &sealed_key->public, + sealed_handle, &name,NULL ); +/* If TPM2_Load returns (TPM_RC_INTEGRITY | TPM_RC_P | TPM_RC_1), then it +implies the wrong SRK is used. */ Ditto.
Quoted 26 lines
grub-core/tpm2/module.c new file +26
-0+if (rc == (TPM_RC_INTEGRITY |TPM_RC_P |TPM_RC_1 )) + { + err =grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("SRK not matched" )); + goto error; + } +else if (rc !=TPM_RC_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Failed to load sealed key (TPM2_Load: 0x%x)" ), + rc); + goto error; + } + +return GRUB_ERR_NONE ; + +error: +if (!TPM_HT_IS_PERSISTENT (*srk_handle)) +TPM2_FlushContext (*srk_handle); + +return err; +} + +static const char * +srk_type_to_name (grub_srk_type_t srk_type) +{ +if (srk_type.type ==TPM_ALG_ECC )
Quoted 9 lines
grub-core/tpm2/module.c line 535
Quoted 6 lines
grub-core/tpm2/module.c new file +6
-0+ { +switch (srk_type.detail.ecc_curve) + { +case TPM_ECC_NIST_P256 : +return "ECC_NIST_P256" ; + }I would use "if" instead of "switch" here.
Ok.
Quoted 44 lines
grub-core/tpm2/module.c line 542
Quoted 7 lines
grub-core/tpm2/module.c new file +7
-0+ } +else if (srk_type.type ==TPM_ALG_RSA ) + { +switch (srk_type.detail.rsa_bits) + { +case 2048 : +return "RSA2048" ;Ditto.
Quoted 5 lines
grub-core/tpm2/module.c new file +5
-0+ } + } + +return "Unknown" ; +}[...]
Quoted 26 lines
grub-core/tpm2/module.c new file +26
-0+static grub_err_t +grub_tpm2_protector_simple_policy_seq (const struct grub_tpm2_protector_context *ctx, +tpm2key_policy_t *policy_seq) +{ +tpm2key_policy_t policy =NULL ; +struct grub_tpm2_buffer buf; +TPML_PCR_SELECTION pcr_sel = { + .count =1 , + .pcrSelections = { + { + .hash = ctx->bank, + .sizeOfSelect =3 , + .pcrSelect = {0 } + }, + } + }; +grub_uint8_t i; +grub_err_t err; + +if (policy_seq ==NULL ) +return GRUB_ERR_BAD_ARGUMENT ; + +grub_tpm2_buffer_init (&buf); + +for (i =0 ; i < ctx->pcr_count; i++) +TPMS_PCR_SELECTION_SelectPCR (&pcr_sel.pcrSelections[0 ], ctx->pcrs[i]);
Quoted 9 lines
grub-core/tpm2/module.c line 726
Quoted 5 lines
grub-core/tpm2/module.c new file +5
-0+ +grub_tpm2_buffer_pack_u16 (&buf,0 ); +grub_tpm2_mu_TPML_PCR_SELECTION_Marshal (&buf, &pcr_sel); + +if (buf.error)If you define buf.error as a bool please use "if (buf.error == true)", etc. for checks.
Ok.
Quoted 47 lines
Quoted 27 lines
grub-core/tpm2/module.c new file +27
-0+return GRUB_ERR_BAD_ARGUMENT ; + + policy =grub_malloc (sizeof (struct tpm2key_policy )); +if (policy ==NULL ) + { + err =GRUB_ERR_OUT_OF_MEMORY ; + goto error; + } + policy->cmd_code = TPM_CC_PolicyPCR; + policy->cmd_policy =grub_malloc (buf.size); +if (policy->cmd_policy ==NULL ) + { + err =GRUB_ERR_OUT_OF_MEMORY ; + goto error; + } +grub_memcpy (policy->cmd_policy, buf.data, buf.size); + policy->cmd_policy_len = buf.size; + +grub_list_push (GRUB_AS_LIST_P (policy_seq),GRUB_AS_LIST (policy)); + +return GRUB_ERR_NONE ; + +error: +grub_free (policy); + +return err; +}[...]
grub-core/tpm2/module.c line 955
Quoted 7 lines
grub-core/tpm2/module.c new file +7
-0+static grub_err_t +grub_tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx, +grub_uint8_t **key,grub_size_t *key_size) +{ + (void )ctx; + (void )key; + (void )key_size;"
__attribute__((unused)" is your friend.Quoted 4 lines
grub-core/tpm2/module.c new file +4
-0+ +return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET , +N_ ("NV Index mode is not implemented yet" )); +}[...]
Quoted 7 lines
grub-core/tpm2/module.c line 1220
Quoted 4 lines
grub-core/tpm2/module.c new file +4
-0+GRUB_MOD_FINI (tpm2) +{ +grub_free ((void *) grub_tpm2_protector_ctx.keyfile); +grub_memset (&grub_tpm2_protector_ctx,0 ,sizeof (grub_tpm2_protector_ctx));I think this
grub_memset()is redundant just before module unload.
Yeah, there is nothing confidential in the struct. This one is not necessary.
Quoted 4 lines
Quoted 4 lines
grub-core/tpm2/module.c new file +4
-0+grub_key_protector_unregister (&grub_tpm2_key_protector); +grub_unregister_extcmd (grub_tpm2_protector_clear_cmd); +grub_unregister_extcmd (grub_tpm2_protector_init_cmd); +}
Quoted 9 lines
grub-core/tpm2/tpm2key.asn
Quoted 6 lines
grub-core/tpm2/tpm2key.asn new file +0
-0@@ -0,0 +1,33 @@Missing license...
Will add the license in v18.
Quoted 103 lines
Quoted 103 lines
grub-core/tpm2/tpm2key.asn new file +33
-0+-- +-- TPM 2.0 key file format +-- To generate tpm2key_asn1_tab.c: asn1Parser tpm2key.asn +-- +TPM2KEY {} +DEFINITIONS IMPLICIT TAGS ::= + +BEGIN + +TPMPolicy ::= SEQUENCE { + CommandCode [0] EXPLICIT INTEGER, + CommandPolicy [1] EXPLICIT OCTET STRING +} + +TPMAuthPolicy ::= SEQUENCE { + Name [0] EXPLICIT UTF8String OPTIONAL, + Policy [1] EXPLICIT SEQUENCE OF TPMPolicy +} + +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 +} + +ENDgrub-core/tpm2/tpm2key.c new file +64
-0@@ -0,0 +1,475 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2023 SUSE LLC +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/list.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/tpm2/buffer.h> +#include <grub/tpm2/tpm2key.h> + +extern asn1_static_node tpm2key_asn1_tab[]; +const char *sealed_key_oid ="2.23.133.10.1.5" ; + +static int +asn1_allocate_and_read (asn1_node node,const char *name,void **content,grub_size_t *content_size) +{ +grub_uint8_t *tmpstr =NULL ; +int tmpstr_size =0 ; +int ret; + +if (content ==NULL ) +return ASN1_MEM_ERROR ; + + ret =asn1_read_value (node, name,NULL , &tmpstr_size); +if (ret !=ASN1_MEM_ERROR ) +return ret; + + tmpstr =grub_malloc (tmpstr_size); +if (tmpstr ==NULL ) +return ASN1_MEM_ERROR ; + + ret =asn1_read_value (node, name, tmpstr, &tmpstr_size); +if (ret !=ASN1_SUCCESS ) +return ret; + + *content = tmpstr; + *content_size = tmpstr_size; + +return ASN1_SUCCESS ; +} + +static int +asn1_read_uint32 (asn1_node node,const char *name,grub_uint32_t *out) +{ +grub_uint32_t tmp =0 ; +grub_uint8_t *ptr; +void *data =NULL ; +grub_size_t data_size; +int ret; +
Quoted 9 lines
grub-core/tpm2/tpm2key.c line 69
Quoted 5 lines
grub-core/tpm2/tpm2key.c new file +5
-0+ ret =asn1_allocate_and_read (node, name, &data, &data_size); +if (ret !=ASN1_SUCCESS ) +return ret; + +if (data_size >4 )Is it possible to get 3 or less here? If yes then we should check for this too. Or s/>/!=/...
ASN.1 encoding only tells the number of octects for the integer so there could be 1 to hundreds of octects. Here we want a UINT32, so 1~4 octects are valid.
Quoted 9 lines
grub-core/tpm2/tpm2key.c line 77
Quoted 8 lines
grub-core/tpm2/tpm2key.c new file +8
-0+ { + ret =ASN1_MEM_ERROR ; + goto error; + } + +/* convert the big-endian integer to host uint32 */ + ptr = (grub_uint8_t *)&tmp + (4 - data_size); +grub_memcpy (ptr, data, data_size);
Quoted 3 lines
Could you explain this? Why
grub_be_to_cpu32()is not enough? Is it related to alignment? If yes you could usegrub_get_unaligned32().
The data_size could 1 to 4, so I have to copy data to tmp to make it a big-endian uint32 and convert it.
Quoted 28 lines
Quoted 8 lines
grub-core/tpm2/tpm2key.c new file +8
-0+ tmp =grub_be_to_cpu32 (tmp); + + *out = tmp; +error: +if (data) +grub_free (data); +return ret; +}[...]
Quoted 17 lines
grub-core/tpm2/tpm2key.c new file +17
-0+grub_err_t +grub_tpm2key_get_authpolicy_seq (asn1_node tpm2key,tpm2key_authpolicy_t *authpol_seq) +{ +tpm2key_authpolicy_t tmp_seq =NULL ; +tpm2key_authpolicy_t authpol =NULL ; +int authpol_n; +char authpol_pol[AUTHPOLICY_POL_MAX ]; +int i; +int ret; +grub_err_t err; + + ret =asn1_number_of_elements (tpm2key,"authPolicy" , &authpol_n); +if (ret ==ASN1_ELEMENT_NOT_FOUND ) + { +/* "authPolicy" is optional, so it may not be available */ + *authpol_seq =NULL ; +return GRUB_ERR_NONE ;
Quoted 9 lines
grub-core/tpm2/tpm2key.c line 415
Quoted 6 lines
grub-core/tpm2/tpm2key.c new file +6
-0+ } +else if (ret !=ASN1_SUCCESS ) +return grub_error (GRUB_ERR_READ_ERROR ,N_ ("Failed to retrieve authPolicy" )); + +/* Limit the number of authPolicy elements to two digits (99) */ +if (authpol_n >100 || authpol_n <1 )I would define high/low policy limits as constants and use them everywhere.
At least 1 elements is expected, so we only need to define the upper limit.
Quoted 60 lines
Quoted 60 lines
grub-core/tpm2/tpm2key.c new file +60
-0+return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Invalid number of authPolicy elements" )); + +/* +* Iterate the authPolicy elements backwards since grub_list_push() prepends +* the item into the list. +*/ +for (i = authpol_n; i >=1 ; i--) { + authpol =grub_zalloc (sizeof (struct tpm2key_authpolicy )); +if (authpol ==NULL ) + { + err =grub_error (GRUB_ERR_OUT_OF_MEMORY , +N_ ("Failed to allocate memory for authPolicy" )); + goto error; + } +grub_snprintf (authpol_pol,AUTHPOLICY_POL_MAX ,"authPolicy.?%d.Policy" , i); + + ret =tpm2key_get_policy_seq (tpm2key, authpol_pol, &authpol->policy_seq); +if (ret !=ASN1_SUCCESS ) + { + err =grub_error (GRUB_ERR_READ_ERROR , +N_ ("Failed to retrieve policy from authPolicy" )); + goto error; + } + +/* Prepend the authPolicy element into the sequence */ +grub_list_push (GRUB_AS_LIST_P (&tmp_seq),GRUB_AS_LIST (authpol)); + } + + *authpol_seq = tmp_seq; + +return GRUB_ERR_NONE ; + +error: +if (authpol) + { +grub_tpm2key_free_policy_seq (authpol->policy_seq); +grub_free (authpol); + } + +grub_tpm2key_free_authpolicy_seq (tmp_seq); + +return err; +} + +void +grub_tpm2key_free_authpolicy_seq (tpm2key_authpolicy_t authpol_seq) +{ +tpm2key_authpolicy_t authpol; +tpm2key_authpolicy_t next; + +if (authpol_seq ==NULL ) +return ; + + FOR_LIST_ELEMENTS_SAFE (authpol, next, authpol_seq) + { +grub_tpm2key_free_policy_seq (authpol->policy_seq); +grub_free (authpol); + } +}
Quoted 9 lines
grub-core/tpm2/tpm2key_asn1_tab.c
Quoted 6 lines
grub-core/tpm2/tpm2key_asn1_tab.c new file +0
-0@@ -0,0 +1,45 @@Missing license here.
Will add the license in v18.
Gary Lin
Quoted 48 lines
Quoted 35 lines
grub-core/tpm2/tpm2key_asn1_tab.c new file +35
-0+/* +* This file is generated by 'asn1Parser tpm2key.asn' and the '#include' +* headers are replaced with the ones in grub2. +* - 'grub/mm.h' for the definition of 'NULL' +* - 'libtasn1.h' for the definition of 'asn1_static_node' +*/ + +#include <grub/mm.h> +#include <libtasn1.h> + +const asn1_static_node tpm2key_asn1_tab[] = { + {"TPM2KEY" ,536875024 ,NULL }, + {NULL ,1073741836 ,NULL }, + {"TPMPolicy" ,1610612741 ,NULL }, + {"CommandCode" ,1610620931 ,NULL }, + {NULL ,2056 ,"0" }, + {"CommandPolicy" ,536879111 ,NULL }, + {NULL ,2056 ,"1" }, + {"TPMAuthPolicy" ,1610612741 ,NULL }, + {"Name" ,1610637346 ,NULL }, + {NULL ,2056 ,"0" }, + {"Policy" ,536879115 ,NULL }, + {NULL ,1073743880 ,"1" }, + {NULL ,2 ,"TPMPolicy" }, + {"TPMKey" ,536870917 ,NULL }, + {"type" ,1073741836 ,NULL }, + {"emptyAuth" ,1610637316 ,NULL }, + {NULL ,2056 ,"0" }, + {"policy" ,1610637323 ,NULL }, + {NULL ,1073743880 ,"1" }, + {NULL ,2 ,"TPMPolicy" }, + {"secret" ,1610637319 ,NULL }, + {NULL ,2056 ,"2" }, + {"authPolicy" ,1610637323 ,NULL }, + {NULL ,1073743880 ,"3" },grub-core/tpm2/tpm2key_asn1_tab.c line 45
Quoted 10 lines
grub-core/tpm2/tpm2key_asn1_tab.c new file +10
-0+ { NULL, 2, "TPMAuthPolicy"}, + { "description", 1610637346, NULL }, + { NULL, 2056, "4"}, + { "rsaParent", 1610637316, NULL }, + { NULL, 2056, "5"}, + { "parent", 1073741827, NULL }, + { "pubkey", 1073741831, NULL }, + { "privkey", 7, NULL }, + { NULL, 0, NULL } +};Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v17 11/20] key_protector: Add TPM2 Key Protector
Quoted 165 lines · Gary Lin
Quoted 154 lines · Daniel Kiper
Quoted 153 lines · Gary Lin
From: Hernan Gatta <[email protected]>
The TPM2 key protector is a module that enables the automatic retrieval of a fully-encrypted disk's unlocking key from a TPM 2.0.
The theory of operation is such that the module accepts various arguments, most of which are optional and therefore possess reasonable defaults. One of these arguments is the keyfile/tpm2key parameter, which is mandatory. There are two supported key formats:
1. Raw Sealed Key (--keyfile)
When sealing a key withTPM2_Create, the public portion of the sealed key is stored inTPM2B_PUBLIC, and the private portion is inTPM2B_PRIVATE. The raw sealed key glues the fully marshalledTPM2B_PUBLICandTPM2B_PRIVATEinto one file.2. TPM 2.0 Key (--tpm2key)
The following is the ASN.1 definition of TPM 2.0 Key File:TPMPolicy ::= SEQUENCE { CommandCode [0] EXPLICIT INTEGER CommandPolicy [1] EXPLICIT OCTET STRING } TPMAuthPolicy ::= SEQUENCE { Name [0] EXPLICIT UTF8STRING OPTIONAL Policy [1] EXPLICIT SEQUENCE OF TPMPolicy }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 OPTIONALdescription [4] EXPLICIT UTF8String OPTIONAL,
rsaParent [5] EXPLICIT BOOLEAN OPTIONAL,
parent INTEGER
pubkey OCTET STRING
privkey OCTET STRING
}The TPM2 key protector only expects a "sealed" key in DER encoding, so 'type' is always 2.23.133.10.1.5, 'emptyAuth' is 'TRUE', and 'secret' is empty. 'policy' and 'authPolicy' are the possible policy command sequences to construst the policy digest to unseal the key. Similar to the raw sealed key, the public portion (
TPM2B_PUBLIC) of the sealed key is stored in 'pubkey', and the private portion (TPM2B_PRIVATE) is in 'privkey'.For more details: https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
This sealed key file is created via the grub-protect tool. The tool utilizes the TPM's sealing functionality to seal (i.e., encrypt) an unlocking key using a Storage Root Key (SRK) to the values of various Platform Configuration Registers (PCRs). These PCRs reflect the state of the system as it boots. If the values are as expected, the system may be considered trustworthy, at which point the TPM allows for a caller to utilize the private component of the SRK to unseal (i.e., decrypt) the sealed key file. The caller, in this case, is this key protector.
The TPM2 key protector registers two commands:
-
tpm2_key_protector_init: Initializes the state of the TPM2 key protector for later usage, clearing any previous state, too, if any.-
tpm2_key_protector_clear: Clears any state set bytpm2_key_protector_init.The way this is expected to be used requires the user to, either interactively or, normally, via a boot script, initialize/configure the key protector and then specify that it be used by the 'cryptomount' command (modifications to this command are in a different patch).
For instance, to unseal the raw sealed key file:
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub2/sealed-1.key cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--keyfile=(hd0,gpt1)/efi/grub2/sealed-2.key --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2Or, to unseal the TPM 2.0 Key file:
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub2/sealed-1.tpm cryptomount -u <PART1_UUID> -P tpm2
tpm2_key_protector_init--tpm2key=(hd0,gpt1)/efi/grub2/sealed-2.tpm --pcrs=7,11 cryptomount -u <PART2_UUID> -P tpm2If a user does not initialize the key protector and attempts to use it anyway, the protector returns an error.
Before unsealing the key, the TPM2 key protector follows the "TPMPolicy" sequences to enforce the TPM policy commands to construct a valid policy digest to unseal the key.
For the TPM 2.0 Key files, 'authPolicy' may contain multiple "TPMPolicy" sequences, the TPM2 key protector iterates 'authPolicy' to find a valid sequence to unseal key. If 'authPolicy' is empty or all sequences in 'authPolicy' fail, the protector tries the one from 'policy'. In case 'policy' is also empty, the protector creates a "TPMPolicy" sequence based on the given PCR selection.
For the raw sealed key, the TPM2 key protector treats the key file as a TPM 2.0 Key file without 'authPolicy' and 'policy', so the "TPMPolicy" sequence is always based on the PCR selection from the command parameters.
This commit only supports one policy command:
TPM2_PolicyPCR. The command set will be extended to support advanced features, such as authorized policy, in the later commits.Cc Stefan Berger <[email protected]>Cc James Bottomley <[email protected]>Signed-off-by Hernan Gatta <[email protected]>Signed-off-by Gary Lin <[email protected]>--- grub-core/Makefile.core.def | 14 + grub-core/tpm2/args.c | 140 ++++ grub-core/tpm2/module.c | 1225 +++++++++++++++++++++++++++++ grub-core/tpm2/tpm2key.asn | 33 + grub-core/tpm2/tpm2key.c | 475 +++++++++++ grub-core/tpm2/tpm2key_asn1_tab.c | 45 ++ include/grub/tpm2/internal/args.h | 49 ++ include/grub/tpm2/tpm2key.h | 86 ++ 8 files changed, 2067 insertions(+) create mode 100644 grub-core/tpm2/args.c create mode 100644 grub-core/tpm2/module.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/tpm2/internal/args.h create mode 100644 include/grub/tpm2/tpm2key.hgrub-core/Makefile.core.def +9
-0@@ -2566,6 +2566,20 @@ module = { enable = efi; }; +module = { + name = tpm2; + common = tpm2/args.c; + common = tpm2/buffer.c; + common = tpm2/module.c; + common = tpm2/mu.c; + common = tpm2/tpm2.c; + common = tpm2/tpm2key.c; + common = tpm2/tpm2key_asn1_tab.c;Quoted 9 lines
grub-core/Makefile.core.def module = { · line 2582
Quoted 5 lines
grub-core/Makefile.core.def +5
-0+ efi = tpm2/tcg2.c; + enable = efi; + cppflags = '-I$(srcdir)/lib/libtasn1-grub'; +}; +I think the TPM2 key protector should be in separate GRUB module, i.e. not integrated with tss2 and tpm2 modules.
Quoted 3 lines
Hmmm, I'm thinking about putting the tss2 parts in grub-core/lib/ since buffer.c/mu.c/tpm2.c/tcg2 do not provide any grub commands and are more like a library.
Good point! Go ahead!
Quoted 62 lines
Quoted 53 lines
Quoted 53 lines
grub-core/Makefile.core.def +0
-0module = { name = tr; common = commands/tr.c;grub-core/tpm2/args.c new file +44
-0@@ -0,0 +1,140 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Microsoft Corporation +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/err.h> +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/tpm2/internal/args.h> + +grub_err_t +grub_tpm2_protector_parse_pcrs (char *value,grub_uint8_t *pcrs, +grub_uint8_t *pcr_count) +{ +char *current_pcr = value; +char *next_pcr; +unsigned long pcr; +grub_uint8_t i; + +if (grub_strlen (value) ==0 ) +return GRUB_ERR_BAD_ARGUMENT ; + + *pcr_count =0 ; +for (i =0 ; i <TPM_MAX_PCRS ; i++) + { + next_pcr =grub_strchr (current_pcr,',' ); +if (next_pcr == current_pcr) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Empty entry in PCR list" )); +if (next_pcr) + *next_pcr ='\0' ;Quoted 9 lines
grub-core/tpm2/args.c line 48
Quoted 4 lines
grub-core/tpm2/args.c new file +4
-0+ + grub_errno =GRUB_ERR_NONE ; + pcr =grub_strtoul (current_pcr,NULL ,10 ); +if (grub_errno !=GRUB_ERR_NONE )This check is unreliable. Please take a look at commit
ac8a37dda(net/http: Allow use of non-standard TCP/IP ports) how is should be done properly.
Quoted 1 line
Thanks for the hint. Will improve the check in v18.
You are welcome!
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
grub-core/tpm2/args.c new file +5
-0+return grub_error (grub_errno, +N_ ("Entry '%s' in PCR list is not a number" ), + current_pcr); + +if (pcr >TPM_MAX_PCRS )Quoted 9 lines
grub-core/tpm2/args.c line 59
Quoted 6 lines
grub-core/tpm2/args.c new file +6
-0+return grub_error (GRUB_ERR_OUT_OF_RANGE , +N_ ("Entry %lu in PCR list is too large to be a PCR " +"number, PCR numbers range from 0 to %u" ), + pcr,TPM_MAX_PCRS ); + + pcrs[i] = (grub_uint8_t )pcr;Missing space after ")".
Quoted 7 lines
Will fix it in v18.
Quoted 4 lines
grub-core/tpm2/args.c line 60
Quoted 1 line
grub-core/tpm2/args.c new file +1
-0+ *pcr_count +=1 ;++(*pcr_count); ???
Those look the same to me here. Is there any functional difference?
No, just matter of taste.
[...]
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
grub-core/tpm2/args.c new file +9
-0+static int +asn1_read_uint32 (asn1_node node,const char *name,grub_uint32_t *out) +{ +grub_uint32_t tmp =0 ; +grub_uint8_t *ptr; +void *data =NULL ; +grub_size_t data_size; +int ret; +Quoted 9 lines
grub-core/tpm2/args.c line 74
Quoted 5 lines
grub-core/tpm2/args.c new file +5
-0+ ret =asn1_allocate_and_read (node, name, &data, &data_size); +if (ret !=ASN1_SUCCESS ) +return ret; + +if (data_size >4 )Is it possible to get 3 or less here? If yes then we should check for this too. Or s/>/!=/...
Quoted 3 lines
ASN.1 encoding only tells the number of octects for the integer so there could be 1 to hundreds of octects. Here we want a UINT32, so 1~4 octects are valid.
OK, we need a comment before the function then. Otherwise its name and the code is confusing.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
grub-core/tpm2/args.c new file +8
-0+ { + ret =ASN1_MEM_ERROR ; + goto error; + } + +/* convert the big-endian integer to host uint32 */ + ptr = (grub_uint8_t *)&tmp + (4 - data_size); +grub_memcpy (ptr, data, data_size);
Quoted 5 lines
Quoted 3 lines
Could you explain this? Why
grub_be_to_cpu32()is not enough? Is it related to alignment? If yes you could usegrub_get_unaligned32().The
data_sizecould 1 to 4, so I have to copy data to tmp to make it a big-endian uint32 and convert it.
Please be more verbose in the comment then.
Quoted 37 lines
Quoted 28 lines
Quoted 8 lines
grub-core/tpm2/args.c new file +8
-0+ tmp =grub_be_to_cpu32 (tmp); + + *out = tmp; +error: +if (data) +grub_free (data); +return ret; +}[...]
Quoted 17 lines
grub-core/tpm2/args.c new file +17
-0+grub_err_t +grub_tpm2key_get_authpolicy_seq (asn1_node tpm2key,tpm2key_authpolicy_t *authpol_seq) +{ +tpm2key_authpolicy_t tmp_seq =NULL ; +tpm2key_authpolicy_t authpol =NULL ; +int authpol_n; +char authpol_pol[AUTHPOLICY_POL_MAX ]; +int i; +int ret; +grub_err_t err; + + ret =asn1_number_of_elements (tpm2key,"authPolicy" , &authpol_n); +if (ret ==ASN1_ELEMENT_NOT_FOUND ) + { +/* "authPolicy" is optional, so it may not be available */ + *authpol_seq =NULL ; +return GRUB_ERR_NONE ;Quoted 9 lines
grub-core/tpm2/args.c
Quoted 6 lines
grub-core/tpm2/args.c new file +6
-0+ } +else if (ret !=ASN1_SUCCESS ) +return grub_error (GRUB_ERR_READ_ERROR ,N_ ("Failed to retrieve authPolicy" )); + +/* Limit the number of authPolicy elements to two digits (99) */ +if (authpol_n >100 || authpol_n <1 )I would define high/low policy limits as constants and use them everywhere.
Quoted 2 lines
At least 1 elements is expected, so we only need to define the upper limit.
I would define both for completeness.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 12/20 cryptodisk: Support key protectors
In reply to Gary Lin via Grub-develFrom: Hernan Gatta <[email protected]>
Add a new parameter to cryptomount to support the key protectors framework: -P. The parameter is used to automatically retrieve a key from specified key protectors. The parameter may be repeated to specify any number of key protectors. These are tried in order until one provides a usable key for any given disk.
--- Makefile.util.def | 1 + grub-core/disk/cryptodisk.c | 243 ++++++++++++++++++++++++++---------- include/grub/cryptodisk.h | 16 +++ 3 files changed, 196 insertions(+), 64 deletions(-)
Makefile.util.def +1 -0
@@ -40,6 +40,7 @@ library = { common = grub-core/disk/luks.c; common = grub-core/disk/luks2.c; common = grub-core/disk/geli.c; + common = grub-core/disk/key_protector.c; common = grub-core/disk/cryptodisk.c; common = grub-core/disk/AFSplitter.c; common = grub-core/lib/pbkdf2.c;
grub-core/disk/cryptodisk.c +179 -64
343 lines not on this page · raw message
include/grub/cryptodisk.h +16 -0
@@ -70,6 +70,18 @@ typedef gcry_err_code_t (*grub_cryptodisk_rekey_func_t) (struct grub_cryptodisk *dev, grub_uint64_t zoneno); +struct grub_cryptomount_cached_key +{ +grub_uint8_t *key; +grub_size_t key_len; + +/* +* The key protector associated with this cache entry failed, so avoid it +* even if the cached entry (an instance of this structure) is empty. +*/ +int invalid; +}; +struct grub_cryptomount_args {/* scan: Flag to indicate that only bootable volumes should be decrypted */
@@ -81,6 +93,10 @@ struct grub_cryptomount_args/* recover_key: Length of key_data */ grub_size_t key_len;grub_file_t hdr_file; +/* recover_key: Names of the key protectors to use (NULL-terminated) */ +char **protectors; +/* recover_key: Key cache to avoid invoking the same key protector twice */ +struct grub_cryptomount_cached_key *key_cache; };typedef struct grub_cryptomount_args *grub_cryptomount_args_t ;
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 13/20 util/grub-protect: Add new tool
In reply to Gary Lin via Grub-develFrom: Hernan Gatta <[email protected]>
To utilize the key protectors framework, there must be a way to protect full-disk encryption keys in the first place. The grub-protect tool includes support for the TPM2 key protector but other protectors that require setup ahead of time can be supported in the future.
For the TPM2 key protector, the intended flow is for a user to have a LUKS 1 or LUKS 2-protected fully-encrypted disk. The user then creates a new LUKS key file, say by reading /dev/urandom into a file, and creates a new LUKS key slot for this key. Then, the user invokes the grub-protect tool to seal this key file to a set of PCRs using the system's TPM 2.0. The resulting sealed key file is stored in an unencrypted partition such as the EFI System Partition (ESP) so that GRUB may read it. The user also has to ensure the cryptomount command is included in GRUB's boot script and that it carries the requisite key protector (-P) parameter.
Sample usage:
$ dd if=/dev/urandom of=luks-key bs=1 count=32 $ sudo cryptsetup luksAddKey /dev/sdb1 luks-key --pbkdf=pbkdf2 --hash=sha512
To seal the key with TPM 2.0 Key File (recommended):
$ sudo grub-protect --action=add \
--protector=tpm2 \
--tpm2-pcrs=0,2,4,7,9 \
--tpm2key \
--tpm2-keyfile=luks-key \
--tpm2-outfile=/boot/efi/boot/grub2/sealed.tpmOr, to seal the key with the raw sealed key:
$ sudo grub-protect --action=add \
--protector=tpm2 \
--tpm2-pcrs=0,2,4,7,9 \
--tpm2-keyfile=luks-key \
--tpm2-outfile=/boot/efi/boot/grub2/sealed.keyThen, in the boot script, for TPM 2.0 Key File:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -u <SDB1_UUID> -P tpm2
Or, for the raw sealed key:
tpm2_key_protector_init --keyfile=(hd0,gpt1)/boot/grub2/sealed.key --pcrs=0,2,4,7,9 cryptomount -u <SDB1_UUID> -P tpm2
The benefit of using TPM 2.0 Key File is that the PCR set is already written in the key file, so there is no need to specify PCRs when invoking tpm2_key_protector_init.
--- .gitignore | 2 + Makefile.util.def | 24 + configure.ac | 30 + docs/man/grub-protect.h2m | 4 + util/grub-protect.c | 1420 +++++++++++++++++++++++++++++++++++++ 5 files changed, 1480 insertions(+) create mode 100644 docs/man/grub-protect.h2m create mode 100644 util/grub-protect.c
.gitignore +2 -0
@@ -169,6 +169,8 @@ widthspec.bin /grub-ofpathname.exe /grub-probe /grub-probe.exe +/grub-protect +/grub-protect.exe /grub-reboot /grub-render-label /grub-render-label.exe
Makefile.util.def +24 -0
@@ -208,6 +208,30 @@ program = { ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; }; +program = { + name = grub-protect; + mansection = 1; + + common = grub-core/kern/emu/argp_common.c; + common = grub-core/osdep/init.c; + common = grub-core/tpm2/args.c; + common = grub-core/tpm2/buffer.c; + common = grub-core/tpm2/mu.c; + common = grub-core/tpm2/tpm2.c; + common = grub-core/tpm2/tpm2key_asn1_tab.c; + common = util/grub-protect.c; + common = util/probe.c; + + ldadd = libgrubmods.a; + ldadd = libgrubgcry.a; + ldadd = libgrubkern.a; + ldadd = grub-core/lib/gnulib/libgnu.a; + ldadd = '$(LIBTASN1)'; + ldadd = '$(LIBINTL) $(LIBDEVMAPPER) $(LIBUTIL) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; + + condition = COND_GRUB_PROTECT; +}; + program = { name = grub-mkrelpath; mansection = 1;
configure.ac +30 -0
@@ -76,6 +76,7 @@ grub_TRANSFORM([grub-mkpasswd-pbkdf2]) grub_TRANSFORM([grub-mkrelpath]) grub_TRANSFORM([grub-mkrescue]) grub_TRANSFORM([grub-probe]) +grub_TRANSFORM([grub-protect]) grub_TRANSFORM([grub-reboot]) grub_TRANSFORM([grub-script-check]) grub_TRANSFORM([grub-set-default])
@@ -2057,6 +2058,29 @@ fi AC_SUBST([LIBZFS]) AC_SUBST([LIBNVPAIR]) +AC_ARG_ENABLE([grub-protect], + [AS_HELP_STRING([--enable-grub-protect], + [build and install the `grub-protect' utility (default=guessed)])]) +if test x"$enable_grub_protect" = xno ; then + grub_protect_excuse="explicitly disabled" +fi + +LIBTASN1= +if test x"$grub_protect_excuse" = x ; then + AC_CHECK_LIB([tasn1], [asn1_write_value], [LIBTASN1="-ltasn1"], [grub_protect_excuse="need libtasn1 library"]) +fi +AC_SUBST([LIBTASN1]) + +if test x"$enable_grub_protect" = xyes && test x"$grub_protect_excuse" != x ; then + AC_MSG_ERROR([grub-protect was explicitly requested but can't be compiled ($grub_protect_excuse)]) +fi +if test x"$grub_protect_excuse" = x ; then +enable_grub_protect=yes +else +enable_grub_protect=no +fi +AC_SUBST([enable_grub_protect]) + LIBS="" AC_SUBST([FONT_SOURCE])
@@ -2173,6 +2197,7 @@ AM_CONDITIONAL([COND_GRUB_EMU_SDL], [test x$enable_grub_emu_sdl = xyes]) AM_CONDITIONAL([COND_GRUB_EMU_PCI], [test x$enable_grub_emu_pci = xyes]) AM_CONDITIONAL([COND_GRUB_MKFONT], [test x$enable_grub_mkfont = xyes]) AM_CONDITIONAL([COND_GRUB_MOUNT], [test x$enable_grub_mount = xyes]) +AM_CONDITIONAL([COND_GRUB_PROTECT], [test x$enable_grub_protect = xyes]) AM_CONDITIONAL([COND_HAVE_FONT_SOURCE], [test x$FONT_SOURCE != x]) if test x$FONT_SOURCE != x ; then HAVE_FONT_SOURCE=1
@@ -2300,6 +2325,11 @@ echo grub-mount: Yes else echo grub-mount: No "($grub_mount_excuse)" fi +if [ x"$grub_protect_excuse" = x ]; then +echo grub-protect: Yes +else +echo grub-protect: No "($grub_protect_excuse)" +fi if [ x"$starfield_excuse" = x ]; then echo starfield theme: Yes echo With DejaVuSans font from $DJVU_FONT_SOURCE
docs/man/grub-protect.h2m new file +4 -0
@@ -0,0 +1,4 @@ +[NAME] +grub-protect \- protect a disk key with a key protector +[DESCRIPTION] +grub-protect helps to pretect a disk encryption key with a specified key protector.
util/grub-protect.c new file +1420 -0
1,420 lines not on this page · raw message
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 14/20 tpm2: Support authorized policy
In reply to Gary Lin via Grub-develThis commit handles the TPM2_PolicyAuthorize command from the key file in TPM 2.0 Key File format.
TPM2_PolicyAuthorize is the essential command to support authorized policy which allows the users to sign TPM policies with their own keys. Per TPM 2.0 Key File(*1), CommandPolicy for TPM2_PolicyAuthorize comprises 'TPM2B_PUBLIC pubkey', 'TPM2B_DIGEST policy_ref', and 'TPMT_SIGNATURE signature'. To verify the signature, the current policy digest is hashed with the hash algorithm written in 'signature', and then 'signature' is verified with the hashed policy digest and 'pubkey'. Once TPM accepts 'signature', TPM2_PolicyAuthorize is invoked to authorize the signed policy.
To create the key file with authorized policy, here are the pcr-oracle(*2) commands:
# Generate the RSA key and create the authorized policy file
$ pcr-oracle \ --rsa-generate-key \ --private-key policy-key.pem \ --auth authorized.policy \ create-authorized-policy 0,2,4,7,9
# Seal the secret with the authorized policy
$ pcr-oracle \ --key-format tpm2.0 \ --auth authorized.policy \ --input disk-secret.txt \ --output sealed.key \ seal-secret
# Sign the predicted PCR policy
$ pcr-oracle \ --key-format tpm2.0 \ --private-key policy-key.pem \ --from eventlog \ --stop-event "grub-file=grub.cfg" \ --after \ --input sealed.key \ --output sealed.tpm \ sign 0,2,4,7,9
Then specify the key file and the key protector to grub.cfg in the EFI system partition:
tpm2_key_protector_init -a RSA --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -u <PART_UUID> -P tpm2
For any change in the boot components, just run the 'sign' command again to update the signature in sealed.tpm, and TPM can unseal the key file with the updated PCR policy.
(*1) https://www.hansenpartnership.com/draft-bottomley-tpm2-keys.html
(*2) https://github.com/okirch/pcr-oracle
--- grub-core/tpm2/module.c | 84 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+)
grub-core/tpm2/module.c +84 -0
@@ -649,6 +649,87 @@ grub_tpm2_protector_policypcr (TPMI_SH_AUTH_SESSION session,return GRUB_ERR_NONE ; } +static grub_err_t +grub_tpm2_protector_policyauthorize (TPMI_SH_AUTH_SESSION session, +struct grub_tpm2_buffer *cmd_buf) +{ +TPM2B_PUBLIC pubkey; +TPM2B_DIGEST policy_ref; +TPMT_SIGNATURE signature; +TPM2B_DIGEST pcr_policy; +TPM2B_DIGEST pcr_policy_hash; +TPMI_ALG_HASH sig_hash; +TPMT_TK_VERIFIED verification_ticket; +TPM_HANDLE pubkey_handle =0 ; +TPM2B_NAME pubname; +TPM_RC rc; +grub_err_t err; + +grub_tpm2_mu_TPM2B_PUBLIC_Unmarshal (cmd_buf, &pubkey); +grub_tpm2_mu_TPM2B_DIGEST_Unmarshal (cmd_buf, &policy_ref); +grub_tpm2_mu_TPMT_SIGNATURE_Unmarshal (cmd_buf, &signature); +if (cmd_buf->error !=0 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Failed to unmarshal the buffer for TPM2_PolicyAuthorize" )); + +/* Retrieve Policy Digest */ + rc =TPM2_PolicyGetDigest (session,NULL , &pcr_policy,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Failed to get policy digest (TPM2_PolicyGetDigest: 0x%x)." ), + rc); + +/* Calculate the digest of the polcy for VerifySignature */ + sig_hash =TPMT_SIGNATURE_get_hash_alg (&signature); +if (sig_hash ==TPM_ALG_NULL ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("Failed to get the hash algorithm of the signature" )); + + rc =TPM2_Hash (NULL , (TPM2B_MAX_BUFFER *)&pcr_policy, sig_hash, +TPM_RH_NULL , &pcr_policy_hash,NULL ,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Failed to create PCR policy hash (TPM2_Hash: 0x%x)" ), + rc); + +/* Load the public key */ + rc =TPM2_LoadExternal (NULL ,NULL , &pubkey,TPM_RH_OWNER , + &pubkey_handle, &pubname,NULL ); +if (rc !=TPM_RC_SUCCESS ) +return grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Failed to load public key (TPM2_LoadExternal: 0x%x)" ), + rc); + +/* Verify the signature against the public key and the policy digest */ + rc =TPM2_VerifySignature (pubkey_handle,NULL , &pcr_policy_hash, &signature, + &verification_ticket,NULL ); +if (rc !=TPM_RC_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Failed to verify signature (TPM2_VerifySignature: 0x%x)" ), + rc); + goto error; + } + +/* Authorize the signed policy with the public key and the verification ticket */ + rc =TPM2_PolicyAuthorize (session,NULL , &pcr_policy, &policy_ref, &pubname, + &verification_ticket,NULL ); +if (rc !=TPM_RC_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_DEVICE , +N_ ("Failed to authorize PCR policy (TPM2_PolicyAuthorize: 0x%x)" ), + rc); + goto error; + } + + err =GRUB_ERR_NONE ; + +error: +TPM2_FlushContext (pubkey_handle); + +return err; +} +static grub_err_t grub_tpm2_protector_enforce_policy (tpm2key_policy_t policy,TPMI_SH_AUTH_SESSION session) {
@@ -668,6 +749,9 @@ grub_tpm2_protector_enforce_policy (tpm2key_policy_t policy, TPMI_SH_AUTH_SESSIOcase TPM_CC_PolicyPCR: err =grub_tpm2_protector_policypcr (session, &buf);break ; +case TPM_CC_PolicyAuthorize: + err =grub_tpm2_protector_policyauthorize (session, &buf); +break ;default :return grub_error (GRUB_ERR_BAD_ARGUMENT ,N_ ("Unknown TPM Command: 0x%x" ), policy->cmd_code);
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 15/20 tpm2: Implement NV index
In reply to Gary Lin via Grub-develFrom: Patrick Colp <[email protected]>
Currently with the TPM2 protector, only SRK mode is supported and NV index support is just a stub. Implement the NV index option.
Note: This only extends support on the unseal path. grub2_protect has not been updated. tpm2-tools can be used to insert a key into the NV index.
An example of inserting a key using tpm2-tools:
# Get random key.
tpm2_getrandom 32 > key.dat
# Create primary object.
tpm2_createprimary -C o -g sha256 -G ecc -c primary.ctx
# Create policy object. pcrs.dat contains the PCR values to seal against. tpm2_startauthsession -S session.dat
tpm2_policypcr -S session.dat -l sha256:7,11 -f pcrs.dat -L policy.dat tpm2_flushcontext session.dat
# Seal key into TPM.
cat key.dat | tpm2_create -C primary.ctx -u key.pub -r key.priv -L policy.dat -i- tpm2_load -C primary.ctx -u key.pub -r key.priv -n sealing.name -c sealing.ctx tpm2_evictcontrol -C o -c sealing.ctx 0x81000000
Then to unseal the key in grub, add this to grub.cfg:
tpm2_key_protector_init --mode=nv --nvindex=0x81000000 --pcrs=7,11 cryptomount -u <UUID> --protector tpm2
--- grub-core/tpm2/module.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
grub-core/tpm2/module.c +20 -5
@@ -1034,12 +1034,27 @@ static grub_err_t grub_tpm2_protector_nv_recover (const struct grub_tpm2_protector_context *ctx, grub_uint8_t **key, grub_size_t *key_size) { - (void)ctx; - (void )key; - (void )key_size; + TPM_HANDLE sealed_handle = ctx->nv; +tpm2key_policy_t policy_seq =NULL ; +grub_err_t err; + +/* Create a basic policy sequence based on the given PCR selection */ + err =grub_tpm2_protector_simple_policy_seq (ctx, &policy_seq); +if (err !=GRUB_ERR_NONE ) + goto exit; + + err =grub_tpm2_protector_unseal (policy_seq, sealed_handle, key, key_size); + +/* Pop error messages on success */ +if (err ==GRUB_ERR_NONE ) +while (grub_error_pop ()); + +exit: +TPM2_FlushContext (sealed_handle); -return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET , -N_ ("NV Index mode is not implemented yet" )); +grub_tpm2key_free_policy_seq (policy_seq); + +return err; } static grub_err_t
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 16/20 cryptodisk: Fallback to passphrase
In reply to Gary Lin via Grub-develFrom: Patrick Colp <[email protected]>
If a protector is specified, but it fails to unlock the disk, fall back to asking for the passphrase. However, an error was set indicating that the protector(s) failed. Later code (e.g., LUKS code) fails as grub_errno is now set. Print the existing errors out first, before proceeding with the passphrase.
--- grub-core/disk/cryptodisk.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +16 -1
@@ -1167,6 +1167,10 @@ grub_cryptodisk_scan_device_real (const char *name, ret = cr->recover_key (source, dev, cargs);if (ret !=GRUB_ERR_NONE ) { +/* Reset key data to trigger the passphrase prompt later */ + cargs->key_data =NULL ; + cargs->key_len =0 ; + part =grub_partition_get_name (source->partition);grub_dprintf ("cryptodisk" , "recovered a key from key protector %s but it "
@@ -1192,7 +1196,6 @@ grub_cryptodisk_scan_device_real (const char *name, source->name, source->partition !=NULL ?"," :"" , part != NULL ? part : N_("UNKNOWN"), dev->uuid);grub_free (part); - goto error; }if (cargs->key_len)
@@ -1207,6 +1210,18 @@ grub_cryptodisk_scan_device_real (const char *name,unsigned long tries =3 ;const char *tries_env; +/* +* Print the error from key protectors and clear grub_errno. +* Since '--protector' doesn't not coexist with '--password' and +* '--key-file', only "cargs->key_len == 0" is expected if all +* key protectors fail. +*/ +if (grub_errno) + { +grub_print_error (); + grub_errno =GRUB_ERR_NONE ; + } + askpass =1 ; cargs->key_data =grub_malloc (GRUB_CRYPTODISK_MAX_PASSPHRASE );if (cargs->key_data ==NULL )
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 17/20 cryptodisk: wipe out the cached keys from protectors
In reply to Gary Lin via Grub-develAn attacker may insert a malicious disk with the same crypto UUID and trick grub2 to mount the fake root. Even though the key from the key protector fails to unlock the fake root, it's not wiped out cleanly so the attacker could dump the memory to retrieve the secret key. To defend such attack, wipe out the cached key when we don't need it.
--- grub-core/disk/cryptodisk.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/disk/cryptodisk.c +5 -1
@@ -1399,7 +1399,11 @@ grub_cryptodisk_clear_key_cache (struct grub_cryptomount_args *cargs)return ;for (i =0 ; cargs->protectors[i]; i++) -grub_free (cargs->key_cache[i].key); + { +if (cargs->key_cache[i].key) +grub_memset (cargs->key_cache[i].key,0 , cargs->key_cache[i].key_len); +grub_free (cargs->key_cache[i].key); + }grub_free (cargs->key_cache); }
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 18/20 diskfilter: look up cryptodisk devices first
In reply to Gary Lin via Grub-develWhen using disk auto-unlocking with TPM 2.0, the typical grub.cfg may look like this:
tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm cryptomount -u <PART-UUID> -P tpm2 search --fs-uuid --set=root <FS-UUID>
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 the malicious root and further dump memory to steal the unsealed key.
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 an encrypted partition, the search hint provided by 'grub-probe' is:
--hint='lvmid/<VG-UUID>/<LV-UUID>'
It doesn't guarantee to look up the logical volume from the encrypted partition, so the attacker may have the chance to fool grub2 to boot into the malicious disk.
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.
--- grub-core/disk/diskfilter.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-)
grub-core/disk/diskfilter.c +26 -9
@@ -226,15 +226,32 @@ scan_devices (const char *arname)int need_rescan;for (pull =0 ; pull <GRUB_DISK_PULL_MAX ; pull++) -for (p = grub_disk_dev_list; p; p = p->next) -if (p->id !=GRUB_DISK_DEVICE_DISKFILTER_ID - && p->disk_iterate) - { -if ((p->disk_iterate) (scan_disk_hook,NULL , pull)) -return ; -if (arname &&is_lv_readable (find_lv (arname),1 )) -return ; - } + { +/* look up the crytodisk devices first */ +for (p = grub_disk_dev_list; p; p = p->next) +if (p->id ==GRUB_DISK_DEVICE_CRYPTODISK_ID + && p->disk_iterate) + { +if ((p->disk_iterate) (scan_disk_hook,NULL , pull)) +return ; +if (arname &&is_lv_readable (find_lv (arname),1 )) +return ; +break ; + } + +/* check the devices other than crytodisk */ +for (p = grub_disk_dev_list; p; p = p->next) +if (p->id ==GRUB_DISK_DEVICE_CRYPTODISK_ID ) +continue ; +else if (p->id !=GRUB_DISK_DEVICE_DISKFILTER_ID + && p->disk_iterate) + { +if ((p->disk_iterate) (scan_disk_hook,NULL , pull)) +return ; +if (arname &&is_lv_readable (find_lv (arname),1 )) +return ; + } + } scan_depth =0 ; need_rescan =1 ;
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 19/20 tpm2: Enable tpm2 module for grub-emu
In reply to Gary Lin via Grub-develAs a preparation to test TPM 2.0 TSS stack with grub-emu, the new option, --tpm-device, is introduced to specify the TPM device for grub-emu so that grub-emu can share the emulated TPM device with the host.
Since grub-emu can directly access the device node on host, it's easy to implement the essential TCG2 command submission function with the read/write functions and enable tpm2 module for grub-emu, so that we can further test TPM key unsealing with grub-emu.
--- grub-core/Makefile.core.def | 2 ++ grub-core/kern/emu/main.c | 11 +++++++- grub-core/kern/emu/misc.c | 51 ++++++++++++++++++++++++++++++++++++ grub-core/tpm2/tcg2-emu.c | 52 +++++++++++++++++++++++++++++++++++++ include/grub/emu/misc.h | 5 ++++ 5 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 grub-core/tpm2/tcg2-emu.c
grub-core/Makefile.core.def +2 -0
@@ -2576,7 +2576,9 @@ module = { common = tpm2/tpm2key.c; common = tpm2/tpm2key_asn1_tab.c; efi = tpm2/tcg2.c; + emu = tpm2/tcg2-emu.c; enable = efi; + enable = emu; cppflags = '-I$(srcdir)/lib/libtasn1-grub'; };
grub-core/kern/emu/main.c +10 -1
@@ -55,7 +55,7 @@static jmp_buf main_env;/* Store the prefix specified by an argument. */ -static char *root_dev =NULL , *dir =NULL ; +static char *root_dev =NULL , *dir =NULL , *tpm_dev =NULL ;grub_addr_t grub_modbase =0 ;
@@ -108,6 +108,7 @@ static struct argp_option options[] = { {"verbose", 'v', 0, 0, N_("print verbose messages."), 0}, {"hold", 'H', N_("SECS"), OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0}, {"kexec", 'X', 0, 0, N_("use kexec to boot Linux kernels via systemctl (pass twice to enable dangerous fallback to non-systemctl)."), 0}, + {"tpm-device", 't', N_("DEV"), 0, N_("Set TPM device."), 0}, { 0, 0, 0, 0, 0, 0 } };
@@ -168,6 +169,10 @@ argp_parser (int key, char *arg, struct argp_state *state)case 'X' :grub_util_set_kexecute ();break ; +case 't' : +free (tpm_dev); + tpm_dev =xstrdup (arg); +break ;case ARGP_KEY_ARG : {
@@ -276,6 +281,9 @@ main (int argc, char *argv[]) dir =xstrdup (dir); +if (tpm_dev) +grub_util_tpm_open (tpm_dev); +/* Start GRUB! */ if (setjmp (main_env) ==0 )grub_main ();
@@ -283,6 +291,7 @@ main (int argc, char *argv[])grub_fini_all ();grub_hostfs_fini ();grub_host_fini (); +grub_util_tpm_close ();grub_machine_fini (GRUB_LOADER_FLAG_NORETURN );
grub-core/kern/emu/misc.c +51 -0
66 lines not on this page · raw message
grub-core/tpm2/tcg2-emu.c new file +52 -0
52 lines not on this page · raw message
include/grub/emu/misc.h +5 -0
@@ -75,4 +75,9 @@ grub_util_fopen (const char *path, const char *mode);int grub_util_file_sync (FILE *f); +grub_err_t grub_util_tpm_open (const char *tpm_dev); +grub_err_t grub_util_tpm_close (void ); +grub_size_t EXPORT_FUNC (grub_util_tpm_read ) (void *output,grub_size_t size); +grub_size_t EXPORT_FUNC (grub_util_tpm_write ) (const void *input,grub_size_t size); +#endif /* GRUB_EMU_MISC_H */
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v17 20/20 tests: Add tpm2_test
In reply to Gary Lin via Grub-develFor the tpm2 module, the TCG2 command submission function is the only difference between the a QEMU instance and grub-emu. To test TPM key unsealing with a QEMU instance, it requires an extra OS image to invoke grub-protect to seal the LUKS key, rather than a simple grub-shell rescue CD image. On the other hand, grub-emu can share the emulated TPM device with the host, so that we can seal the LUKS key on host and test key unsealing with grub-emu.
This test script firstly creates a simple LUKS image to be loaded as a loopback device in grub-emu. Then an emulated TPM device is created by "swtpm chardev" and PCR 0 and 1 are extended.
There are several test cases in the script to test various settings. Each test case uses grub-protect or tpm2-tools to seal the LUKS password against PCR 0 and PCR 1. Then grub-emu is launched to load the LUKS image, try to mount the image with tpm2_key_protector_init and cryptomount, and verify the result.
Based on the idea from Michael Chang.
--- Makefile.util.def | 6 + tests/tpm2_test.in | 389 +++++++++++++++++++++++++++++++++++++++ tests/util/grub-shell.in | 6 +- 3 files changed, 400 insertions(+), 1 deletion(-) create mode 100644 tests/tpm2_test.in
Makefile.util.def +6 -0
@@ -1288,6 +1288,12 @@ script = { common = tests/asn1_test.in; }; +script = { + testcase = native; + name = tpm2_test; + common = tests/tpm2_test.in; +}; + program = { testcase = native; name = example_unit_test;
tests/tpm2_test.in new file +389 -0
389 lines not on this page · raw message
tests/util/grub-shell.in +5 -1
@@ -75,6 +75,7 @@ work_directory=${WORKDIR:-`mktemp -d "${TMPDIR:-/tmp}/grub-shell.XXXXXXXXXX"`} | . "${builddir}/grub-core/modinfo.sh" qemuopts= +emuopts= serial_port=com0 serial_null= halt_cmd=halt
@@ -281,6 +282,9 @@ for option in "$@"; do --qemu-opts=*) qs=`echo "$option" | sed -e 's/--qemu-opts=//'` qemuopts="$qemuopts $qs" ;; + --emu-opts=*) + qs=`echo "$option" | sed -e 's/--emu-opts=//'` + emuopts="$emuopts $qs" ;; --disk=*) dsk=`echo "$option" | sed -e 's/--disk=//'` if [ ${grub_modinfo_platform} = emu ]; then
@@ -576,7 +580,7 @@ elif [ x$boot = xemu ]; then cat >"$work_directory/run.sh" <<EOF #! @BUILD_SHEBANG@ SDIR=\$(realpath -e \${0%/*}) -exec "$(realpath -e "${builddir}")/grub-core/grub-emu" -m "\$SDIR/${device_map##*/}" --memdisk "\$SDIR/${roottar##*/}" -r memdisk -d "/boot/grub" +exec "$(realpath -e "${builddir}")/grub-core/grub-emu" -m "\$SDIR/${device_map##*/}" --memdisk "\$SDIR/${roottar##*/}" -r memdisk -d "/boot/grub" ${emuopts} EOF else cat >"$work_directory/run.sh" <<EOF
Signature · 7 lines
2.35.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel