Skip to contentSkip to conversation

Appended Signature Secure Boot Support for PowerPC

PATCH Superseded Sudhakar Kuppusamy 2 participants 41 messages Jul 29, 2025 – Aug 14, 2025

v6 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v6 cover Appended Signature Secure Boot Support for PowerPC

This patch set contains v6 the consolidated version of the patch sets for secure boot using appended signatures on powerpc, rebased on top of git HEAD.

The v5 series is at
https://lists.gnu.org/archive/html/grub-devel/2025-07/msg00137.html

Changes since v5:
- Daniel Kiper review comments addressed:
- v6 patch 10: corrected code sytle, changed the function name from file_read_all to file_read_whole and drop enum for check_sigs.
- v6 patch 11: expaned commit message, removed convert_to_esl and
put its code where it called.added abbreviations for ESL, ESD and SD. defined grub_pks_use_keystore flag as bool
- v6 patch 12: corrected code sytle and commit message.
- v6 patch 15: corrected commit message.
- v6 patch 16: We tried use hexdump() to display hash data but it is typically displayed in hexadecimal format, along with an ASCII representation of hash data. So, introduced a new method dump_ascii_to_hex.
- v6 patch 19: corrected the document signing GRUB with an appended signature.
- v6 patch 20: merged from v5 patch 3 and 21. Properly documented GRUB commands and an appended signatures.

Linux on Power LPAR secure boot ensures the integrity of the Linux boot stack. The hypervisor and partition firmware are part of the core root of trust. The partition firmware verifies the signature on the GRUB image before handing control to GRUB. Similarly, GRUB verifies the signature on the kernel image before booting the OS. This ensures that every image running at the boot time is verified and trusted. UEFI platforms relies on PECOFF based signature scheme. Since Power is not a UEFI platform, an alternative mechanism is needed. Power already uses appended signatures on the Linux Kernel, and is now extended to sign the grub as well.

Linux on Power also allows multiple signers, and if any one of the signature passes, then the image passes the validation. Appended signature scheme uses CMS structure to contain signatures. On Power, the multiple signature support relies on the multiple signers features already supported by CMS standards. It does require that all the signers should sign at the same time and are not allowed to add or remove the signatures randomly.

By default, Linux LPAR secure boot uses static key management[1]. This means that each image embeds the keys it needs to verify the image it loads. For example, the keys used to verify the GRUB image are built into the firmware image. Similarly, the keys used for verifying the kernel image are built into the GRUB image. These are pre-defined keys and they cannot be modified at runtime. The drawback of this approach is that key rotations results in both firmware and OS updates. This is where dynamic key management is useful.

An admin can switch from static keys to dynamic keys by coordinating with Hardware Management Console(HMC) admin and enabling the required flags for the given LPAR.

The dynamic key management relies on the Platform KeyStore(PKS)[2] storage allocation for each LPAR with individually managed access controls to store sensitive information securely. Once switched to dynamic keys, HMC advertises this flag to the PowerVM, which then initializes the PKS with the default secvars. It also creates a variable SB_VERSION that represents the secure boot key management mode. The default secvars are used by Partition firmware, grub and the linux kernel to reads keys for verification. These secvars can be managed by user interface exposed via linux kernel. The linux kernel already supports this interface and it is available in the upstream kernel.

This patchset adds the appended signature support both for signing and verification and the key management to the grub component. The whole patchset can be split into following four main parts:

The series has following four main parts:

1.) Sign grub.elf with an appended signature. (Patches 1, 18, 19)

These patches provide some infrastructure and documentation for signing grub's core.elf with a Linux-kernel-module style appended signature.

An appended signature is a 'dumb' signature over the contents of a file. (It is distinct from schemes like Authenticode that are aware of the structure of the file and only sign certain parts.) The signature is wrapped in a PKCS#7 message, and is appended to the signed file along with some metadata and a magic string. The signatures are validated against a public key which is usually provided as an x509 certificate.

Because some platforms, such as powerpc-ieee1275, may load grub from a raw disk partition rather than a filesystem, we extend grub-install to add an ELF note that allows us to specify the size and location of the signature.

2.) Enable lockdown if secure boot is enabled (Patch 9)
Read secure boot mode from 'ibm,secure-boot' property and
If the 'ibm,secure-boot' property of the root node is 2, enter lockdown. Else it is considered as disabled.
There are three secure boot modes. They are
0 - disabled
No signature verification is performed. This is the default.
1 - audit
Signature verification is performed and if signature verification fails, post the errors and allow the boot to continue.
2 - enforced
Lockdown the GRUB. Signature verification is performed and If signature verification fails, post the errors and stop the boot.

Now, only support disabled and enforced.

3.) Enable appended signature verification using builtin keys (Patches 2 - 8 and 10).

Part of a secure boot chain is allowing grub to verify the boot kernel. For UEFI platforms, this is usually delegated to the shim. However, for platforms that do not implement UEFI, an alternative scheme is required.

This part teaches grub how to verify Linux kernel-style appended signatures. Kernels on powerpc are already signed with this scheme and can be verified by IMA for kexec.

As PKCS#7 messages and x509 certificates are both based on ASN.1, we import libtasn1 to parse them. Because ASN.1 isn't self-documenting, we import from GNUTLS the information we need to navigate their structure.

This section is composed of the following patches:

- patches 2 and 3 are small refactorings.

- patch 4 allows x509 certificates to be built in to the grub core in much the same way as PGP keys.

- patch 5 brings in the code from GNUTLS that allows us to parse PKCS#7 and x509 with libtasn1.

- patch 6, 7 and 8 is our ASN1 node, PKCS#7 and x509 parser. They're minimal and fairly strict parsers that extract only the bits we need to verify the signatures.

- patch 10 is the guts of the appended signature verifier. It uses the verifier infrastructure like pgp, and adds a number of user-friendly commands that mirror the pgp module.

4.) Enable accessing keys dynamically from Platform KeyStore (Patch 11 - 16)

This part teaches grub how to read db and dbx variables from platform keystore using client interface call then load keys from those two variable, and use it to verify Linux kernel.

This section is composed of the following patches:

- patch 11 is an exposes an interface in ieee1275 for reading secure boot variable db and dbx from Platform Keystore. Read secure boot variables such as db and dbx from PKS and extract certificates from ESL.

- patch 12 is create the db and dbx lists from PKS.

- patch 13 is verify the kernel using db and dbx lists

- patch 14 sets the use_static_keys flag if DB not available in PKS, and patch 15 is reads the DB default keys from ELF Note and store it in trusted lists if use_static_keys flag is set.

- patch 16 adds GRUB commands to access db and dbx.

5.) patch 17 adds unit test and 20 adds GRUB commands and an appended signatures
documentation.

Thanks to Daniel Kiper for providing review comments on v5.

I've pushed this all to
https://github.com/SudhakarKuppusamy1/grub/tree/appendedsig-2.13

[1]https://www.ibm.com/docs/en/linux-on-systems?topic=servers-guest-secure-boot-static-keys [2]https://community.ibm.com/community/user/power/blogs/chris-engel1/2020/11/20/powervm-introduces-the-platform-keystore

Daniel Axtens (2):
crypto: Move storage for grub_crypto_pk_* to crypto.c docs/grub: Document signing GRUB under UEFI

Sudhakar Kuppusamy (18):
powerpc-ieee1275: Add support for signing GRUB with an appended
signature
pgp: Rename OBJ_TYPE_PUBKEY to OBJ_TYPE_GPG_PUBKEY
grub-install: Support embedding x509 certificates
appended signatures: Import GNUTLS's ASN.1 description files appended signatures: Parse ASN1 node
appended signatures: Parse PKCS#7 signedData
appended signatures: Parse X.509 certificates
powerpc_ieee1275: Enter lockdown based on /ibm,secure-boot appended signatures: Support verifying appended signatures
powerpc_ieee1275: Read the db and dbx secure boot variables appended signatures: Create db and dbx lists
appended signatures: Using db and dbx lists for signature verification
powerpc_ieee1275: Introduce use_static_keys flag
appended signatures: Read default db keys from the ELF Note appended signatures: Introduce GRUB commands to access db and dbx appended signatures: Verification tests
docs/grub: Document signing GRUB with an appended signature docs/grub: Document appended signature

 docs/grub.texi                                |  384 +++-
 grub-core/Makefile.am                         |    2 +
 grub-core/Makefile.core.def                   |   24 +
 grub-core/commands/appendedsig/appendedsig.c  | 1589 +++++++++++++++++
 grub-core/commands/appendedsig/appendedsig.h  |  111 ++
 grub-core/commands/appendedsig/asn1util.c     |   96 +
 .../commands/appendedsig/gnutls_asn1_tab.c    |  148 ++
 grub-core/commands/appendedsig/pkcs7.c        |  454 +++++
 .../commands/appendedsig/pkix_asn1_tab.c      |  485 +++++
 grub-core/commands/appendedsig/x509.c         |  957 ++++++++++
 grub-core/commands/pgp.c                      |    6 +-
 grub-core/kern/ieee1275/ieee1275.c            |    1 -
 grub-core/kern/ieee1275/init.c                |   67 +
 grub-core/kern/powerpc/ieee1275/ieee1275.c    |  139 ++
 .../kern/powerpc/ieee1275/platform_keystore.c |  343 ++++
 grub-core/lib/crypto.c                        |    4 +
 grub-core/tests/appended_signature_test.c     |  241 +++
 grub-core/tests/appended_signatures.h         |  975 ++++++++++
 grub-core/tests/lib/functional_test.c         |    5 +
 include/grub/crypto.h                         |    1 +
 include/grub/efi/pks.h                        |  112 ++
 include/grub/file.h                           |    4 +
 include/grub/ieee1275/ieee1275.h              |   22 +
 include/grub/kernel.h                         |    3 +-
 include/grub/lockdown.h                       |    3 +-
 include/grub/powerpc/ieee1275/ieee1275.h      |   20 +
 .../grub/powerpc/ieee1275/platform_keystore.h |  115 ++
 include/grub/types.h                          |    4 +
 include/grub/util/install.h                   |   10 +-
 include/grub/util/mkimage.h                   |    4 +-
 util/grub-install-common.c                    |   42 +-
 util/grub-mkimage.c                           |   32 +-
 util/grub-mkimagexx.c                         |   40 +-
 util/mkimage.c                                |   50 +-
 34 files changed, 6450 insertions(+), 43 deletions(-)
 create mode 100644 grub-core/commands/appendedsig/appendedsig.c
 create mode 100644 grub-core/commands/appendedsig/appendedsig.h
 create mode 100644 grub-core/commands/appendedsig/asn1util.c
 create mode 100644 grub-core/commands/appendedsig/gnutls_asn1_tab.c
 create mode 100644 grub-core/commands/appendedsig/pkcs7.c
 create mode 100644 grub-core/commands/appendedsig/pkix_asn1_tab.c
 create mode 100644 grub-core/commands/appendedsig/x509.c
 create mode 100644 grub-core/kern/powerpc/ieee1275/ieee1275.c
 create mode 100644 grub-core/kern/powerpc/ieee1275/platform_keystore.c
 create mode 100644 grub-core/tests/appended_signature_test.c
 create mode 100644 grub-core/tests/appended_signatures.h
 create mode 100644 include/grub/efi/pks.h
 create mode 100644 include/grub/powerpc/ieee1275/platform_keystore.h
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v6 1/20 powerpc-ieee1275: Add support for signing GRUB with an appended signature

Add infrastructure to allow firmware to verify the integrity of GRUB by use of a Linux-kernel-module-style appended signature. We initially target powerpc-ieee1275, but the code should be extensible to other platforms.

Usually these signatures are appended to a file without modifying the ELF file itself. (This is what the 'sign-file' tool does, for example.) The verifier loads the signed file from the file system and looks at the end of the file for the appended signature. However, on powerpc-ieee1275 platforms, the bootloader is often stored directly in the PReP partition as raw bytes without a file-system. This makes determining the location of an appended signature more difficult.

To address this, we add a new ELF Note.

The name field of shall be the string "Appended-Signature", zero-padded to 4 byte alignment. The type field shall be 0x41536967 (the ASCII values for the string "ASig"). It must be the final section in the ELF binary.

The description shall contain the appended signature structure as defined by the Linux kernel. The description will also be padded to be a multiple of 4 bytes. The padding shall be added before the appended signature structure (not at the end) so that the final bytes of a signed ELF file are the appended signature magic.

A subsequent patch documents how to create a GRUB core.img validly signed under this scheme.

---
 include/grub/util/install.h |  7 +++++--
 include/grub/util/mkimage.h |  4 ++--
 util/grub-install-common.c  | 23 ++++++++++++++++++---
 util/grub-mkimage.c         | 18 +++++++++++++++++
 util/grub-mkimagexx.c       | 40 ++++++++++++++++++++++++++++++++++++-
 util/mkimage.c              |  9 ++++++---
 6 files changed, 90 insertions(+), 11 deletions(-)
include/grub/util/install.h +5 -2
index 5c0a52ca2..3aabc4285 100644
@@ -69,6 +69,8 @@
       N_("disable shim_lock verifier"), 0 },				\
   { "disable-cli", GRUB_INSTALL_OPTIONS_DISABLE_CLI, 0, 0,		\
     N_("disabled command line interface access"), 0 },			\
+  { "appended-signature-size", GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE,  \
+    "SIZE", 0, N_("Add a note segment reserving SIZE bytes for an appended signature"), 1}, \
   { "verbose", 'v', 0, 0,						\
     N_("print verbose messages."), 1 }
 
@@ -132,7 +134,8 @@ enum grub_install_options {
   GRUB_INSTALL_OPTIONS_DTB,
   GRUB_INSTALL_OPTIONS_SBAT,
   GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK,
-  GRUB_INSTALL_OPTIONS_DISABLE_CLI
+  GRUB_INSTALL_OPTIONS_DISABLE_CLI,
+  GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE
 };
 
 extern char *grub_install_source_directory;
@@ -192,7 +195,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
 			     size_t npubkeys,
 			     char *config_path,
 			     const struct grub_install_image_target_desc *image_target,
-			     int note,
+			     int note, size_t appsig_size,
 			     grub_compression_t comp, const char *dtb_file,
 			     const char *sbat_path, const int disable_shim_lock,
 			     const int disable_cli);
include/grub/util/mkimage.h +2 -2
index 9d74f82c5..0d40383eb 100644
@@ -51,12 +51,12 @@ grub_mkimage_load_image64 (const char *kernel_path,
 			   const struct grub_install_image_target_desc *image_target);
 void
 grub_mkimage_generate_elf32 (const struct grub_install_image_target_desc *image_target,
-			     int note, char *sbat, char **core_img, size_t *core_size,
+			     int note, char *sbat, size_t appsig_size, char **core_img, size_t *core_size,
 			     Elf32_Addr target_addr,
 			     struct grub_mkimage_layout *layout);
 void
 grub_mkimage_generate_elf64 (const struct grub_install_image_target_desc *image_target,
-			     int note, char *sbat, char **core_img, size_t *core_size,
+			     int note, char *sbat, size_t appsig_size, char **core_img, size_t *core_size,
 			     Elf64_Addr target_addr,
 			     struct grub_mkimage_layout *layout);
 
util/grub-install-common.c +20 -3
index 22bccb6a3..5f24124df 100644
@@ -467,10 +467,13 @@ static char *sbat;
 static int disable_shim_lock;
 static grub_compression_t compression;
 static int disable_cli;
+static size_t appsig_size;
 
 int
 grub_install_parse (int key, char *arg)
 {
+  const char *end;
+
   switch (key)
     {
     case GRUB_INSTALL_OPTIONS_INSTALL_CORE_COMPRESS:
@@ -571,6 +574,19 @@ grub_install_parse (int key, char *arg)
       grub_util_error (_("Unrecognized compression `%s'"), arg);
     case GRUB_INSTALL_OPTIONS_GRUB_MKIMAGE:
       return 1;
+    case GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE:
+      appsig_size = grub_strtoul (arg, &end, 10);
+      if (*arg == '\0' || *end != '\0')
+        {
+          grub_util_error (_("non-numeric or invalid appended signature size `%s'"), arg);
+          return 0;
+        }
+      else if (appsig_size == 0)
+        {
+          grub_util_error (_("appended signature size `%s', and it should not be zero"), arg);
+          return 0;
+        }
+      return 1;
     default:
       return 0;
     }
@@ -683,9 +699,10 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,
   *p = '\0';
 
   grub_util_info ("grub-mkimage --directory '%s' --prefix '%s' --output '%s'"
-		  " --format '%s' --compression '%s'%s%s%s%s\n",
+		  " --format '%s' --compression '%s'"
+		  " --appended-signature-size %zu %s %s %s %s\n",
 		  dir, prefix, outname,
-		  mkimage_target, compnames[compression],
+		  mkimage_target, compnames[compression], appsig_size,
 		  note ? " --note" : "",
 		  disable_shim_lock ? " --disable-shim-lock" : "",
 		  disable_cli ? " --disable-cli" : "", s);
@@ -698,7 +715,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,
   grub_install_generate_image (dir, prefix, fp, outname,
 			       modules.entries, memdisk_path,
 			       pubkeys, npubkeys, config_path, tgt,
-			       note, compression, dtb, sbat,
+			       note, appsig_size, compression, dtb, sbat,
 			       disable_shim_lock, disable_cli);
   while (dc--)
     grub_install_pop_module ();
util/grub-mkimage.c +18 -0
index 547f7310f..922de021f 100644
@@ -84,6 +84,7 @@ static struct argp_option options[] = {
   {"sbat", 's', N_("FILE"), 0, N_("SBAT metadata"), 0},
   {"disable-shim-lock", GRUB_INSTALL_OPTIONS_DISABLE_SHIM_LOCK, 0, 0, N_("disable shim_lock verifier"), 0},
   {"disable-cli", GRUB_INSTALL_OPTIONS_DISABLE_CLI, 0, 0, N_("disable command line interface access"), 0},
+  {"appended-signature-size", 'S', N_("SIZE"), 0, N_("Add a note segment reserving SIZE bytes for an appended signature"), 0},
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
   { 0, 0, 0, 0, 0, 0 }
 };
@@ -130,6 +131,7 @@ struct arguments
   int note;
   int disable_shim_lock;
   int disable_cli;
+  size_t appsig_size;
   const struct grub_install_image_target_desc *image_target;
   grub_compression_t comp;
 };
@@ -140,6 +142,7 @@ argp_parser (int key, char *arg, struct argp_state *state)
   /* Get the input argument from argp_parse, which we
      know is a pointer to our arguments structure. */
   struct arguments *arguments = state->input;
+  const char *end;
 
   switch (key)
     {
@@ -172,6 +175,20 @@ argp_parser (int key, char *arg, struct argp_state *state)
       arguments->note = 1;
       break;
 
+    case 'S':
+      arguments->appsig_size = grub_strtoul (arg, &end, 10);
+      if (*arg == '\0' || *end != '\0')
+        {
+          grub_util_error (_("non-numeric or invalid appended signature size `%s'"), arg);
+          return 1;
+        }
+      else if (arguments->appsig_size == 0)
+        {
+          grub_util_error (_("appended signature size `%s', and it should not be zero"), arg);
+          return 1;
+        }
+      break;
+
     case 'm':
       if (arguments->memdisk)
 	free (arguments->memdisk);
@@ -330,6 +347,7 @@ main (int argc, char *argv[])
 			       arguments.memdisk, arguments.pubkeys,
 			       arguments.npubkeys, arguments.config,
 			       arguments.image_target, arguments.note,
+			       arguments.appsig_size,
 			       arguments.comp, arguments.dtb,
 			       arguments.sbat, arguments.disable_shim_lock,
 			       arguments.disable_cli);
util/grub-mkimagexx.c +39 -1
index 448862b2e..7dddd987a 100644
@@ -115,6 +115,14 @@ struct grub_sbat_note {
   char name[ALIGN_UP(sizeof(GRUB_SBAT_NOTE_NAME), 4)];
 };
 
+#define GRUB_APPENDED_SIGNATURE_NOTE_NAME "Appended-Signature"
+#define GRUB_APPENDED_SIGNATURE_NOTE_TYPE 0x41536967 /* "ASig" */
+struct grub_appended_signature_note
+{
+  Elf32_Nhdr header;
+  char name[ALIGN_UP (sizeof (GRUB_APPENDED_SIGNATURE_NOTE_NAME), 4)];
+};
+
 static int
 is_relocatable (const struct grub_install_image_target_desc *image_target)
 {
@@ -216,7 +224,7 @@ grub_arm_reloc_jump24 (grub_uint32_t *target, Elf32_Addr sym_addr)
 
 void
 SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc *image_target,
-				    int note, char *sbat, char **core_img, size_t *core_size,
+				    int note, char *sbat, size_t appsig_size, char **core_img, size_t *core_size,
 				    Elf_Addr target_addr,
 				    struct grub_mkimage_layout *layout)
 {
@@ -237,6 +245,12 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
       footer_size += ALIGN_UP (sizeof (struct grub_sbat_note) + layout->sbat_size, 4);
     }
 
+  if (appsig_size)
+    {
+      phnum++;
+      footer_size += ALIGN_UP (sizeof (struct grub_appended_signature_note), 4);
+    }
+
   if (image_target->id != IMAGE_LOONGSON_ELF)
     phnum += 2;
 
@@ -518,6 +532,30 @@ SUFFIX (grub_mkimage_generate_elf) (const struct grub_install_image_target_desc
       memcpy (note_ptr->name, GRUB_SBAT_NOTE_NAME, sizeof (GRUB_SBAT_NOTE_NAME));
       memcpy ((char *)(note_ptr + 1), sbat, layout->sbat_size);
 
+      phdr++;
+      phdr->p_type = grub_host_to_target32 (PT_NOTE);
+      phdr->p_flags = grub_host_to_target32 (PF_R);
+      phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
+      phdr->p_vaddr = 0;
+      phdr->p_paddr = 0;
+      phdr->p_filesz = grub_host_to_target32 (note_size);
+      phdr->p_memsz = 0;
+      phdr->p_offset = grub_host_to_target32 (header_size + program_size + footer_offset);
+      footer += note_size;
+      footer_offset += note_size;
+    }
+
+  if (appsig_size)
+    {
+      int note_size = ALIGN_UP (sizeof (struct grub_appended_signature_note) + appsig_size, 4);
+      struct grub_appended_signature_note *note_ptr = (struct grub_appended_signature_note *) footer;
+
+      note_ptr->header.n_namesz = grub_host_to_target32 (sizeof (GRUB_APPENDED_SIGNATURE_NOTE_NAME));
+      /* Needs to sit at the end, so we round this up and sign some zero padding. */
+      note_ptr->header.n_descsz = grub_host_to_target32 (ALIGN_UP (appsig_size, 4));
+      note_ptr->header.n_type = grub_host_to_target32 (GRUB_APPENDED_SIGNATURE_NOTE_TYPE);
+      strcpy (note_ptr->name, GRUB_APPENDED_SIGNATURE_NOTE_NAME);
+
       phdr++;
       phdr->p_type = grub_host_to_target32 (PT_NOTE);
       phdr->p_flags = grub_host_to_target32 (PF_R);
util/mkimage.c +6 -3
index b46df2909..9618b37cf 100644
@@ -885,7 +885,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
 			     char *memdisk_path, char **pubkey_paths,
 			     size_t npubkeys, char *config_path,
 			     const struct grub_install_image_target_desc *image_target,
-			     int note, grub_compression_t comp, const char *dtb_path,
+			     int note, size_t appsig_size, grub_compression_t comp, const char *dtb_path,
 			     const char *sbat_path, int disable_shim_lock,
 			     int disable_cli)
 {
@@ -946,6 +946,9 @@ grub_install_generate_image (const char *dir, const char *prefix,
   if (sbat_path != NULL && (image_target->id != IMAGE_EFI && image_target->id != IMAGE_PPC))
     grub_util_error (_("SBAT data can be added only to EFI or powerpc-ieee1275 images"));
 
+  if (appsig_size != 0 && image_target->id != IMAGE_PPC)
+    grub_util_error (_("appended signature can be support only to powerpc-ieee1275 images"));
+
   if (disable_shim_lock)
     total_module_size += sizeof (struct grub_module_header);
 
@@ -1833,10 +1836,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
 	else
 	  target_addr = image_target->link_addr;
 	if (image_target->voidp_sizeof == 4)
-	  grub_mkimage_generate_elf32 (image_target, note, sbat, &core_img, &core_size,
+	  grub_mkimage_generate_elf32 (image_target, note, sbat, appsig_size, &core_img, &core_size,
 				       target_addr, &layout);
 	else
-	  grub_mkimage_generate_elf64 (image_target, note, sbat, &core_img, &core_size,
+	  grub_mkimage_generate_elf64 (image_target, note, sbat, appsig_size, &core_img, &core_size,
 				       target_addr, &layout);
       }
       break;
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

PATCH v6 2/20 crypto: Move storage for grub_crypto_pk_* to crypto.c

In reply to Sudhakar Kuppusamy

From: Daniel Axtens <[email protected]>

The way gcry_rsa and friends (the asymmetric ciphers) are loaded for the pgp module is a bit quirky.

include/grub/crypto.h contains:
extern struct gcry_pk_spec *grub_crypto_pk_rsa;

commands/pgp.c contains the actual storage:
struct gcry_pk_spec *grub_crypto_pk_rsa;

And the module itself saves to the storage in pgp.c:
GRUB_MOD_INIT(gcry_rsa)

  {
    grub_crypto_pk_rsa = &_gcry_pubkey_spec_rsa;

}

This is annoying: gcry_rsa now has a dependency on pgp!

We want to be able to bring in gcry_rsa without bringing in PGP, so move the storage to crypto.c.

Previously, gcry_rsa depended on pgp and mpi. Now it depends on crypto and mpi. As pgp depends on crypto, this doesn't add any new module dependencies using the PGP verfier.

[FWIW, the story is different for the symmetric ciphers. cryptodisk and friends (zfs encryption etc) use grub_crypto_lookup_cipher_by_name() to get a cipher handle. That depends on grub_ciphers being populated by people calling grub_cipher_register. import_gcry.py ensures that the symmetric ciphers call it.]

---
 grub-core/commands/pgp.c | 4 ----
 grub-core/lib/crypto.c   | 4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)
grub-core/commands/pgp.c +0 -4
index e61887862..251ed1b06 100644
@@ -136,10 +136,6 @@ struct signature_v4_header
   grub_uint16_t hashed_sub;
 } GRUB_PACKED;
 
-struct gcry_pk_spec *grub_crypto_pk_dsa;
-struct gcry_pk_spec *grub_crypto_pk_ecdsa;
-struct gcry_pk_spec *grub_crypto_pk_rsa;
-
 struct
 {
   const char *name;
grub-core/lib/crypto.c +4 -0
index dd60dd4ac..292b747b2 100644
@@ -170,6 +170,10 @@ grub_md_unregister (gcry_md_spec_t *cipher)
       }
 }
 
+struct gcry_pk_spec *grub_crypto_pk_dsa;
+struct gcry_pk_spec *grub_crypto_pk_ecdsa;
+struct gcry_pk_spec *grub_crypto_pk_rsa;
+
 void
 grub_crypto_hash (const gcry_md_spec_t *hash, void *out, const void *in,
 		  grub_size_t inlen)
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

PATCH v6 3/20 pgp: Rename OBJ_TYPE_PUBKEY to OBJ_TYPE_GPG_PUBKEY

In reply to Sudhakar Kuppusamy

Prior to the addition of the X.509 public key support for appended signature, current PGP signature relied on the GPG public key. Changing the enum name from "OBJ_TYPE_PUBKEY" to "OBJ_TYPE_GPG_PUBKEY" to differentiate between x509 certificate based appended signature and GPG certificate based PGP signature.

---
 docs/grub.texi           | 34 +++++++++++++++++-----------------
 grub-core/commands/pgp.c |  2 +-
 include/grub/kernel.h    |  2 +-
 util/grub-mkimage.c      |  2 +-
 util/mkimage.c           |  2 +-
 5 files changed, 21 insertions(+), 21 deletions(-)
docs/grub.texi +17 -17
index 34b3484dc..bdbc3b82e 100644
@@ -3347,8 +3347,8 @@ chain-loaded system, @pxref{drivemap}.
 @node check_signatures
 @subsection check_signatures
 
-This variable controls whether GRUB enforces digital signature
-validation on loaded files. @xref{Using digital signatures}.
+This variable controls whether GRUB enforces GPG-style digital signature
+validation on loaded files. @xref{Using GPG-style digital signatures}.
 
 @node chosen
 @subsection chosen
@@ -6854,7 +6854,7 @@ These keys are used to validate signatures when environment variable
 @code{check_signatures} is set to @code{enforce}
 (@pxref{check_signatures}), and by some invocations of
 @command{verify_detached} (@pxref{verify_detached}).  @xref{Using
-digital signatures}, for more information.
+GPG-style digital signatures}, for more information.
 @end deffn
 
 @node drivemap
@@ -7270,7 +7270,7 @@ The output is in GPG's v4 key fingerprint format (i.e., the output of
 @code{gpg --fingerprint}).  The least significant four bytes (last
 eight hexadecimal digits) can be used as an argument to
 @command{distrust} (@pxref{distrust}).
-@xref{Using digital signatures}, for more information about uses for
+@xref{Using GPG-style digital signatures}, for more information about uses for
 these keys.
 @end deffn
 
@@ -7305,7 +7305,7 @@ When used with care, @option{--skip-sig} and the whitelist enable an
 administrator to configure a system to boot only signed
 configurations, but to allow the user to select from among multiple
 configurations, and to enable ``one-shot'' boot attempts and
-``savedefault'' behavior.  @xref{Using digital signatures}, for more
+``savedefault'' behavior.  @xref{Using GPG-style digital signatures}, for more
 information.
 @end deffn
 
@@ -7677,7 +7677,7 @@ read.  It is possible to modify a digitally signed environment block
 file from within GRUB using this command, such that its signature will
 no longer be valid on subsequent boots.  Care should be taken in such
 advanced configurations to avoid rendering the system
-unbootable. @xref{Using digital signatures}, for more information.
+unbootable. @xref{Using GPG-style digital signatures}, for more information.
 @end deffn
 
 
@@ -8167,7 +8167,7 @@ signatures when environment variable @code{check_signatures} is set to
 must itself be properly signed.  The @option{--skip-sig} option can be
 used to disable signature-checking when reading @var{pubkey_file}
 itself. It is expected that @option{--skip-sig} is useful for testing
-and manual booting. @xref{Using digital signatures}, for more
+and manual booting. @xref{Using GPG-style digital signatures}, for more
 information.
 @end deffn
 
@@ -8208,7 +8208,7 @@ tried.
 
 Exit code @code{$?} is set to 0 if the signature validates
 successfully.  If validation fails, it is set to a non-zero value.
-@xref{Using digital signatures}, for more information.
+@xref{Using GPG-style digital signatures}, for more information.
 @end deffn
 
 @node videoinfo
@@ -8668,13 +8668,13 @@ environment variables and commands are listed in the same order.
 @chapter Security
 
 @menu
-* Authentication and authorisation:: Users and access control
-* Using digital signatures::         Booting digitally signed code
-* UEFI secure boot and shim::        Booting digitally signed PE files
-* Secure Boot Advanced Targeting::   Embedded information for generation number based revocation
-* Measured Boot::                    Measuring boot components
-* Lockdown::                         Lockdown when booting on a secure setup
-* TPM2 key protector::               Managing disk key with TPM2 key protector
+* Authentication and authorisation::   Users and access control
+* Using GPG-style digital signatures:: Booting digitally signed code
+* UEFI secure boot and shim::          Booting digitally signed PE files
+* Secure Boot Advanced Targeting::     Embedded information for generation number based revocation
+* Measured Boot::                      Measuring boot components
+* Lockdown::                           Lockdown when booting on a secure setup
+* TPM2 key protector::                 Managing disk key with TPM2 key protector
 @end menu
 
 @node Authentication and authorisation
@@ -8750,8 +8750,8 @@ generating configuration files with authentication.  You can use
 adding @kbd{set superusers=} and @kbd{password} or @kbd{password_pbkdf2}
 commands.
 
-@node Using digital signatures
-@section Using digital signatures in GRUB
+@node Using GPG-style digital signatures
+@section Using GPG-style digital signatures in GRUB
 
 GRUB's @file{core.img} can optionally provide enforcement that all files
 subsequently read from disk are covered by a valid digital signature.
grub-core/commands/pgp.c +1 -1
index 251ed1b06..a2549f9fd 100644
@@ -920,7 +920,7 @@ GRUB_MOD_INIT(pgp)
     grub_memset (&pseudo_file, 0, sizeof (pseudo_file));
 
     /* Not an ELF module, skip.  */
-    if (header->type != OBJ_TYPE_PUBKEY)
+    if (header->type != OBJ_TYPE_GPG_PUBKEY)
       continue;
 
     pseudo_file.fs = &pseudo_fs;
include/grub/kernel.h +1 -1
index 6121c1e66..885289f5d 100644
@@ -28,7 +28,7 @@ enum
   OBJ_TYPE_MEMDISK,
   OBJ_TYPE_CONFIG,
   OBJ_TYPE_PREFIX,
-  OBJ_TYPE_PUBKEY,
+  OBJ_TYPE_GPG_PUBKEY,
   OBJ_TYPE_DTB,
   OBJ_TYPE_DISABLE_SHIM_LOCK,
   OBJ_TYPE_DISABLE_CLI
util/grub-mkimage.c +1 -1
index 922de021f..000d05822 100644
@@ -75,7 +75,7 @@ static struct argp_option options[] = {
    /* TRANSLATORS: "embed" is a verb (command description).  "*/
   {"config",   'c', N_("FILE"), 0, N_("embed FILE as an early config"), 0},
    /* TRANSLATORS: "embed" is a verb (command description).  "*/
-  {"pubkey",   'k', N_("FILE"), 0, N_("embed FILE as public key for signature checking"), 0},
+  {"pubkey",   'k', N_("FILE"), 0, N_("embed FILE as public key for PGP signature checking"), 0},
   /* TRANSLATORS: NOTE is a name of segment.  */
   {"note",   'n', 0, 0, N_("add NOTE segment for CHRP IEEE1275"), 0},
   {"output",  'o', N_("FILE"), 0, N_("output a generated image to FILE [default=stdout]"), 0},
util/mkimage.c +1 -1
index 9618b37cf..61c4e78e2 100644
@@ -1056,7 +1056,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
 	curs = grub_util_get_image_size (pubkey_paths[i]);
 
 	header = (struct grub_module_header *) (kernel_img + offset);
-	header->type = grub_host_to_target32 (OBJ_TYPE_PUBKEY);
+	header->type = grub_host_to_target32 (OBJ_TYPE_GPG_PUBKEY);
 	header->size = grub_host_to_target32 (curs + sizeof (*header));
 	offset += sizeof (*header);
 
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#4 lore raw

PATCH v6 4/20 grub-install: Support embedding x509 certificates

In reply to Sudhakar Kuppusamy

To support verification of appended signatures, we need a way to embed the necessary public keys. Existing appended signature schemes in the Linux kernel use X.509 certificates, so allow certificates to be embedded in the GRUB core image in the same way as PGP keys.

---
 include/grub/kernel.h       |  1 +
 include/grub/util/install.h |  3 +++
 util/grub-install-common.c  | 19 +++++++++++++++++-
 util/grub-mkimage.c         | 12 +++++++++++-
 util/mkimage.c              | 39 ++++++++++++++++++++++++++++++++++++-
 5 files changed, 71 insertions(+), 3 deletions(-)
include/grub/kernel.h +1 -0
index 885289f5d..9f3e2031f 100644
@@ -29,6 +29,7 @@ enum
   OBJ_TYPE_CONFIG,
   OBJ_TYPE_PREFIX,
   OBJ_TYPE_GPG_PUBKEY,
+  OBJ_TYPE_X509_PUBKEY,
   OBJ_TYPE_DTB,
   OBJ_TYPE_DISABLE_SHIM_LOCK,
   OBJ_TYPE_DISABLE_CLI
include/grub/util/install.h +3 -0
index 3aabc4285..6f27e2e42 100644
@@ -69,6 +69,8 @@
       N_("disable shim_lock verifier"), 0 },				\
   { "disable-cli", GRUB_INSTALL_OPTIONS_DISABLE_CLI, 0, 0,		\
     N_("disabled command line interface access"), 0 },			\
+  { "x509key",   'x', N_("FILE"), 0,					\
+      N_("embed FILE as an x509 certificate for appended signature checking"), 0}, \
   { "appended-signature-size", GRUB_INSTALL_OPTIONS_APPENDED_SIGNATURE_SIZE,  \
     "SIZE", 0, N_("Add a note segment reserving SIZE bytes for an appended signature"), 1}, \
   { "verbose", 'v', 0, 0,						\
@@ -193,6 +195,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
 			     const char *outname, char *mods[],
 			     char *memdisk_path, char **pubkey_paths,
 			     size_t npubkeys,
+			     char **x509key_paths, size_t nx509keys,
 			     char *config_path,
 			     const struct grub_install_image_target_desc *image_target,
 			     int note, size_t appsig_size,
util/grub-install-common.c +18 -1
index 5f24124df..820105771 100644
@@ -463,6 +463,8 @@ handle_install_list (struct install_list *il, const char *val,
 
 static char **pubkeys;
 static size_t npubkeys;
+static char **x509keys;
+static size_t nx509keys;
 static char *sbat;
 static int disable_shim_lock;
 static grub_compression_t compression;
@@ -511,6 +513,10 @@ grub_install_parse (int key, char *arg)
     case GRUB_INSTALL_OPTIONS_DISABLE_CLI:
       disable_cli = 1;
       return 1;
+    case 'x':
+      x509keys = xrealloc (x509keys, sizeof (x509keys[0]) * (nx509keys + 1));
+      x509keys[nx509keys++] = xstrdup (arg);
+      return 1;
 
     case GRUB_INSTALL_OPTIONS_VERBOSITY:
       verbosity++;
@@ -648,6 +654,9 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,
   for (pk = pubkeys; pk < pubkeys + npubkeys; pk++)
     slen += sizeof (" --pubkey ''") + grub_strlen (*pk);
 
+  for (pk = x509keys; pk < x509keys + nx509keys; pk++)
+    slen += sizeof (" --x509key ''") + grub_strlen (*pk);
+
   for (md = modules.entries; *md; md++)
     slen += sizeof (" ''") + grub_strlen (*md);
 
@@ -688,6 +697,14 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,
       *p++ = '\'';
     }
 
+  for (pk = x509keys; pk < x509keys + nx509keys; pk++)
+    {
+      p = grub_stpcpy (p, "--x509key '");
+      p = grub_stpcpy (p, *pk);
+      *p++ = '\'';
+      *p++ = ' ';
+    }
+
   for (md = modules.entries; *md; md++)
     {
       *p++ = ' ';
@@ -714,7 +731,7 @@ grub_install_make_image_wrap_file (const char *dir, const char *prefix,
 
   grub_install_generate_image (dir, prefix, fp, outname,
 			       modules.entries, memdisk_path,
-			       pubkeys, npubkeys, config_path, tgt,
+			       pubkeys, npubkeys,  x509keys, nx509keys, config_path, tgt,
 			       note, appsig_size, compression, dtb, sbat,
 			       disable_shim_lock, disable_cli);
   while (dc--)
util/grub-mkimage.c +11 -1
index 000d05822..6fcc20699 100644
@@ -76,6 +76,7 @@ static struct argp_option options[] = {
   {"config",   'c', N_("FILE"), 0, N_("embed FILE as an early config"), 0},
    /* TRANSLATORS: "embed" is a verb (command description).  "*/
   {"pubkey",   'k', N_("FILE"), 0, N_("embed FILE as public key for PGP signature checking"), 0},
+  {"x509key",     'x', N_("FILE"), 0, N_("embed FILE as an x509 certificate for appended signature checking"), 0},
   /* TRANSLATORS: NOTE is a name of segment.  */
   {"note",   'n', 0, 0, N_("add NOTE segment for CHRP IEEE1275"), 0},
   {"output",  'o', N_("FILE"), 0, N_("output a generated image to FILE [default=stdout]"), 0},
@@ -125,6 +126,8 @@ struct arguments
   char *dtb;
   char **pubkeys;
   size_t npubkeys;
+  char **x509keys;
+  size_t nx509keys;
   char *font;
   char *config;
   char *sbat;
@@ -215,6 +218,12 @@ argp_parser (int key, char *arg, struct argp_state *state)
       arguments->pubkeys[arguments->npubkeys++] = xstrdup (arg);
       break;
 
+    case 'x':
+      arguments->x509keys = xrealloc (arguments->x509keys,
+                                      sizeof (arguments->x509keys[0]) * (arguments->nx509keys + 1));
+      arguments->x509keys[arguments->nx509keys++] = xstrdup (arg);
+      break;
+
     case 'c':
       if (arguments->config)
 	free (arguments->config);
@@ -345,7 +354,8 @@ main (int argc, char *argv[])
   grub_install_generate_image (arguments.dir, arguments.prefix, fp,
 			       arguments.output, arguments.modules,
 			       arguments.memdisk, arguments.pubkeys,
-			       arguments.npubkeys, arguments.config,
+			       arguments.npubkeys, arguments.x509keys,
+			       arguments.nx509keys, arguments.config,
 			       arguments.image_target, arguments.note,
 			       arguments.appsig_size,
 			       arguments.comp, arguments.dtb,
util/mkimage.c +38 -1
index 61c4e78e2..f364a5718 100644
@@ -883,7 +883,7 @@ void
 grub_install_generate_image (const char *dir, const char *prefix,
 			     FILE *out, const char *outname, char *mods[],
 			     char *memdisk_path, char **pubkey_paths,
-			     size_t npubkeys, char *config_path,
+			     size_t npubkeys, char **x509key_paths, size_t nx509keys, char *config_path,
 			     const struct grub_install_image_target_desc *image_target,
 			     int note, size_t appsig_size, grub_compression_t comp, const char *dtb_path,
 			     const char *sbat_path, int disable_shim_lock,
@@ -929,6 +929,24 @@ grub_install_generate_image (const char *dir, const char *prefix,
       }
   }
 
+  if (nx509keys != 0 && image_target->id != IMAGE_PPC)
+    grub_util_error (_("x509 public key can be support only to appended signature"
+                       " with powerpc-ieee1275 images"));
+
+  {
+    size_t i;
+
+    for (i = 0; i < nx509keys; i++)
+      {
+        size_t curs;
+
+        curs = ALIGN_ADDR (grub_util_get_image_size (x509key_paths[i]));
+        grub_util_info ("the size of x509 public key %u is 0x%" GRUB_HOST_PRIxLONG_LONG,
+                        (unsigned) i, (unsigned long long) curs);
+        total_module_size += curs + sizeof (struct grub_module_header);
+      }
+  }
+
   if (memdisk_path)
     {
       memdisk_size = ALIGN_UP(grub_util_get_image_size (memdisk_path), 512);
@@ -1065,6 +1083,25 @@ grub_install_generate_image (const char *dir, const char *prefix,
       }
   }
 
+  {
+    size_t i;
+
+    for (i = 0; i < nx509keys; i++)
+      {
+        size_t curs;
+        struct grub_module_header *header;
+
+        curs = grub_util_get_image_size (x509key_paths[i]);
+        header = (struct grub_module_header *) (kernel_img + offset);
+        header->type = grub_host_to_target32 (OBJ_TYPE_X509_PUBKEY);
+        header->size = grub_host_to_target32 (curs + sizeof (*header));
+
+        offset += sizeof (*header);
+        grub_util_load_image (x509key_paths[i], kernel_img + offset);
+        offset += ALIGN_ADDR (curs);
+      }
+  }
+
   if (memdisk_path)
     {
       struct grub_module_header *header;
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw

PATCH v6 5/20 appended signatures: Import GNUTLS's ASN.1 description files

In reply to Sudhakar Kuppusamy

In order to parse PKCS#7 messages and X.509 certificates with libtasn1, we need some information about how they are encoded.

We get these from GNUTLS, which has the benefit that they support the features we need and are well tested.

The GNUTLS files are from:

- https://github.com/gnutls/gnutls/blob/master/lib/gnutls.asn
- https://github.com/gnutls/gnutls/blob/master/lib/pkix.asn

The GNUTLS license is LGPLv2.1+, which is GPLv3 compatible, allowing us to import it without issue.

---
 .../commands/appendedsig/gnutls_asn1_tab.c    | 148 ++++++
 .../commands/appendedsig/pkix_asn1_tab.c      | 485 ++++++++++++++++++
 2 files changed, 633 insertions(+)
 create mode 100644 grub-core/commands/appendedsig/gnutls_asn1_tab.c
 create mode 100644 grub-core/commands/appendedsig/pkix_asn1_tab.c
grub-core/commands/appendedsig/gnutls_asn1_tab.c new file +148 -0
new file mode 100644
index 000000000..efc0c145a
@@ -0,0 +1,148 @@
+#include <grub/mm.h>
+#include <libtasn1.h>
+
+/*
+ * Imported from gnutls.asn.
+ * https://github.com/gnutls/gnutls/blob/master/lib/gnutls.asn
+ */
+const asn1_static_node grub_gnutls_asn1_tab[] = {
+  { "GNUTLS", 536872976, NULL },
+  { NULL, 1073741836, NULL },
+  { "RSAPublicKey", 1610612741, NULL },
+  { "modulus", 1073741827, NULL },
+  { "publicExponent", 3, NULL },
+  { "RSAPrivateKey", 1610612741, NULL },
+  { "version", 1073741827, NULL },
+  { "modulus", 1073741827, NULL },
+  { "publicExponent", 1073741827, NULL },
+  { "privateExponent", 1073741827, NULL },
+  { "prime1", 1073741827, NULL },
+  { "prime2", 1073741827, NULL },
+  { "exponent1", 1073741827, NULL },
+  { "exponent2", 1073741827, NULL },
+  { "coefficient", 1073741827, NULL },
+  { "otherPrimeInfos", 16386, "OtherPrimeInfos"},
+  { "ProvableSeed", 1610612741, NULL },
+  { "algorithm", 1073741836, NULL },
+  { "seed", 7, NULL },
+  { "OtherPrimeInfos", 1612709899, NULL },
+  { "MAX", 1074266122, "1"},
+  { NULL, 2, "OtherPrimeInfo"},
+  { "OtherPrimeInfo", 1610612741, NULL },
+  { "prime", 1073741827, NULL },
+  { "exponent", 1073741827, NULL },
+  { "coefficient", 3, NULL },
+  { "AlgorithmIdentifier", 1610612741, NULL },
+  { "algorithm", 1073741836, NULL },
+  { "parameters", 541081613, NULL },
+  { "algorithm", 1, NULL },
+  { "DigestInfo", 1610612741, NULL },
+  { "digestAlgorithm", 1073741826, "DigestAlgorithmIdentifier"},
+  { "digest", 7, NULL },
+  { "DigestAlgorithmIdentifier", 1073741826, "AlgorithmIdentifier"},
+  { "DSAPublicKey", 1073741827, NULL },
+  { "DSAParameters", 1610612741, NULL },
+  { "p", 1073741827, NULL },
+  { "q", 1073741827, NULL },
+  { "g", 3, NULL },
+  { "DSASignatureValue", 1610612741, NULL },
+  { "r", 1073741827, NULL },
+  { "s", 3, NULL },
+  { "DSAPrivateKey", 1610612741, NULL },
+  { "version", 1073741827, NULL },
+  { "p", 1073741827, NULL },
+  { "q", 1073741827, NULL },
+  { "g", 1073741827, NULL },
+  { "Y", 1073741827, NULL },
+  { "priv", 3, NULL },
+  { "DHParameter", 1610612741, NULL },
+  { "prime", 1073741827, NULL },
+  { "base", 1073741827, NULL },
+  { "privateValueLength", 16387, NULL },
+  { "pkcs-11-ec-Parameters", 1610612754, NULL },
+  { "oId", 1073741836, NULL },
+  { "curveName", 31, NULL },
+  { "ECParameters", 1610612754, NULL },
+  { "namedCurve", 12, NULL },
+  { "ECPrivateKey", 1610612741, NULL },
+  { "Version", 1073741827, NULL },
+  { "privateKey", 1073741831, NULL },
+  { "parameters", 1610637314, "ECParameters"},
+  { NULL, 2056, "0"},
+  { "publicKey", 536895494, NULL },
+  { NULL, 2056, "1"},
+  { "PrincipalName", 1610612741, NULL },
+  { "name-type", 1610620931, NULL },
+  { NULL, 2056, "0"},
+  { "name-string", 536879115, NULL },
+  { NULL, 1073743880, "1"},
+  { NULL, 27, NULL },
+  { "KRB5PrincipalName", 1610612741, NULL },
+  { "realm", 1610620955, NULL },
+  { NULL, 2056, "0"},
+  { "principalName", 536879106, "PrincipalName"},
+  { NULL, 2056, "1"},
+  { "RSAPSSParameters", 1610612741, NULL },
+  { "hashAlgorithm", 1610637314, "AlgorithmIdentifier"},
+  { NULL, 2056, "0"},
+  { "maskGenAlgorithm", 1610637314, "AlgorithmIdentifier"},
+  { NULL, 2056, "1"},
+  { "saltLength", 1610653699, NULL },
+  { NULL, 1073741833, "20"},
+  { NULL, 2056, "2"},
+  { "trailerField", 536911875, NULL },
+  { NULL, 1073741833, "1"},
+  { NULL, 2056, "3"},
+  { "RSAOAEPParameters", 1610612741, NULL },
+  { "hashAlgorithm", 1610637314, "AlgorithmIdentifier"},
+  { NULL, 2056, "0"},
+  { "maskGenAlgorithm", 1610637314, "AlgorithmIdentifier"},
+  { NULL, 2056, "1"},
+  { "pSourceFunc", 536895490, "AlgorithmIdentifier"},
+  { NULL, 2056, "2"},
+  { "GOSTParameters", 1610612741, NULL },
+  { "publicKeyParamSet", 1073741836, NULL },
+  { "digestParamSet", 16396, NULL },
+  { "GOSTParametersOld", 1610612741, NULL },
+  { "publicKeyParamSet", 1073741836, NULL },
+  { "digestParamSet", 1073741836, NULL },
+  { "encryptionParamSet", 16396, NULL },
+  { "GOSTPrivateKey", 1073741831, NULL },
+  { "GOSTPrivateKeyOld", 1073741827, NULL },
+  { "IssuerSignTool", 1610612741, NULL },
+  { "signTool", 1073741858, NULL },
+  { "cATool", 1073741858, NULL },
+  { "signToolCert", 1073741858, NULL },
+  { "cAToolCert", 34, NULL },
+  { "Gost28147-89-EncryptedKey", 1610612741, NULL },
+  { "encryptedKey", 1073741831, NULL },
+  { "maskKey", 1610637319, NULL },
+  { NULL, 4104, "0"},
+  { "macKey", 7, NULL },
+  { "SubjectPublicKeyInfo", 1610612741, NULL },
+  { "algorithm", 1073741826, "AlgorithmIdentifier"},
+  { "subjectPublicKey", 6, NULL },
+  { "GostR3410-TransportParameters", 1610612741, NULL },
+  { "encryptionParamSet", 1073741836, NULL },
+  { "ephemeralPublicKey", 1610637314, "SubjectPublicKeyInfo"},
+  { NULL, 4104, "0"},
+  { "ukm", 7, NULL },
+  { "GostR3410-KeyTransport", 1610612741, NULL },
+  { "sessionEncryptedKey", 1073741826, "Gost28147-89-EncryptedKey"},
+  { "transportParameters", 536895490, "GostR3410-TransportParameters"},
+  { NULL, 4104, "0"},
+  { "TPMKey", 1610612741, NULL },
+  { "type", 1073741836, NULL },
+  { "emptyAuth", 1610637316, NULL },
+  { NULL, 2056, "0"},
+  { "parent", 1073741827, NULL },
+  { "pubkey", 1073741831, NULL },
+  { "privkey", 7, NULL },
+  { "MLDSAPrivateKey", 536870917, NULL },
+  { "version", 1073741827, NULL },
+  { "privateKeyAlgorithm", 1073741826, "AlgorithmIdentifier"},
+  { "privateKey", 1073741831, NULL },
+  { "publicKey", 536895495, NULL },
+  { NULL, 2056, "1"},
+  { NULL, 0, NULL }
+};
grub-core/commands/appendedsig/pkix_asn1_tab.c new file +485 -0

485 lines not on this page · raw message

Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#6 lore raw

PATCH v6 6/20 appended signatures: Parse ASN1 node

In reply to Sudhakar Kuppusamy

This code allows us to parse ASN1 node and allocating memory to store it. It will work for anything where the size libtasn1 returns is right:
- Integers
- Octet strings
- DER encoding of other structures
It will _not_ work for things where libtasn1 size requires adjustment:
- Strings that require an extra NULL byte at the end
- Bit strings because libtasn1 returns the length in bits, not bytes.
If the function returns a non-NULL value, the caller must free it.

---
 grub-core/commands/appendedsig/appendedsig.h | 42 +++++++++
 grub-core/commands/appendedsig/asn1util.c    | 96 ++++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100644 grub-core/commands/appendedsig/appendedsig.h
 create mode 100644 grub-core/commands/appendedsig/asn1util.c
grub-core/commands/appendedsig/appendedsig.h new file +42 -0
new file mode 100644
index 000000000..91cc040bb
@@ -0,0 +1,42 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2020, 2022 Free Software Foundation, Inc.
+ *  Copyright (C) 2020, 2022, 2025 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 <libtasn1.h>
+
+extern asn1_node grub_gnutls_gnutls_asn;
+extern asn1_node grub_gnutls_pkix_asn;
+
+/* Do libtasn1 init. */
+extern int
+asn1_init (void);
+
+/*
+ * Read a value from an ASN1 node, allocating memory to store it.
+ * It will work for anything where the size libtasn1 returns is right:
+ *  - Integers
+ *  - Octet strings
+ *  - DER encoding of other structures
+ * It will _not_ work for things where libtasn1 size requires adjustment:
+ *  - Strings that require an extra null byte at the end
+ *  - Bit strings because libtasn1 returns the length in bits, not bytes.
+ *
+ * If the function returns a non-NULL value, the caller must free it.
+ */
+extern void *
+grub_asn1_allocate_and_read (asn1_node node, const char *name, const char *friendly_name, int *content_size);
grub-core/commands/appendedsig/asn1util.c new file +96 -0

96 lines not on this page · raw message

Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw

PATCH v6 7/20 appended signatures: Parse PKCS#7 signedData

In reply to Sudhakar Kuppusamy

This code allows us to parse:

- PKCS#7 signedData messages. Only a single signerInfo is supported, which is all that the Linux sign-file utility supports creating out-of-the-box. Only RSA, SHA-256 and SHA-512 are supported. Any certificate embedded in the PKCS#7 message will be ignored.

---
 grub-core/commands/appendedsig/appendedsig.h |  36 ++
 grub-core/commands/appendedsig/pkcs7.c       | 454 +++++++++++++++++++
 2 files changed, 490 insertions(+)
 create mode 100644 grub-core/commands/appendedsig/pkcs7.c
grub-core/commands/appendedsig/appendedsig.h +36 -0
index 91cc040bb..cac7fb02c 100644
@@ -17,11 +17,47 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <grub/crypto.h>
 #include <libtasn1.h>
 
 extern asn1_node grub_gnutls_gnutls_asn;
 extern asn1_node grub_gnutls_pkix_asn;
 
+#define MAX_OID_LEN 32
+
+/* A PKCS#7 signedData signerInfo. */
+struct pkcs7_signerInfo
+{
+  const gcry_md_spec_t *hash;
+  gcry_mpi_t sig_mpi;
+};
+
+/*
+ * A PKCS#7 signedData message.
+ * We make no attempt to match intelligently, so we don't save any info about
+ * the signer.
+ */
+struct pkcs7_signedData
+{
+  int signerInfo_count;
+  struct pkcs7_signerInfo *signerInfos;
+};
+
+/*
+ * Parse a PKCS#7 message, which must be a signedData message.
+ * The message must be in 'sigbuf' and of size 'data_size'. The result is
+ * placed in 'msg', which must already be allocated.
+ */
+extern grub_err_t
+parse_pkcs7_signedData (const void *sigbuf, grub_size_t data_size, struct pkcs7_signedData *msg);
+
+/*
+ * Release all the storage associated with the PKCS#7 message.
+ * If the caller dynamically allocated the message, it must free it.
+ */
+extern void
+pkcs7_signedData_release (struct pkcs7_signedData *msg);
+
 /* Do libtasn1 init. */
 extern int
 asn1_init (void);
grub-core/commands/appendedsig/pkcs7.c new file +454 -0

454 lines not on this page · raw message

Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#8 lore raw

PATCH v6 8/20 appended signatures: Parse X.509 certificates

In reply to Sudhakar Kuppusamy

This code allows us to parse:

- X.509 certificates: at least enough to verify the signatures on the PKCS#7 messages. We expect that the certificates embedded in GRUB will be leaf certificates, not CA certificates. The parser enforces this.

- X.509 certificates support the Extended Key Usage extension and handle it by verifying that the certificate has a Code Signing usage.

---
 grub-core/commands/appendedsig/appendedsig.h |  33 +
 grub-core/commands/appendedsig/x509.c        | 957 +++++++++++++++++++
 2 files changed, 990 insertions(+)
 create mode 100644 grub-core/commands/appendedsig/x509.c
grub-core/commands/appendedsig/appendedsig.h +33 -0
index cac7fb02c..5ea31f1fd 100644
@@ -25,6 +25,24 @@ extern asn1_node grub_gnutls_pkix_asn;
 
 #define MAX_OID_LEN 32
 
+/*
+ * One or more x509 certificates.
+ * We do limited parsing:
+ * extracting only the serial, issuer, subject and RSA public key.
+ */
+struct x509_certificate
+{
+  struct x509_certificate *next;
+  grub_uint8_t *serial;
+  grub_size_t serial_len;
+  char *issuer;
+  grub_size_t issuer_len;
+  char *subject;
+  grub_size_t subject_len;
+  /* We only support RSA public keys. This encodes [modulus, publicExponent]. */
+  gcry_mpi_t mpis[2];
+};
+
 /* A PKCS#7 signedData signerInfo. */
 struct pkcs7_signerInfo
 {
@@ -43,6 +61,21 @@ struct pkcs7_signedData
   struct pkcs7_signerInfo *signerInfos;
 };
 
+/*
+ * Import a DER-encoded certificate at 'data', of size 'size'.
+ * Place the results into 'results', which must be already allocated.
+ */
+extern grub_err_t
+parse_x509_certificate (const void *data, grub_size_t size, struct x509_certificate *results);
+
+/*
+ * Release all the storage associated with the x509 certificate.
+ * If the caller dynamically allocated the certificate, it must free it.
+ * The caller is also responsible for maintenance of the linked list.
+ */
+extern void
+certificate_release (struct x509_certificate *cert);
+
 /*
  * Parse a PKCS#7 message, which must be a signedData message.
  * The message must be in 'sigbuf' and of size 'data_size'. The result is
grub-core/commands/appendedsig/x509.c new file +957 -0

957 lines not on this page · raw message

Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#9 lore raw

PATCH v6 9/20 powerpc_ieee1275: Enter lockdown based on /ibm, secure-boot

In reply to Sudhakar Kuppusamy

Read secure boot mode from 'ibm,secure-boot' property and if the secure boot mode is enforced, enter lockdown. Else it is considered as disabled. There are three secure boot modes. They are

0 - disabled
No signature verification is performed. This is the default.
1 - audit
Signature verification is performed and if signature verification fails, post the errors and allow the boot to continue.
2 - enforced
Lockdown the GRUB. Signature verification is performed and If signature verification fails, post the errors and stop the boot.

Now, only support disabled and enforced.

---
 docs/grub.texi                   |  2 +-
 grub-core/Makefile.core.def      |  1 +
 grub-core/kern/ieee1275/init.c   | 55 ++++++++++++++++++++++++++++++++
 include/grub/ieee1275/ieee1275.h | 19 +++++++++++
 include/grub/lockdown.h          |  3 +-
 5 files changed, 78 insertions(+), 2 deletions(-)
docs/grub.texi +1 -1
index bdbc3b82e..15f087f27 100644
@@ -8912,7 +8912,7 @@ platforms.
 @section Lockdown when booting on a secure setup
 
 The GRUB can be locked down when booted on a secure boot environment, for example
-if the UEFI secure boot is enabled. On a locked down configuration, the GRUB will
+if the UEFI or Power secure boot is enabled. On a locked down configuration, the GRUB will
 be restricted and some operations/commands cannot be executed. This also includes
 limiting which filesystems are supported to those thought to be more robust and
 widely used within GRUB.
grub-core/Makefile.core.def +1 -0
index b3f71196a..b72f322b1 100644
@@ -331,6 +331,7 @@ kernel = {
   powerpc_ieee1275 = kern/powerpc/cache.S;
   powerpc_ieee1275 = kern/powerpc/dl.c;
   powerpc_ieee1275 = kern/powerpc/compiler-rt.S;
+  powerpc_ieee1275 = kern/lockdown.c;
 
   sparc64_ieee1275 = kern/sparc64/cache.S;
   sparc64_ieee1275 = kern/sparc64/dl.c;
grub-core/kern/ieee1275/init.c +55 -0

73 lines not on this page · raw message

include/grub/ieee1275/ieee1275.h +19 -0
index c445d0499..6f7925168 100644
@@ -261,4 +261,23 @@ char *EXPORT_FUNC(grub_ieee1275_get_boot_dev) (void);
 						      (alias).name;	\
 						      grub_ieee1275_children_peer (&(alias)))
 
+
+#ifdef __powerpc__
+
+/*
+ * Secure Boot Mode:
+ * 0 - disabled
+ *      No signature verification is performed. This is the default.
+ * 2 - enforced
+ *      Lockdown the GRUB. Signature verification is performed and
+ *      If signature verification fails, post the errors and stop the boot.
+ */
+#define GRUB_SB_DISABLED        0
+#define GRUB_SB_ENFORCED        2
+
+extern grub_uint32_t
+EXPORT_FUNC(grub_ieee1275_is_secure_boot) (void);
+
+#endif
+
 #endif /* ! GRUB_IEEE1275_HEADER */
include/grub/lockdown.h +2 -1
index 40531fa82..ebfee4bf0 100644
@@ -24,7 +24,8 @@
 #define GRUB_LOCKDOWN_DISABLED       0
 #define GRUB_LOCKDOWN_ENABLED        1
 
-#ifdef GRUB_MACHINE_EFI
+#if defined(GRUB_MACHINE_EFI) || \
+    (defined(__powerpc__) && defined(GRUB_MACHINE_IEEE1275))
 extern void
 EXPORT_FUNC (grub_lockdown) (void);
 extern int
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#10 lore raw

PATCH v6 10/20 appended signatures: Support verifying appended signatures

In reply to Sudhakar Kuppusamy

Building on the parsers and the ability to embed X.509 certificates, as well as the existing gcrypt functionality, add a module for verifying appended signatures.

This includes a verifier that requires that Linux kernels and GRUB modules have appended signatures, and commands to manage the list of trusted certificates for verification.

Verification must be enabled by setting check_appended_signatures. If secure boot is enabled with enforced mode when the module is loaded, verification will be enabled and locked automatically. If verification is enabled, extract trusted keys from the GRUB ELF Note and store them in the db.

As with the PGP verifier, it is not a complete secure-boot solution:
other mechanisms, such as a password or lockdown, must be used to ensure that a user cannot drop to the GRUB shell and disable verification.

Introducing the following GRUB commands.

1. append_list_db:
Show the list of trusted certificates from the db list
2. append_add_db_cert:
Add the trusted certificate to the db list
3. append_rm_dbx_cert:
Remove the distrusted certificate from the db list
4. append_verify:
Verify the signed file using db list

---
 grub-core/Makefile.core.def                  |  14 +
 grub-core/commands/appendedsig/appendedsig.c | 691 +++++++++++++++++++
 include/grub/file.h                          |   2 +
 3 files changed, 707 insertions(+)
 create mode 100644 grub-core/commands/appendedsig/appendedsig.c
grub-core/Makefile.core.def +14 -0
index b72f322b1..23fddba9a 100644
@@ -980,6 +980,20 @@ module = {
   cppflags = '$(CPPFLAGS_GCRY)';
 };
 
+module = {
+  name = appendedsig;
+  common = commands/appendedsig/appendedsig.c;
+  common = commands/appendedsig/x509.c;
+  common = commands/appendedsig/pkcs7.c;
+  common = commands/appendedsig/asn1util.c;
+  common = commands/appendedsig/gnutls_asn1_tab.c;
+  common = commands/appendedsig/pkix_asn1_tab.c;
+  enable = powerpc_ieee1275;
+  cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls';
+  cppflags = '$(CPPFLAGS_GCRY) -I$(srcdir)/lib/libtasn1-grub';
+  depends = crypto, gcry_rsa, gcry_sha256, gcry_sha512, mpi, asn1;
+};
+
 module = {
   name = hdparm;
   common = commands/hdparm.c;
grub-core/commands/appendedsig/appendedsig.c new file +691 -0

691 lines not on this page · raw message

include/grub/file.h +2 -0
index a5bf3a792..d678de063 100644
@@ -80,6 +80,8 @@ enum grub_file_type
     GRUB_FILE_TYPE_PUBLIC_KEY,
     /* File holding public key to add to trused keys.  */
     GRUB_FILE_TYPE_PUBLIC_KEY_TRUST,
+    /* File holding x509 certificiate to add to trusted keys.  */
+    GRUB_FILE_TYPE_CERTIFICATE_TRUST,
     /* File of which we intend to print a blocklist to the user.  */
     GRUB_FILE_TYPE_PRINT_BLOCKLIST,
     /* File we intend to use for test loading or testing speed.  */
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#11 lore raw

Re: [PATCH v6 10/20] appended signatures: Support verifying appended signatures

Quoted 32 lines · Sudhakar Kuppusamy

Building on the parsers and the ability to embed X.509 certificates, as well as the existing gcrypt functionality, add a module for verifying appended signatures.

This includes a verifier that requires that Linux kernels and GRUB modules have appended signatures, and commands to manage the list of trusted certificates for verification.

Verification must be enabled by setting check_appended_signatures. If secure boot is enabled with enforced mode when the module is loaded, verification will be enabled and locked automatically. If verification is enabled, extract trusted keys from the GRUB ELF Note and store them in the db.

As with the PGP verifier, it is not a complete secure-boot solution:
other mechanisms, such as a password or lockdown, must be used to ensure that a user cannot drop to the GRUB shell and disable verification.

Introducing the following GRUB commands.

1. append_list_db:
Show the list of trusted certificates from the db list
2. append_add_db_cert:
Add the trusted certificate to the db list
3. append_rm_dbx_cert:
Remove the distrusted certificate from the db list
4. append_verify:
Verify the signed file using db list

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+static bool
+is_cert_removed_from_db (const struct x509_certificate *cert)

s/is_cert_removed_from_db/remove_cert_from_db/

And then I would return grub_err_t, GRUB_ERR_NONE/GRUB_ERR_FILE_NOT_FOUND, or even void... More details below...

Quoted 28 lines
grub-core/commands/appendedsig/appendedsig.c new file +28 -0
+{
+  int i = 1;
+  struct x509_certificate *curr_cert, *prev_cert;
+
+  for (curr_cert = prev_cert = db; curr_cert != NULL; curr_cert = curr_cert->next)
+    {
+      if (is_cert_match (curr_cert, cert) == true)
+        {
+          if (i == 1) /* Match with first certificate in the db list. */
+            db = curr_cert->next;
+          else
+            prev_cert->next = curr_cert->next;
+
+          grub_dprintf ("appendedsig",
+                        "removed certificate with CN: %s from the db list\n", curr_cert->subject);
+          curr_cert->next = NULL;
+          certificate_release (curr_cert);
+          grub_free (curr_cert);
+          return true;
+	}
+      else
+        prev_cert = curr_cert;
+
+      i++;
+    }
+
+  return false;
+}

[...]

Quoted 18 lines
grub-core/commands/appendedsig/appendedsig.c new file +18 -0
+static grub_err_t
+grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **args)
+{
+  grub_err_t err;
+  grub_file_t cert_file;
+  struct x509_certificate *cert = NULL;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                       "a distrusted X.509 certificate file is expected in DER format\n"
+                       "Example:\n\tappend_rm_dbx_cert <X509_CERTIFICATE>\n");
+
+  if (*args == NULL)
+    return grub_error (GRUB_ERR_BAD_FILENAME, "missing distrusted X.509 certificate file");
+
+  cert_file = grub_file_open (args[0],
+                              GRUB_FILE_TYPE_CERTIFICATE_TRUST | GRUB_FILE_TYPE_NO_DECOMPRESS);
+  if (cert_file == NULL)

grub-core/commands/appendedsig/appendedsig.c line 504

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10 -0
+    return grub_error (GRUB_ERR_BAD_FILE_TYPE, "could not open %s file", args[0]);
+
+  err = read_cert_from_file (cert_file, &cert);
+  grub_file_close (cert_file);
+  if (err != GRUB_ERR_NONE)
+    grub_free (cert);
+
+  if (is_cert_removed_from_db (cert) == false)
+    err = grub_error (GRUB_ERR_EOF,
+                      "not found certificate with CN:%s in the db list", cert->subject);

First of all, I am not convinced the cert should be removed automatically from the db. I think it would be better if it is documented it should be done manually. However, if you convince me it should be done automatically here then lack of cert in the db should not trigger an error...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#12 lore raw

Re: [PATCH v6 10/20] appended signatures: Support verifying appended signatures

Thank you Daniel.

Quoted 38 lines · Daniel Kiper
Quoted 32 lines · Sudhakar Kuppusamy

Building on the parsers and the ability to embed X.509 certificates, as well as the existing gcrypt functionality, add a module for verifying appended signatures.

This includes a verifier that requires that Linux kernels and GRUB modules have appended signatures, and commands to manage the list of trusted certificates for verification.

Verification must be enabled by setting check_appended_signatures. If secure boot is enabled with enforced mode when the module is loaded, verification will be enabled and locked automatically. If verification is enabled, extract trusted keys from the GRUB ELF Note and store them in the db.

As with the PGP verifier, it is not a complete secure-boot solution:
other mechanisms, such as a password or lockdown, must be used to ensure that a user cannot drop to the GRUB shell and disable verification.

Introducing the following GRUB commands.

1. append_list_db:
Show the list of trusted certificates from the db list
2. append_add_db_cert:
Add the trusted certificate to the db list
3. append_rm_dbx_cert:
Remove the distrusted certificate from the db list
4. append_verify:
Verify the signed file using db list

[...]

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static bool
+is_cert_removed_from_db (const struct x509_certificate *cert)

s/is_cert_removed_from_db/remove_cert_from_db/

And then I would return grub_err_t, GRUB_ERR_NONE/GRUB_ERR_FILE_NOT_FOUND, or even void... More details below...

Sure. Will do it.

Quoted 57 lines
Quoted 29 lines
grub-core/commands/appendedsig/appendedsig.c new file +28 -0
+{
+  int i = 1;
+  struct x509_certificate *curr_cert, *prev_cert;
+
+  for (curr_cert = prev_cert = db; curr_cert != NULL; curr_cert = curr_cert->next)
+    {
+      if (is_cert_match (curr_cert, cert) == true)
+        {
+          if (i == 1) /* Match with first certificate in the db list. */
+            db = curr_cert->next;
+          else
+            prev_cert->next = curr_cert->next;
+
+          grub_dprintf ("appendedsig",
+                        "removed certificate with CN: %s from the db list\n", curr_cert->subject);
+          curr_cert->next = NULL;
+          certificate_release (curr_cert);
+          grub_free (curr_cert);
+          return true;
+ }
+      else
+        prev_cert = curr_cert;
+
+      i++;
+    }
+
+  return false;
+}

[...]

Quoted 25 lines
grub-core/commands/appendedsig/appendedsig.c new file +25 -0
+static grub_err_t
+grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **args)
+{
+  grub_err_t err;
+  grub_file_t cert_file;
+  struct x509_certificate *cert = NULL;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                       "a distrusted X.509 certificate file is expected in DER format\n"
+                       "Example:\n\tappend_rm_dbx_cert <X509_CERTIFICATE>\n");
+
+  if (*args == NULL)
+    return grub_error (GRUB_ERR_BAD_FILENAME, "missing distrusted X.509 certificate file");
+
+  cert_file = grub_file_open (args[0],
+                              GRUB_FILE_TYPE_CERTIFICATE_TRUST | GRUB_FILE_TYPE_NO_DECOMPRESS);
+  if (cert_file == NULL)
+    return grub_error (GRUB_ERR_BAD_FILE_TYPE, "could not open %s file", args[0]);
+
+  err = read_cert_from_file (cert_file, &cert);
+  grub_file_close (cert_file);
+  if (err != GRUB_ERR_NONE)
+    grub_free (cert);
+
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c line 504

Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3 -0
+  if (is_cert_removed_from_db (cert) == false)
+    err = grub_error (GRUB_ERR_EOF,
+                      "not found certificate with CN:%s in the db list", cert->subject);

First of all, I am not convinced the cert should be removed automatically from the db. I think it would be better if it is documented it should be done manually. However, if you convince me it should be done automatically here then lack of cert in the db should not trigger an error...

It is not automatically removing the cert from the db but does it manually when user try to remove distrusted cert via append_rm_dbx_cert command.

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw

Re: [PATCH v6 10/20] appended signatures: Support verifying appended signatures

Quoted 4 lines · Sudhakar Kuppusamy

Thank you Daniel.

Quoted 2 lines · Daniel Kiper

On Tue, Jul 29, 2025 at 08:21:46PM +0530, Sudhakar Kuppusamy wrote:

[...]

Quoted 11 lines
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c line 504

Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3 -0
+  if (is_cert_removed_from_db (cert) == false)
+    err = grub_error (GRUB_ERR_EOF,
+                      "not found certificate with CN:%s in the db list", cert->subject);

First of all, I am not convinced the cert should be removed automatically from the db. I think it would be better if it is documented it should be done manually. However, if you convince me it should be done automatically here then lack of cert in the db should not trigger an error...

It is not automatically removing the cert from the db but does it manually when user try to remove distrusted cert via append_rm_dbx_cert command.

So, I mean it should not happen then...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#14 lore raw

Re: [PATCH v6 10/20] appended signatures: Support verifying appended signatures

Quoted 21 lines · Daniel Kiper
Quoted 4 lines · Sudhakar Kuppusamy

Thank you Daniel.

Quoted 2 lines · Daniel Kiper

On Tue, Jul 29, 2025 at 08:21:46PM +0530, Sudhakar Kuppusamy wrote:

[...]

Quoted 9 lines
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c line 504

Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3 -0
+  if (is_cert_removed_from_db (cert) == false)
+    err = grub_error (GRUB_ERR_EOF,
+                      "not found certificate with CN:%s in the db list", cert->subject);

First of all, I am not convinced the cert should be removed automatically from the db. I think it would be better if it is documented it should be done manually. However, if you convince me it should be done automatically here then lack of cert in the db should not trigger an error...

Quoted 4 lines
Quoted 2 lines

It is not automatically removing the cert from the db but does it manually when user try to remove distrusted cert via append_rm_dbx_cert command.

So, I mean it should not happen then...

The removal of certificate here is not persist accross the boots, it is only for the current boot. Also, this command accepts only signed certificates when secure boot is set to enabled.

I do not understand “automatic" and “manual” from your previous comments. Could you please elabarate it.

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#15 lore raw

Re: [PATCH v6 10/20] appended signatures: Support verifying appended signatures

Quoted 18 lines · Sudhakar Kuppusamy
Quoted 18 lines · Daniel Kiper
Quoted 4 lines · Sudhakar Kuppusamy

Thank you Daniel.

Quoted 2 lines · Daniel Kiper

On Tue, Jul 29, 2025 at 08:21:46PM +0530, Sudhakar Kuppusamy wrote:

[...]

Quoted 9 lines
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c line 504

Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3 -0
+  if (is_cert_removed_from_db (cert) == false)
+    err = grub_error (GRUB_ERR_EOF,
+                      "not found certificate with CN:%s in the db list", cert->subject);

First of all, I am not convinced the cert should be removed automatically from the db. I think it would be better if it is documented it should be done manually. However, if you convince me it should be done automatically here then lack of cert in the db should not trigger an error...

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

It is not automatically removing the cert from the db but does it manually when user try to remove distrusted cert via append_rm_dbx_cert command.

So, I mean it should not happen then...

The removal of certificate here is not persist accross the boots, it is only for the current boot.

Ahhh... OK... You can ignore my comment then. Though I think it means comments and/or code should be more clear about it...

Quoted 4 lines

Also, this command accepts only signed certificates when secure boot is set to enabled.

I do not understand “automatic" and “manual” from your previous comments. Could you please elabarate it.

When I say "automatic" I mean here the command at once inserts a given cert into dbx and removes it from the db.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#16 lore raw

Re: [PATCH v6 10/20] appended signatures: Support verifying appended signatures

Quoted 26 lines · Daniel Kiper
Quoted 21 lines · Sudhakar Kuppusamy
Quoted 21 lines · Daniel Kiper
Quoted 4 lines · Sudhakar Kuppusamy

Thank you Daniel.

Quoted 2 lines · Daniel Kiper

On Tue, Jul 29, 2025 at 08:21:46PM +0530, Sudhakar Kuppusamy wrote:

[...]

Quoted 11 lines
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c line 504

Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3 -0
+  if (is_cert_removed_from_db (cert) == false)
+    err = grub_error (GRUB_ERR_EOF,
+                      "not found certificate with CN:%s in the db list", cert->subject);

First of all, I am not convinced the cert should be removed automatically from the db. I think it would be better if it is documented it should be done manually. However, if you convince me it should be done automatically here then lack of cert in the db should not trigger an error...

It is not automatically removing the cert from the db but does it manually when user try to remove distrusted cert via append_rm_dbx_cert command.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

So, I mean it should not happen then...

The removal of certificate here is not persist accross the boots, it is only for the current boot.

Ahhh... OK... You can ignore my comment then. Though I think it means comments and/or code should be more clear about it...

Sure. Will add clear comments.

Quoted 8 lines
Quoted 5 lines

Also, this command accepts only signed certificates when secure boot is set to enabled.

I do not understand “automatic" and “manual” from your previous comments. Could you please elabarate it.

When I say "automatic" I mean here the command at once inserts a given cert into dbx and removes it from the db.

Thank you Daniel.

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#17 lore raw

PATCH v6 11/20 powerpc_ieee1275: Read the db and dbx secure boot variables

In reply to Sudhakar Kuppusamy

Enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB_VERSION, db, and dbx secure boot variables from PKS.

If secure boot is enabled with dynamic key management mode, it will read secure boot variables such as db and dbx from PKS and extract EFI Signature List (ESL) from it. The ESLs would be saved in the Platform Keystore buffer, and the appendedsig module would read it later to extract the certificate's details from ESL.

In the following scenarios, static key management mode will be activated:
1. When Secure Boot is enabled with static key management mode
2. When SB_VERSION is unavailable but Secure Boot is enabled
3. When PKS support is unavailable but Secure Boot is enabled

Note:-

SB_VERSION: Key Management Mode
1 - Enable dynamic key management mode. Read the db and dbx variables from PKS,
and use them for signature verification.
0 - Enable static key management mode. Read keys from the GRUB ELF Note and
use it for signature verification.

---
 grub-core/Makefile.am                         |   2 +
 grub-core/Makefile.core.def                   |   2 +
 grub-core/kern/ieee1275/ieee1275.c            |   1 -
 grub-core/kern/ieee1275/init.c                |  14 +-
 grub-core/kern/powerpc/ieee1275/ieee1275.c    | 139 ++++++++
 .../kern/powerpc/ieee1275/platform_keystore.c | 333 ++++++++++++++++++
 include/grub/ieee1275/ieee1275.h              |   3 +
 include/grub/powerpc/ieee1275/ieee1275.h      |  20 ++
 .../grub/powerpc/ieee1275/platform_keystore.h | 114 ++++++
 9 files changed, 626 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/kern/powerpc/ieee1275/ieee1275.c
 create mode 100644 grub-core/kern/powerpc/ieee1275/platform_keystore.c
 create mode 100644 include/grub/powerpc/ieee1275/platform_keystore.h
grub-core/Makefile.am +2 -0
index e50db8106..cd6bb7c32 100644
@@ -241,11 +241,13 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 endif
 
 if COND_powerpc_ieee1275
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/ieee1275.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/alloc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/platform_keystore.h
 endif
 
 if COND_sparc64_ieee1275
grub-core/Makefile.core.def +2 -0
index 23fddba9a..f1068eadd 100644
@@ -328,10 +328,12 @@ kernel = {
   extra_dist = video/sis315_init.c;
   mips_loongson = commands/keylayouts.c;
 
+  powerpc_ieee1275 = kern/powerpc/ieee1275/ieee1275.c;
   powerpc_ieee1275 = kern/powerpc/cache.S;
   powerpc_ieee1275 = kern/powerpc/dl.c;
   powerpc_ieee1275 = kern/powerpc/compiler-rt.S;
   powerpc_ieee1275 = kern/lockdown.c;
+  powerpc_ieee1275 = kern/powerpc/ieee1275/platform_keystore.c;
 
   sparc64_ieee1275 = kern/sparc64/cache.S;
   sparc64_ieee1275 = kern/sparc64/dl.c;
grub-core/kern/ieee1275/ieee1275.c +0 -1
index 36ca2dbfc..afa37a9f0 100644
@@ -23,7 +23,6 @@
 
 #define IEEE1275_PHANDLE_INVALID  ((grub_ieee1275_cell_t) -1)
 #define IEEE1275_IHANDLE_INVALID  ((grub_ieee1275_cell_t) 0)
-#define IEEE1275_CELL_INVALID     ((grub_ieee1275_cell_t) -1)
 
 
 
grub-core/kern/ieee1275/init.c +13 -1
index 0f0047d69..67969da87 100644
@@ -51,6 +51,8 @@
 #endif
 #if defined(__powerpc__)
 #include <grub/lockdown.h>
+#include <grub/powerpc/ieee1275/ieee1275.h>
+#include <grub/powerpc/ieee1275/platform_keystore.h>
 #endif
 
 /* The maximum heap size we're going to claim at boot. Not used by sparc. */
@@ -1006,6 +1008,7 @@ static grub_uint32_t sb_mode = GRUB_SB_DISABLED;
 static void
 grub_ieee1275_get_secure_boot (void)
 {
+  grub_err_t err;
   grub_ieee1275_phandle_t root;
   int rc;
 
@@ -1036,7 +1039,16 @@ grub_ieee1275_get_secure_boot (void)
    * Now, only support disabled and enforced.
    */
   if (sb_mode == GRUB_SB_ENFORCED)
-    grub_lockdown ();
+    {
+      grub_dprintf ("ieee1275", "Secure Boot Enabled\n");
+      err = grub_pks_keystore_init ();
+      if (err != GRUB_ERR_NONE)
+        grub_error (err, "initialization of the Platform Keystore failed.\n");
+
+      grub_lockdown ();
+    }
+  else
+    grub_dprintf ("ieee1275", "Secure Boot Disabled\n");
 }
 
 grub_uint32_t
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +139 -0

139 lines not on this page · raw message

grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +333 -0

333 lines not on this page · raw message

include/grub/ieee1275/ieee1275.h +3 -0
index 6f7925168..365fbb22c 100644
@@ -24,6 +24,9 @@
 #include <grub/types.h>
 #include <grub/machine/ieee1275.h>
 
+#define IEEE1275_CELL_INVALID          ((grub_ieee1275_cell_t) -1)
+#define IEEE1275_CELL_NOT_FOUND        ((int) -7)
+
 #define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
 #define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
 
include/grub/powerpc/ieee1275/ieee1275.h +20 -0
index 4eb207018..b70b813f2 100644
@@ -28,4 +28,24 @@ typedef grub_uint32_t grub_ieee1275_cell_t;
 #define PRIxGRUB_IEEE1275_CELL_T	PRIxGRUB_UINT32_T
 #define PRIuGRUB_IEEE1275_CELL_T	PRIuGRUB_UINT32_T
 
+#ifdef __powerpc__
+
+extern int
+grub_ieee1275_test (const char *name, grub_ieee1275_cell_t *missing);
+
+extern int
+grub_ieee1275_pks_max_object_size (grub_size_t *result);
+
+extern int
+grub_ieee1275_pks_read_object (grub_uint8_t consumer, grub_uint8_t *label,
+                               grub_size_t label_len, grub_uint8_t *buffer,
+                               grub_size_t buffer_len, grub_size_t *data_len,
+                               grub_uint32_t *policies);
+
+extern int
+grub_ieee1275_pks_read_sbvar (grub_uint8_t sbvarflags, grub_uint8_t sbvartype,
+                              grub_uint8_t *buffer, grub_size_t buffer_len,
+                              grub_size_t *data_len);
+#endif
+
 #endif /* ! GRUB_IEEE1275_MACHINE_HEADER */
include/grub/powerpc/ieee1275/platform_keystore.h new file +114 -0

114 lines not on this page · raw message

Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#18 lore raw

Re: [PATCH v6 11/20] powerpc_ieee1275: Read the db and dbx secure boot variables

Quoted 24 lines · Sudhakar Kuppusamy

Enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB_VERSION, db, and dbx secure boot variables from PKS.

If secure boot is enabled with dynamic key management mode, it will read secure boot variables such as db and dbx from PKS and extract EFI Signature List (ESL) from it. The ESLs would be saved in the Platform Keystore buffer, and the appendedsig module would read it later to extract the certificate's details from ESL.

In the following scenarios, static key management mode will be activated:
1. When Secure Boot is enabled with static key management mode
2. When SB_VERSION is unavailable but Secure Boot is enabled
3. When PKS support is unavailable but Secure Boot is enabled

Note:-

SB_VERSION: Key Management Mode
1 - Enable dynamic key management mode. Read the db and dbx variables from PKS,
and use them for signature verification.
0 - Enable static key management mode. Read keys from the GRUB ELF Note and
use it for signature verification.

[...]

include/grub/ieee1275/ieee1275.h line 28

Quoted 10 lines
include/grub/ieee1275/ieee1275.h +2 -0
index 6f7925168..365fbb22c 100644
@@ -24,6 +24,9 @@
 #include <grub/types.h>
 #include <grub/machine/ieee1275.h>
 
+#define IEEE1275_CELL_INVALID          ((grub_ieee1275_cell_t) -1)
+#define IEEE1275_CELL_NOT_FOUND        ((int) -7)

This definition looks strange and I am almost certain it should not be an int but a "grub_" prefixed type...

Quoted 21 lines
include/grub/ieee1275/ieee1275.h +1 -0
+
 #define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
 #define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
 
include/grub/powerpc/ieee1275/ieee1275.h +9 -0
index 4eb207018..b70b813f2 100644
@@ -28,4 +28,24 @@ typedef grub_uint32_t grub_ieee1275_cell_t;
 #define PRIxGRUB_IEEE1275_CELL_T	PRIxGRUB_UINT32_T
 #define PRIuGRUB_IEEE1275_CELL_T	PRIuGRUB_UINT32_T
 
+#ifdef __powerpc__
+
+extern int
+grub_ieee1275_test (const char *name, grub_ieee1275_cell_t *missing);
+
+extern int
+grub_ieee1275_pks_max_object_size (grub_size_t *result);
+
+extern int

include/grub/powerpc/ieee1275/ieee1275.h typedef grub_uint32_t grub_ieee1275_cell_t; · line 49

Quoted 10 lines
include/grub/powerpc/ieee1275/ieee1275.h +10 -0
+grub_ieee1275_pks_read_object (grub_uint8_t consumer, grub_uint8_t *label,
+                               grub_size_t label_len, grub_uint8_t *buffer,
+                               grub_size_t buffer_len, grub_size_t *data_len,
+                               grub_uint32_t *policies);
+
+extern int
+grub_ieee1275_pks_read_sbvar (grub_uint8_t sbvarflags, grub_uint8_t sbvartype,
+                              grub_uint8_t *buffer, grub_size_t buffer_len,
+                              grub_size_t *data_len);
+#endif

Return types does not look right for me. I think you should check IEEE 1275 spec and choose proper "grub_" prefixed type. Same for some args members, e.g. rc, types in these functions.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#19 lore raw

Re: [PATCH v6 11/20] powerpc_ieee1275: Read the db and dbx secure boot variables

Thank you Daniel.

Quoted 38 lines · Daniel Kiper
Quoted 24 lines · Sudhakar Kuppusamy

Enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB_VERSION, db, and dbx secure boot variables from PKS.

If secure boot is enabled with dynamic key management mode, it will read secure boot variables such as db and dbx from PKS and extract EFI Signature List (ESL) from it. The ESLs would be saved in the Platform Keystore buffer, and the appendedsig module would read it later to extract the certificate's details from ESL.

In the following scenarios, static key management mode will be activated:
1. When Secure Boot is enabled with static key management mode
2. When SB_VERSION is unavailable but Secure Boot is enabled
3. When PKS support is unavailable but Secure Boot is enabled

Note:-

SB_VERSION: Key Management Mode
1 - Enable dynamic key management mode. Read the db and dbx variables from PKS,
and use them for signature verification.
0 - Enable static key management mode. Read keys from the GRUB ELF Note and
use it for signature verification.

[...]

Quoted 8 lines
include/grub/ieee1275/ieee1275.h +0 -0
index 6f7925168..365fbb22c 100644
@@ -24,6 +24,9 @@
 #include <grub/types.h>
 #include <grub/machine/ieee1275.h>
 
Quoted 5 lines

include/grub/ieee1275/ieee1275.h line 28

Quoted 2 lines
include/grub/ieee1275/ieee1275.h +2 -0
+#define IEEE1275_CELL_INVALID          ((grub_ieee1275_cell_t) -1)
+#define IEEE1275_CELL_NOT_FOUND        ((int) -7)

This definition looks strange and I am almost certain it should not be an int but a "grub_" prefixed type...

Sure. Will use it like

#define IEEE1275_CELL_NOT_FOUND ((grub_int32_t) -7)

Quoted 28 lines
Quoted 28 lines
include/grub/ieee1275/ieee1275.h +1 -0
+
 #define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
 #define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
 
include/grub/powerpc/ieee1275/ieee1275.h +15 -0
index 4eb207018..b70b813f2 100644
@@ -28,4 +28,24 @@ typedef grub_uint32_t grub_ieee1275_cell_t;
 #define PRIxGRUB_IEEE1275_CELL_T PRIxGRUB_UINT32_T
 #define PRIuGRUB_IEEE1275_CELL_T PRIuGRUB_UINT32_T
 
+#ifdef __powerpc__
+
+extern int
+grub_ieee1275_test (const char *name, grub_ieee1275_cell_t *missing);
+
+extern int
+grub_ieee1275_pks_max_object_size (grub_size_t *result);
+
+extern int
+grub_ieee1275_pks_read_object (grub_uint8_t consumer, grub_uint8_t *label,
+                               grub_size_t label_len, grub_uint8_t *buffer,
+                               grub_size_t buffer_len, grub_size_t *data_len,
+                               grub_uint32_t *policies);
+
+extern int
Quoted 8 lines

include/grub/powerpc/ieee1275/ieee1275.h typedef grub_uint32_t grub_ieee1275_cell_t; · line 49

Quoted 4 lines
include/grub/powerpc/ieee1275/ieee1275.h +4 -0
+grub_ieee1275_pks_read_sbvar (grub_uint8_t sbvarflags, grub_uint8_t sbvartype,
+                              grub_uint8_t *buffer, grub_size_t buffer_len,
+                              grub_size_t *data_len);
+#endif

Return types does not look right for me. I think you should check IEEE 1275 spec and choose proper "grub_" prefixed type. Same for some args members, e.g. rc, types in these functions.

Sure. I will do it.

I followed the below two files and wrote these PKS interfaces.

include/grub/ieee1275/ieee1275.h
grub-core/kern/ieee1275/ieee1275.c

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#20 lore raw

Re: [PATCH v6 11/20] powerpc_ieee1275: Read the db and dbx secure boot variables

Quoted 86 lines · Sudhakar Kuppusamy

Thank you Daniel.

Quoted 38 lines · Daniel Kiper
Quoted 24 lines · Sudhakar Kuppusamy

Enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB_VERSION, db, and dbx secure boot variables from PKS.

If secure boot is enabled with dynamic key management mode, it will read secure boot variables such as db and dbx from PKS and extract EFI Signature List (ESL) from it. The ESLs would be saved in the Platform Keystore buffer, and the appendedsig module would read it later to extract the certificate's details from ESL.

In the following scenarios, static key management mode will be activated:
1. When Secure Boot is enabled with static key management mode
2. When SB_VERSION is unavailable but Secure Boot is enabled
3. When PKS support is unavailable but Secure Boot is enabled

Note:-

SB_VERSION: Key Management Mode
1 - Enable dynamic key management mode. Read the db and dbx variables from PKS,
and use them for signature verification.
0 - Enable static key management mode. Read keys from the GRUB ELF Note and
use it for signature verification.

[...]

Quoted 8 lines
include/grub/ieee1275/ieee1275.h +0 -0
index 6f7925168..365fbb22c 100644
@@ -24,6 +24,9 @@
 #include <grub/types.h>
 #include <grub/machine/ieee1275.h>
 
Quoted 5 lines

include/grub/ieee1275/ieee1275.h line 28

Quoted 2 lines
include/grub/ieee1275/ieee1275.h +2 -0
+#define IEEE1275_CELL_INVALID          ((grub_ieee1275_cell_t) -1)
+#define IEEE1275_CELL_NOT_FOUND        ((int) -7)

This definition looks strange and I am almost certain it should not be an int but a "grub_" prefixed type...

Sure. Will use it like

#define IEEE1275_CELL_NOT_FOUND ((grub_int32_t) -7)

Quoted 28 lines
Quoted 28 lines
include/grub/ieee1275/ieee1275.h +1 -0
+
 #define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
 #define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
 
include/grub/powerpc/ieee1275/ieee1275.h +15 -0
index 4eb207018..b70b813f2 100644
@@ -28,4 +28,24 @@ typedef grub_uint32_t grub_ieee1275_cell_t;
 #define PRIxGRUB_IEEE1275_CELL_T PRIxGRUB_UINT32_T
 #define PRIuGRUB_IEEE1275_CELL_T PRIuGRUB_UINT32_T
 
+#ifdef __powerpc__
+
+extern int
+grub_ieee1275_test (const char *name, grub_ieee1275_cell_t *missing);
+
+extern int
+grub_ieee1275_pks_max_object_size (grub_size_t *result);
+
+extern int
+grub_ieee1275_pks_read_object (grub_uint8_t consumer, grub_uint8_t *label,
+                               grub_size_t label_len, grub_uint8_t *buffer,
+                               grub_size_t buffer_len, grub_size_t *data_len,
+                               grub_uint32_t *policies);
+
+extern int
Quoted 8 lines

include/grub/powerpc/ieee1275/ieee1275.h typedef grub_uint32_t grub_ieee1275_cell_t; · line 49

Quoted 4 lines
include/grub/powerpc/ieee1275/ieee1275.h +4 -0
+grub_ieee1275_pks_read_sbvar (grub_uint8_t sbvarflags, grub_uint8_t sbvartype,
+                              grub_uint8_t *buffer, grub_size_t buffer_len,
+                              grub_size_t *data_len);
+#endif

Return types does not look right for me. I think you should check IEEE 1275 spec and choose proper "grub_" prefixed type. Same for some args members, e.g. rc, types in these functions.

Quoted 1 line

Sure. I will do it.

Thank you!

Quoted 4 lines

I followed the below two files and wrote these PKS interfaces.

include/grub/ieee1275/ieee1275.h
grub-core/kern/ieee1275/ieee1275.c

I think this should be fixed too. Though it can be done later...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#21 lore raw

PATCH v6 12/20 appended signatures: Create db and dbx lists

In reply to Sudhakar Kuppusamy

If secure boot is enabled with static key management mode, the trusted certificates will be extracted from the GRUB ELF Note and added to db list. This is introduced by a subsequent patch.

If secure boot is enabled with dynamic key management mode, the trusted certificates and certificate/binary hash will be extracted from the PKS and added to db list. The distrusted certificates, certificate/binary hash from the PKS and added to dbx list. Both dbx and db lists are introduced by a subsequent patch.

Note:-

If the certificate or the certificate hash exists in the dbx list, then do not add that certificate/certificate hash to the db list.

---
 grub-core/commands/appendedsig/appendedsig.c | 530 +++++++++++++++++--
 include/grub/crypto.h                        |   1 +
 include/grub/efi/pks.h                       | 112 ++++
 include/grub/types.h                         |   4 +
 4 files changed, 606 insertions(+), 41 deletions(-)
 create mode 100644 include/grub/efi/pks.h
grub-core/commands/appendedsig/appendedsig.c +489 -41
index 48f1ec94e..1e5610ddf 100644
@@ -33,7 +33,8 @@
 #include <libtasn1.h>
 #include <grub/env.h>
 #include <grub/ieee1275/ieee1275.h>
-
+#include <grub/efi/pks.h>
+#include <grub/powerpc/ieee1275/platform_keystore.h>
 #include "appendedsig.h"
 
 GRUB_MOD_LICENSE ("GPLv3+");
@@ -67,8 +68,30 @@ struct grub_appended_signature
   struct pkcs7_signedData pkcs7;        /* Parsed PKCS#7 data. */
 };
 
-/* Trusted certificates for verifying appended signatures. */
-struct x509_certificate *db;
+/* This represents a db/dbx list. */
+struct grub_database
+{
+  struct x509_certificate *certs; /* Certificates. */
+  grub_size_t cert_entries;       /* Number of certificates. */
+  grub_uint8_t **signatures;      /* Certificate/binary hashes. */
+  grub_size_t *signature_size;    /* Size of certificate/binary hashes. */
+  grub_size_t signature_entries;  /* Number of certificate/binary hashes. */
+};
+
+/* The db list */
+struct grub_database db = {.certs = NULL, .cert_entries = 0, .signatures = NULL,
+                           .signature_size = NULL, .signature_entries = 0};
+
+/* The dbx list */
+struct grub_database dbx = {.certs = NULL, .cert_entries = 0, .signatures = NULL,
+                            .signature_size = NULL, .signature_entries = 0};
+
+/* Free db list memory */
+static void
+free_db_list (void);
+/* Free dbx list memory */
+static void
+free_dbx_list (void);
 
 /* Appended signature size. */
 static grub_size_t append_sig_len = 0;
@@ -124,6 +147,94 @@ grub_env_write_sec (struct grub_env_var *var __attribute__ ((unused)), const cha
   return ret;
 }
 
+/*
+ * GUID can be used to determine the hashing function and
+ * generate the hash using determined hashing function.
+ */
+static grub_err_t
+get_hash (const grub_packed_guid_t *guid, const grub_uint8_t *data, const grub_size_t data_size,
+          grub_uint8_t *hash, grub_size_t *hash_size)
+{
+  gcry_md_spec_t *hash_func = NULL;
+
+  if (guid == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "GUID is not available");
+
+  if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID, GRUB_PACKED_GUID_SIZE) == 0 ||
+      grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID, GRUB_PACKED_GUID_SIZE) == 0)
+    hash_func = &_gcry_digest_spec_sha256;
+  else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID, GRUB_PACKED_GUID_SIZE) == 0 ||
+           grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID, GRUB_PACKED_GUID_SIZE) == 0)
+    hash_func = &_gcry_digest_spec_sha384;
+  else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID, GRUB_PACKED_GUID_SIZE) == 0 ||
+           grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID, GRUB_PACKED_GUID_SIZE) == 0)
+    hash_func = &_gcry_digest_spec_sha512;
+  else
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "unsupported GUID hash");
+
+  grub_memset (hash, 0, GRUB_MAX_HASH_SIZE);
+  grub_crypto_hash (hash_func, hash, data, data_size);
+  *hash_size =  hash_func->mdlen;
+
+  return GRUB_ERR_NONE;
+}
+
+/* Add the certificate/binary hash into the db/dbx list. */
+static grub_err_t
+add_hash (const grub_uint8_t **data, const grub_size_t data_size,
+          grub_uint8_t ***signature_list, grub_size_t **signature_size_list,
+          grub_size_t *signature_list_entries)
+{
+  grub_uint8_t **signatures = *signature_list;
+  grub_size_t *signature_size = *signature_size_list;
+  grub_size_t signature_entries = *signature_list_entries;
+
+  if (*data == NULL || data_size == 0)
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "certificate/binary-hash data or size is not available");
+
+  signatures = grub_realloc (signatures, sizeof (grub_uint8_t *) * (signature_entries + 1));
+  signature_size = grub_realloc (signature_size,
+                                 sizeof (grub_size_t) * (signature_entries + 1));
+
+  if (signatures == NULL || signature_size == NULL)
+    {
+      /*
+       * Allocated memory will be freed by
+       * free_db_list/free_dbx_list.
+       */
+      if (signatures != NULL)
+        {
+          *signature_list = signatures;
+          *signature_list_entries = signature_entries + 1;
+        }
+
+      if (signature_size != NULL)
+        *signature_size_list = signature_size;
+
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
+    }
+
+  signatures[signature_entries] = (grub_uint8_t *) *data;
+  signature_size[signature_entries] = data_size;
+  signature_entries++;
+  *data = NULL;
+
+  *signature_list = signatures;
+  *signature_size_list = signature_size;
+  *signature_list_entries = signature_entries;
+
+  return GRUB_ERR_NONE;
+}
+
+static bool
+is_x509 (const grub_packed_guid_t *guid)
+{
+  if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_GUID, GRUB_PACKED_GUID_SIZE) == 0)
+    return true;
+
+  return false;
+}
+
 static bool
 is_cert_match (const struct x509_certificate *distrusted_cert,
                const struct x509_certificate *db_cert)
@@ -138,6 +249,95 @@ is_cert_match (const struct x509_certificate *distrusted_cert,
   return false;
 }
 
+/* Check the certificate presence in the Platform Keystore dbx list. */
+static grub_err_t
+is_dbx_cert (const struct x509_certificate *db_cert)
+{
+  grub_err_t rc;
+  grub_size_t i;
+  struct x509_certificate *distrusted_cert;
+
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (grub_pks_keystore.dbx[i].data == NULL)
+        continue;
+
+      if (is_x509 (&grub_pks_keystore.dbx[i].guid) == true)
+        {
+          distrusted_cert = grub_zalloc (sizeof (struct x509_certificate));
+          if (distrusted_cert == NULL)
+            return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
+
+          rc = parse_x509_certificate (grub_pks_keystore.dbx[i].data,
+                                       grub_pks_keystore.dbx[i].data_size, distrusted_cert);
+          if (rc != GRUB_ERR_NONE)
+            {
+              grub_free (distrusted_cert);
+              continue;
+            }
+
+          if (is_cert_match (distrusted_cert, db_cert) == true)
+            {
+              grub_dprintf ("appendedsig", "a trusted certificate CN='%s' is ignored "
+                            "because it is on the dbx list\n", db_cert->subject);
+              return GRUB_ERR_ACCESS_DENIED;
+            }
+
+          certificate_release (distrusted_cert);
+          grub_free (distrusted_cert);
+        }
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+/* Add the certificate into the db/dbx list */
+static grub_err_t
+add_certificate (const grub_uint8_t *data, const grub_size_t data_size,
+                 struct grub_database *database, const bool is_db)
+{
+  struct x509_certificate *cert;
+  grub_err_t rc;
+  grub_size_t cert_entries = database->cert_entries;
+
+  if (data == NULL || data_size == 0)
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "certificate data or size is not available");
+
+  cert = grub_zalloc (sizeof (struct x509_certificate));
+  if (cert == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
+
+  rc = parse_x509_certificate (data, data_size, cert);
+  if (rc != GRUB_ERR_NONE)
+    {
+      grub_dprintf ("appendedsig", "skipped %s certificate (%d)\n",
+                    ((is_db == true) ? "trusted" : "distrusted"), (int) rc);
+      grub_free (cert);
+      return rc;
+    }
+
+  if (is_db == true)
+    {
+      rc = is_dbx_cert (cert);
+      if (rc != GRUB_ERR_NONE)
+        {
+          certificate_release (cert);
+          grub_free (cert);
+          return rc;
+        }
+    }
+
+  grub_dprintf ("appendedsig", "add a %s certificate CN='%s'\n",
+                ((is_db == true) ? "trusted" : "distrusted"), cert->subject);
+
+  cert_entries++;
+  cert->next = database->certs;
+  database->certs = cert;
+  database->cert_entries = cert_entries;
+
+  return rc;
+}
+
 static grub_err_t
 file_read_whole (grub_file_t file, grub_uint8_t **buf, grub_size_t *len)
 {
@@ -272,7 +472,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
   struct pkcs7_signerInfo *si;
   int i;
 
-  if (db == NULL)
+  if (!db.cert_entries)
     return grub_error (GRUB_ERR_BAD_SIGNATURE, "no trusted keys to verify against");
 
   err = extract_appended_signature (buf, bufsize, &sig);
@@ -303,7 +503,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
       grub_dprintf ("appendedsig", "data size %" PRIuGRUB_SIZE ", signer %d hash %02x%02x%02x%02x...\n",
                     datasize, i, hash[0], hash[1], hash[2], hash[3]);
 
-      for (pk = db; pk != NULL; pk = pk->next)
+      for (pk = db.certs; pk != NULL; pk = pk->next)
         {
           err = verify_signature (pk->mpis, si->sig_mpi, si->hash, hash);
           if (err == GRUB_ERR_NONE)
@@ -359,7 +559,7 @@ is_cert_present_in_db (const struct x509_certificate *cert_in)
 {
   struct x509_certificate *cert;
 
-  for (cert = db; cert; cert = cert->next)
+  for (cert = db.certs; cert; cert = cert->next)
     {
       if (is_cert_match (cert, cert_in) == true)
         return true;
@@ -374,12 +574,12 @@ is_cert_removed_from_db (const struct x509_certificate *cert)
   int i = 1;
   struct x509_certificate *curr_cert, *prev_cert;
 
-  for (curr_cert = prev_cert = db; curr_cert != NULL; curr_cert = curr_cert->next)
+  for (curr_cert = prev_cert = db.certs; curr_cert != NULL; curr_cert = curr_cert->next)
     {
       if (is_cert_match (curr_cert, cert) == true)
         {
           if (i == 1) /* Match with first certificate in the db list. */
-            db = curr_cert->next;
+            db.certs = curr_cert->next;
           else
             prev_cert->next = curr_cert->next;
 
@@ -468,8 +668,9 @@ grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **
 
   grub_dprintf ("appendedsig", "added certificate with CN: %s\n", cert->subject);
 
-  cert->next = db;
-  db = cert;
+  cert->next = db.certs;
+  db.certs = cert;
+  db.cert_entries++;
 
   return GRUB_ERR_NONE;
 }
@@ -517,7 +718,7 @@ grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib
   int cert_num = 1;
   grub_size_t i;
 
-  for (cert = db; cert != NULL; cert = cert->next)
+  for (cert = db.certs; cert != NULL; cert = cert->next)
     {
       grub_printf ("Certificate %d:\n", cert_num);
       grub_printf ("\tSerial: ");
@@ -609,6 +810,238 @@ static struct grub_fs pseudo_fs = {
 
 static grub_command_t cmd_verify, cmd_list_db, cmd_dbx_cert, cmd_db_cert;
 
+/* Check the certificate hash presence in the PKS dbx list. */
+static bool
+is_dbx_cert_hash (const grub_uint8_t *data, const grub_size_t data_size)
+{
+  grub_err_t rc;
+  grub_size_t i, cert_hash_size = 0;
+  grub_uint8_t cert_hash[GRUB_MAX_HASH_SIZE] = { 0 };
+
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (grub_pks_keystore.dbx[i].data == NULL ||
+          grub_pks_keystore.dbx[i].data_size == 0)
+        continue;
+
+      rc = get_hash (&grub_pks_keystore.dbx[i].guid, data, data_size,
+                     cert_hash, &cert_hash_size);
+      if (rc != GRUB_ERR_NONE)
+        continue;
+
+      if (cert_hash_size == grub_pks_keystore.dbx[i].data_size &&
+          grub_memcmp (grub_pks_keystore.dbx[i].data, cert_hash, cert_hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "a trusted certificate (%02x%02x%02x%02x) is ignored "
+                        "because this certificate hash is on the dbx list\n",
+                        cert_hash[0], cert_hash[1], cert_hash[2], cert_hash[3]);
+          return true;
+        }
+    }
+
+  return false;
+}
+
+/* Check the binary hash presence in the PKS dbx list. */
+static bool
+is_dbx_binary_hash (const grub_uint8_t *binary_hash, const grub_size_t binary_hash_size)
+{
+  grub_size_t i;
+
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (grub_pks_keystore.dbx[i].data == NULL ||
+          grub_pks_keystore.dbx[i].data_size == 0)
+        continue;
+
+      if (binary_hash_size == grub_pks_keystore.dbx[i].data_size &&
+          grub_memcmp (grub_pks_keystore.dbx[i].data, binary_hash, binary_hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "a trusted binary hash (%02x%02x%02x%02x) is ignored"
+                        " because it is on the dbx list\n", binary_hash[0], binary_hash[1],
+                        binary_hash[2], binary_hash[3]);
+          return true;
+        }
+    }
+
+  return false;
+}
+
+/* Add the binary hash to the db list if it does not exist in the PKS dbx list. */
+static grub_err_t
+add_db_binary_hash (const grub_uint8_t **data, const grub_size_t data_size)
+{
+  if (*data == NULL || data_size == 0)
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "trusted binary hash data or size is not available");
+
+  if (is_dbx_binary_hash (*data, data_size) == false)
+    return add_hash (data, data_size, &db.signatures, &db.signature_size,
+                     &db.signature_entries);
+
+  return GRUB_ERR_BAD_SIGNATURE;
+}
+
+static bool
+is_hash (const grub_packed_guid_t *guid)
+{
+  /* GUID type of the binary hash. */
+  if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID, GRUB_PACKED_GUID_SIZE) == 0 ||
+      grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID, GRUB_PACKED_GUID_SIZE) == 0 ||
+      grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID, GRUB_PACKED_GUID_SIZE) == 0)
+    return true;
+
+  /* GUID type of the certificate hash. */
+  if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID, GRUB_PACKED_GUID_SIZE) == 0 ||
+      grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID, GRUB_PACKED_GUID_SIZE) == 0 ||
+      grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID, GRUB_PACKED_GUID_SIZE) == 0)
+    return true;
+
+  return false;
+}
+
+/* Add the X.509 certificates/binary hash to the db list from PKS. */
+static grub_err_t
+create_db_list (void)
+{
+  grub_err_t rc;
+  grub_size_t i;
+
+  for (i = 0; i < grub_pks_keystore.db_entries; i++)
+    {
+      if (is_hash (&grub_pks_keystore.db[i].guid) == true)
+        {
+          rc = add_db_binary_hash ((const grub_uint8_t **) grub_pks_keystore.db[i].data,
+                                   grub_pks_keystore.db[i].data_size);
+          if (rc == GRUB_ERR_OUT_OF_MEMORY)
+            return rc;
+        }
+      else if (is_x509 (&grub_pks_keystore.db[i].guid) == true)
+        {
+          if (is_dbx_cert_hash (grub_pks_keystore.db[i].data,
+                                grub_pks_keystore.db[i].data_size) == true)
+            continue;
+
+          rc = add_certificate (grub_pks_keystore.db[i].data,
+                                grub_pks_keystore.db[i].data_size, &db, true);
+          if (rc == GRUB_ERR_OUT_OF_MEMORY)
+            return rc;
+        }
+      else
+        grub_dprintf ("appendedsig", "unsupported signature data type and "
+                      "skipped (%" PRIuGRUB_SIZE ")\n", i + 1);
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+/* Add the certificates and certificate/binary hash to the dbx list from PKS. */
+static grub_err_t
+create_dbx_list (void)
+{
+  grub_err_t rc;
+  grub_size_t i;
+
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (is_x509 (&grub_pks_keystore.dbx[i].guid) == true)
+        {
+          rc = add_certificate (grub_pks_keystore.dbx[i].data,
+                                grub_pks_keystore.dbx[i].data_size, &dbx, false);
+          if (rc == GRUB_ERR_OUT_OF_MEMORY)
+            return rc;
+        }
+      else if (is_hash (&grub_pks_keystore.dbx[i].guid) == true)
+        {
+          rc = add_hash ((const grub_uint8_t **) grub_pks_keystore.dbx[i].data,
+                         grub_pks_keystore.dbx[i].data_size, &dbx.signatures,
+                         &dbx.signature_size, &dbx.signature_entries);
+          if (rc != GRUB_ERR_NONE)
+            return rc;
+        }
+      else
+        grub_dprintf ("appendedsig", "unsupported signature data type and "
+                      "skipped (%" PRIuGRUB_SIZE ")\n", i + 1);
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+/*
+ * Extract the x509 certificates from the ELF Note header,
+ * parse it, and add it to the db list.
+ */
+static grub_err_t
+build_static_db_list (const struct grub_module_header *header)
+{
+  grub_err_t err;
+  struct grub_file pseudo_file;
+  grub_uint8_t *cert_data = NULL;
+  grub_size_t cert_data_size = 0;
+
+  grub_memset (&pseudo_file, 0, sizeof (pseudo_file));
+  pseudo_file.fs = &pseudo_fs;
+  pseudo_file.size = header->size - sizeof (struct grub_module_header);
+  pseudo_file.data = (char *) header + sizeof (struct grub_module_header);
+
+  grub_dprintf ("appendedsig", "found an x509 key, size=%" PRIuGRUB_UINT64_T "\n",
+                pseudo_file.size);
+
+  err = file_read_whole (&pseudo_file, &cert_data, &cert_data_size);
+  if (err != GRUB_ERR_NONE)
+    return err;
+
+  err = add_certificate (cert_data, cert_data_size, &db, true);
+  grub_free (cert_data);
+
+  return err;
+}
+
+/* Free db list memory */
+static void
+free_db_list (void)
+{
+  struct x509_certificate *cert;
+  grub_size_t i = 0;
+
+  while (db.certs != NULL)
+    {
+      cert = db.certs;
+      db.certs = db.certs->next;
+      certificate_release (cert);
+      grub_free (cert);
+    }
+
+  for (i = 0; i < db.signature_entries; i++)
+    grub_free (db.signatures[i]);
+
+  grub_free (db.signatures);
+  grub_free (db.signature_size);
+  grub_memset (&db, 0, sizeof (db));
+}
+
+/* Free dbx list memory */
+static void
+free_dbx_list (void)
+{
+  struct x509_certificate *cert;
+  grub_size_t i = 0;
+
+  while (dbx.certs != NULL)
+    {
+      cert = dbx.certs;
+      dbx.certs = dbx.certs->next;
+      certificate_release (cert);
+      grub_free (cert);
+    }
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    grub_free (dbx.signatures[i]);
+
+  grub_free (dbx.signatures);
+  grub_free (dbx.signature_size);
+  grub_memset (&dbx, 0, sizeof (dbx));
+}
+
 GRUB_MOD_INIT (appendedsig)
 {
   int rc;
@@ -621,7 +1054,6 @@ GRUB_MOD_INIT (appendedsig)
   if (grub_ieee1275_is_secure_boot () == GRUB_SB_ENFORCED)
     check_sigs = true;
 
-  db = NULL;
   grub_register_variable_hook ("check_appended_signatures", grub_env_read_sec, grub_env_write_sec);
   grub_env_export ("check_appended_signatures");
 
@@ -630,38 +1062,54 @@ GRUB_MOD_INIT (appendedsig)
     grub_fatal ("error initing ASN.1 data structures: %d: %s\n", rc, asn1_strerror (rc));
 
   /*
-   * If signature verification is enabled,
-   * extract trusted keys from ELF Note and store them in the db.
+   * If signature verification is enabled with static key management mode,
+   * extract trusted keys from ELF Note and store them in the db list.
    */
-  if (check_sigs == true)
+  if (grub_pks_use_keystore == false && check_sigs == true)
     {
       FOR_MODULES (header)
-      {
-        struct grub_file pseudo_file;
-        struct x509_certificate *pk = NULL;
-        grub_err_t err;
-
-        /* Not an X.509 certificate, skip. */
-        if (header->type != OBJ_TYPE_X509_PUBKEY)
-          continue;
-
-        grub_memset (&pseudo_file, 0, sizeof (pseudo_file));
-        pseudo_file.fs = &pseudo_fs;
-        pseudo_file.size = header->size - sizeof (struct grub_module_header);
-        pseudo_file.data = (char *) header + sizeof (struct grub_module_header);
-
-        grub_dprintf ("appendedsig", "found an x509 key, size=%" PRIuGRUB_UINT64_T "\n",
-                      pseudo_file.size);
-
-        err = read_cert_from_file (&pseudo_file, &pk);
-        if (err != GRUB_ERR_NONE)
-          grub_fatal ("error loading initial key: %s", grub_errmsg);
-
-        grub_dprintf ("appendedsig", "loaded certificate CN='%s'\n", pk->subject);
-
-        pk->next = db;
-        db = pk;
-      }
+        {
+          /* Not an ELF module, skip.  */
+          if (header->type != OBJ_TYPE_X509_PUBKEY)
+            continue;
+          rc = build_static_db_list (header);
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              grub_error (rc, "static db list creation failed");
+            }
+          else
+            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " static keys\n",
+                          db.cert_entries);
+        }
+    }
+  /*
+   * If signature verification is enabled with dynamic key management mode,
+   * extract trusted and distrusted keys from PKS and store them in the db and dbx list.
+   */
+  else if (grub_pks_use_keystore == true && check_sigs == true)
+    {
+      rc = create_db_list ();
+      if (rc != GRUB_ERR_NONE)
+        {
+          free_db_list ();
+          grub_error (rc, "db list creation failed");
+        }
+      else
+        {
+          rc = create_dbx_list ();
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              free_dbx_list ();
+              grub_error (rc, "dbx list creation failed");
+            }
+          else
+            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " keys\n"
+                          "the dbx list now has %" PRIuGRUB_SIZE " keys\n",
+                          db.signature_entries + db.cert_entries, dbx.signature_entries);
+        }
+      grub_pks_free_keystore ();
     }
 
   cmd_db_cert = grub_register_command ("append_add_db_cert", grub_cmd_db_cert, N_("X509_CERTIFICATE"),
include/grub/crypto.h +1 -0
index b0d7add1d..00d074df8 100644
@@ -510,6 +510,7 @@ grub_crypto_hmac_buffer (const struct gcry_md_spec *md,
 extern gcry_md_spec_t _gcry_digest_spec_md5;
 extern gcry_md_spec_t _gcry_digest_spec_sha1;
 extern gcry_md_spec_t _gcry_digest_spec_sha256;
+extern gcry_md_spec_t _gcry_digest_spec_sha384;
 extern gcry_md_spec_t _gcry_digest_spec_sha512;
 extern gcry_md_spec_t _gcry_digest_spec_crc32;
 extern gcry_cipher_spec_t _gcry_cipher_spec_aes;
include/grub/efi/pks.h new file +112 -0

112 lines not on this page · raw message

include/grub/types.h +4 -0
index 45079bf65..b3ba762fc 100644
@@ -379,6 +379,8 @@ struct grub_guid
 } __attribute__ ((aligned(4)));
 typedef struct grub_guid grub_guid_t;
 
+#define GRUB_GUID_SIZE    (sizeof (grub_guid_t))
+
 struct grub_packed_guid
 {
   grub_uint32_t data1;
@@ -388,4 +390,6 @@ struct grub_packed_guid
 } GRUB_PACKED;
 typedef struct grub_packed_guid grub_packed_guid_t;
 
+#define GRUB_PACKED_GUID_SIZE    (sizeof (grub_packed_guid_t))
+
 #endif /* ! GRUB_TYPES_HEADER */
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#22 lore raw

Re: [PATCH v6 12/20] appended signatures: Create db and dbx lists

Quoted 18 lines · Sudhakar Kuppusamy

If secure boot is enabled with static key management mode, the trusted certificates will be extracted from the GRUB ELF Note and added to db list. This is introduced by a subsequent patch.

If secure boot is enabled with dynamic key management mode, the trusted certificates and certificate/binary hash will be extracted from the PKS and added to db list. The distrusted certificates, certificate/binary hash from the PKS and added to dbx list. Both dbx and db lists are introduced by a subsequent patch.

Note:-

If the certificate or the certificate hash exists in the dbx list, then do not add that certificate/certificate hash to the db list.

[...]

Quoted 11 lines
grub-core/commands/appendedsig/appendedsig.c +11 -0
+/* Add the certificate into the db/dbx list */
+static grub_err_t
+add_certificate (const grub_uint8_t *data, const grub_size_t data_size,
+                 struct grub_database *database, const bool is_db)
+{
+  struct x509_certificate *cert;
+  grub_err_t rc;
+  grub_size_t cert_entries = database->cert_entries;
+
+  if (data == NULL || data_size == 0)
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "certificate data or size is not available");

grub-core/commands/appendedsig/appendedsig.c is_cert_match (const struct x509_certificate *distrusted_cert, · line 314

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+
+  cert = grub_zalloc (sizeof (struct x509_certificate));
+  if (cert == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
+
+  rc = parse_x509_certificate (data, data_size, cert);
+  if (rc != GRUB_ERR_NONE)
+    {
+      grub_dprintf ("appendedsig", "skipped %s certificate (%d)\n",
+                    ((is_db == true) ? "trusted" : "distrusted"), (int) rc);

The rc value is meaningless for user because it may change from one GRUB version to another. Just add an error message which makes sense here...

Quoted 7 lines
grub-core/commands/appendedsig/appendedsig.c +7 -0
+      grub_free (cert);
+      return rc;
+    }
+
+  if (is_db == true)
+    {
+      rc = is_dbx_cert (cert);

grub-core/commands/appendedsig/appendedsig.c is_cert_match (const struct x509_certificate *distrusted_cert, · line 331

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+      if (rc != GRUB_ERR_NONE)
+        {
+          certificate_release (cert);
+          grub_free (cert);
+          return rc;
+        }
+    }
+
+  grub_dprintf ("appendedsig", "add a %s certificate CN='%s'\n",
+                ((is_db == true) ? "trusted" : "distrusted"), cert->subject);

grub_dprintf ("add a certificate CN='%s' to %s", ((is_db == true) ? "db" : "dbx"), cert->subject);

I think the error message above should be changed in similar way, e.g., "cannot add a certificate CN='%s' to %s due to an error"...

Quoted 30 lines
grub-core/commands/appendedsig/appendedsig.c +10 -2
+  cert_entries++;
+  cert->next = database->certs;
+  database->certs = cert;
+  database->cert_entries = cert_entries;
+
+  return rc;
+}
+
 static grub_err_t
 file_read_whole (grub_file_t file, grub_uint8_t **buf, grub_size_t *len)
 {
@@ -272,7 +472,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
   struct pkcs7_signerInfo *si;
   int i;
 
-  if (db == NULL)
+  if (!db.cert_entries)
     return grub_error (GRUB_ERR_BAD_SIGNATURE, "no trusted keys to verify against");
 
   err = extract_appended_signature (buf, bufsize, &sig);
@@ -303,7 +503,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
       grub_dprintf ("appendedsig", "data size %" PRIuGRUB_SIZE ", signer %d hash %02x%02x%02x%02x...\n",
                     datasize, i, hash[0], hash[1], hash[2], hash[3]);
 
-      for (pk = db; pk != NULL; pk = pk->next)
+      for (pk = db.certs; pk != NULL; pk = pk->next)
         {
           err = verify_signature (pk->mpis, si->sig_mpi, si->hash, hash);
           if (err == GRUB_ERR_NONE)

grub-core/commands/appendedsig/appendedsig.c is_cert_present_in_db (const struct x509_certificate *cert_in) · line 562

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
@@ -359,7 +559,7 @@ is_cert_present_in_db (const struct x509_certificate *cert_in)
 {
   struct x509_certificate *cert;
 
-  for (cert = db; cert; cert = cert->next)
+  for (cert = db.certs; cert; cert = cert->next)

...; cert != NULL;...??? If yes then it should be changed in patch which introduces the file_read_whole() function...

Quoted 31 lines
grub-core/commands/appendedsig/appendedsig.c +5 -4
     {
       if (is_cert_match (cert, cert_in) == true)
         return true;
@@ -374,12 +574,12 @@ is_cert_removed_from_db (const struct x509_certificate *cert)
   int i = 1;
   struct x509_certificate *curr_cert, *prev_cert;
 
-  for (curr_cert = prev_cert = db; curr_cert != NULL; curr_cert = curr_cert->next)
+  for (curr_cert = prev_cert = db.certs; curr_cert != NULL; curr_cert = curr_cert->next)
     {
       if (is_cert_match (curr_cert, cert) == true)
         {
           if (i == 1) /* Match with first certificate in the db list. */
-            db = curr_cert->next;
+            db.certs = curr_cert->next;
           else
             prev_cert->next = curr_cert->next;
 
@@ -468,8 +668,9 @@ grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **
 
   grub_dprintf ("appendedsig", "added certificate with CN: %s\n", cert->subject);
 
-  cert->next = db;
-  db = cert;
+  cert->next = db.certs;
+  db.certs = cert;
+  db.cert_entries++;
 
   return GRUB_ERR_NONE;
 }

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 721

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
@@ -517,7 +718,7 @@ grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib
   int cert_num = 1;
   grub_size_t i;
 
-  for (cert = db; cert != NULL; cert = cert->next)
+  for (cert = db.certs; cert != NULL; cert = cert->next)

... like here...

Quoted 20 lines
grub-core/commands/appendedsig/appendedsig.c +13 -0
     {
       grub_printf ("Certificate %d:\n", cert_num);
       grub_printf ("\tSerial: ");
@@ -609,6 +810,238 @@ static struct grub_fs pseudo_fs = {
 
 static grub_command_t cmd_verify, cmd_list_db, cmd_dbx_cert, cmd_db_cert;
 
+/* Check the certificate hash presence in the PKS dbx list. */
+static bool
+is_dbx_cert_hash (const grub_uint8_t *data, const grub_size_t data_size)
+{
+  grub_err_t rc;
+  grub_size_t i, cert_hash_size = 0;
+  grub_uint8_t cert_hash[GRUB_MAX_HASH_SIZE] = { 0 };
+
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (grub_pks_keystore.dbx[i].data == NULL ||
+          grub_pks_keystore.dbx[i].data_size == 0)
+        continue;

grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 835

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+
+      rc = get_hash (&grub_pks_keystore.dbx[i].guid, data, data_size,
+                     cert_hash, &cert_hash_size);
+      if (rc != GRUB_ERR_NONE)
+        continue;
+
+      if (cert_hash_size == grub_pks_keystore.dbx[i].data_size &&
+          grub_memcmp (grub_pks_keystore.dbx[i].data, cert_hash, cert_hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "a trusted certificate (%02x%02x%02x%02x) is ignored "

s/trusted//

Quoted 15 lines
grub-core/commands/appendedsig/appendedsig.c +15 -0
+                        "because this certificate hash is on the dbx list\n",
+                        cert_hash[0], cert_hash[1], cert_hash[2], cert_hash[3]);
+          return true;
+        }
+    }
+
+  return false;
+}
+
+/* Check the binary hash presence in the PKS dbx list. */
+static bool
+is_dbx_binary_hash (const grub_uint8_t *binary_hash, const grub_size_t binary_hash_size)
+{
+  grub_size_t i;
+

grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 860

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (grub_pks_keystore.dbx[i].data == NULL ||
+          grub_pks_keystore.dbx[i].data_size == 0)
+        continue;
+
+      if (binary_hash_size == grub_pks_keystore.dbx[i].data_size &&
+          grub_memcmp (grub_pks_keystore.dbx[i].data, binary_hash, binary_hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "a trusted binary hash (%02x%02x%02x%02x) is ignored"

s/trusted//

Quoted 8 lines
grub-core/commands/appendedsig/appendedsig.c +8 -0
+                        " because it is on the dbx list\n", binary_hash[0], binary_hash[1],
+                        binary_hash[2], binary_hash[3]);
+          return true;
+        }
+    }
+
+  return false;
+}

[...]

Quoted 65 lines

GRUB_MOD_INIT (appendedsig)

 {
   int rc;
grub-core/commands/appendedsig/appendedsig.c +18 -30
@@ -621,7 +1054,6 @@ GRUB_MOD_INIT (appendedsig)
   if (grub_ieee1275_is_secure_boot () == GRUB_SB_ENFORCED)
     check_sigs = true;
 
-  db = NULL;
   grub_register_variable_hook ("check_appended_signatures", grub_env_read_sec, grub_env_write_sec);
   grub_env_export ("check_appended_signatures");
 
@@ -630,38 +1062,54 @@ GRUB_MOD_INIT (appendedsig)
     grub_fatal ("error initing ASN.1 data structures: %d: %s\n", rc, asn1_strerror (rc));
 
   /*
-   * If signature verification is enabled,
-   * extract trusted keys from ELF Note and store them in the db.
+   * If signature verification is enabled with static key management mode,
+   * extract trusted keys from ELF Note and store them in the db list.
    */
-  if (check_sigs == true)
+  if (grub_pks_use_keystore == false && check_sigs == true)
     {
       FOR_MODULES (header)
-      {
-        struct grub_file pseudo_file;
-        struct x509_certificate *pk = NULL;
-        grub_err_t err;
-
-        /* Not an X.509 certificate, skip. */
-        if (header->type != OBJ_TYPE_X509_PUBKEY)
-          continue;
-
-        grub_memset (&pseudo_file, 0, sizeof (pseudo_file));
-        pseudo_file.fs = &pseudo_fs;
-        pseudo_file.size = header->size - sizeof (struct grub_module_header);
-        pseudo_file.data = (char *) header + sizeof (struct grub_module_header);
-
-        grub_dprintf ("appendedsig", "found an x509 key, size=%" PRIuGRUB_UINT64_T "\n",
-                      pseudo_file.size);
-
-        err = read_cert_from_file (&pseudo_file, &pk);
-        if (err != GRUB_ERR_NONE)
-          grub_fatal ("error loading initial key: %s", grub_errmsg);
-
-        grub_dprintf ("appendedsig", "loaded certificate CN='%s'\n", pk->subject);
-
-        pk->next = db;
-        db = pk;
-      }
+        {
+          /* Not an ELF module, skip.  */
+          if (header->type != OBJ_TYPE_X509_PUBKEY)
+            continue;
+          rc = build_static_db_list (header);
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              grub_error (rc, "static db list creation failed");
+            }
+          else
+            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " static keys\n",
+                          db.cert_entries);
+        }
+    }

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1095

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+  /*
+   * If signature verification is enabled with dynamic key management mode,
+   * extract trusted and distrusted keys from PKS and store them in the db and dbx list.
+   */
+  else if (grub_pks_use_keystore == true && check_sigs == true)
+    {
+      rc = create_db_list ();
+      if (rc != GRUB_ERR_NONE)
+        {
+          free_db_list ();

I would not call free_db_list() here because even partially populated list can be useful. Though we should be sure partial or even empty list does not lead to crashes.

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1100

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +5 -0
+          grub_error (rc, "db list creation failed");
+        }
+      else
+        {
+          rc = create_dbx_list ();

The dbx should be populated regardless of create_db_list() failure.

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1104

Quoted 4 lines
grub-core/commands/appendedsig/appendedsig.c +4 -0
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              free_dbx_list ();

Again, do not free lists in case of errors.

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1115

Quoted 11 lines
grub-core/commands/appendedsig/appendedsig.c +8 -0
+              grub_error (rc, "dbx list creation failed");
+            }
+          else
+            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " keys\n"
+                          "the dbx list now has %" PRIuGRUB_SIZE " keys\n",
+                          db.signature_entries + db.cert_entries, dbx.signature_entries);
+        }
+      grub_pks_free_keystore ();
     }
 
   cmd_db_cert = grub_register_command ("append_add_db_cert", grub_cmd_db_cert, N_("X509_CERTIFICATE"),

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#23 lore raw

Re: [PATCH v6 12/20] appended signatures: Create db and dbx lists

Thank you Daniel.

Quoted 40 lines · Daniel Kiper
Quoted 18 lines · Sudhakar Kuppusamy

If secure boot is enabled with static key management mode, the trusted certificates will be extracted from the GRUB ELF Note and added to db list. This is introduced by a subsequent patch.

If secure boot is enabled with dynamic key management mode, the trusted certificates and certificate/binary hash will be extracted from the PKS and added to db list. The distrusted certificates, certificate/binary hash from the PKS and added to dbx list. Both dbx and db lists are introduced by a subsequent patch.

Note:-

If the certificate or the certificate hash exists in the dbx list, then do not add that certificate/certificate hash to the db list.

[...]

Quoted 16 lines
grub-core/commands/appendedsig/appendedsig.c +16 -0
+/* Add the certificate into the db/dbx list */
+static grub_err_t
+add_certificate (const grub_uint8_t *data, const grub_size_t data_size,
+                 struct grub_database *database, const bool is_db)
+{
+  struct x509_certificate *cert;
+  grub_err_t rc;
+  grub_size_t cert_entries = database->cert_entries;
+
+  if (data == NULL || data_size == 0)
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, "certificate data or size is not available");
+
+  cert = grub_zalloc (sizeof (struct x509_certificate));
+  if (cert == NULL)
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of memory");
+
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c is_cert_match (const struct x509_certificate *distrusted_cert, · line 314

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +5 -0
+  rc = parse_x509_certificate (data, data_size, cert);
+  if (rc != GRUB_ERR_NONE)
+    {
+      grub_dprintf ("appendedsig", "skipped %s certificate (%d)\n",
+                    ((is_db == true) ? "trusted" : "distrusted"), (int) rc);

The rc value is meaningless for user because it may change from one GRUB version to another. Just add an error message which makes sense here...

Sure. Will do it.

Quoted 21 lines
Quoted 8 lines
grub-core/commands/appendedsig/appendedsig.c +7 -0
+      grub_free (cert);
+      return rc;
+    }
+
+  if (is_db == true)
+    {
+      rc = is_dbx_cert (cert);

grub-core/commands/appendedsig/appendedsig.c is_cert_match (const struct x509_certificate *distrusted_cert, · line 331

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+      if (rc != GRUB_ERR_NONE)
+        {
+          certificate_release (cert);
+          grub_free (cert);
+          return rc;
+        }
+    }
+
+  grub_dprintf ("appendedsig", "add a %s certificate CN='%s'\n",
+                ((is_db == true) ? "trusted" : "distrusted"), cert->subject);

grub_dprintf ("add a certificate CN='%s' to %s", ((is_db == true) ? "db" : "dbx"), cert->subject);

Quoted 2 lines

I think the error message above should be changed in similar way, e.g., "cannot add a certificate CN='%s' to %s due to an error"...

Sure. Will do it.

Quoted 34 lines
Quoted 34 lines
grub-core/commands/appendedsig/appendedsig.c +8 -0
+  cert_entries++;
+  cert->next = database->certs;
+  database->certs = cert;
+  database->cert_entries = cert_entries;
+
+  return rc;
+}
+

static grub_err_t
file_read_whole (grub_file_t file, grub_uint8_t **buf, grub_size_t *len) {

grub-core/commands/appendedsig/appendedsig.c +2 -2
@@ -272,7 +472,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
  struct pkcs7_signerInfo *si;
  int i;
 
-  if (db == NULL)
+  if (!db.cert_entries)
    return grub_error (GRUB_ERR_BAD_SIGNATURE, "no trusted keys to verify against");
 
  err = extract_appended_signature (buf, bufsize, &sig);
@@ -303,7 +503,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
      grub_dprintf ("appendedsig", "data size %" PRIuGRUB_SIZE ", signer %d hash %02x%02x%02x%02x...\n",
                    datasize, i, hash[0], hash[1], hash[2], hash[3]);
 
-      for (pk = db; pk != NULL; pk = pk->next)
+      for (pk = db.certs; pk != NULL; pk = pk->next)
        {
          err = verify_signature (pk->mpis, si->sig_mpi, si->hash, hash);
          if (err == GRUB_ERR_NONE)
@@ -359,7 +559,7 @@ is_cert_present_in_db (const struct x509_certificate *cert_in)
 {
  struct x509_certificate *cert;
 
Quoted 5 lines

grub-core/commands/appendedsig/appendedsig.c is_cert_present_in_db (const struct x509_certificate *cert_in) · line 562

Quoted 2 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
-  for (cert = db; cert; cert = cert->next)
+  for (cert = db.certs; cert; cert = cert->next)

...; cert != NULL;...??? If yes then it should be changed in patch which introduces the file_read_whole() function...

Sure. Will do it.

Quoted 35 lines
Quoted 35 lines
grub-core/commands/appendedsig/appendedsig.c +5 -4
    {
      if (is_cert_match (cert, cert_in) == true)
        return true;
@@ -374,12 +574,12 @@ is_cert_removed_from_db (const struct x509_certificate *cert)
  int i = 1;
  struct x509_certificate *curr_cert, *prev_cert;
 
-  for (curr_cert = prev_cert = db; curr_cert != NULL; curr_cert = curr_cert->next)
+  for (curr_cert = prev_cert = db.certs; curr_cert != NULL; curr_cert = curr_cert->next)
    {
      if (is_cert_match (curr_cert, cert) == true)
        {
          if (i == 1) /* Match with first certificate in the db list. */
-            db = curr_cert->next;
+            db.certs = curr_cert->next;
          else
            prev_cert->next = curr_cert->next;
 
@@ -468,8 +668,9 @@ grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **
 
  grub_dprintf ("appendedsig", "added certificate with CN: %s\n", cert->subject);
 
-  cert->next = db;
-  db = cert;
+  cert->next = db.certs;
+  db.certs = cert;
+  db.cert_entries++;
 
  return GRUB_ERR_NONE;
 }
@@ -517,7 +718,7 @@ grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib
  int cert_num = 1;
  grub_size_t i;
 
Quoted 4 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 721

Quoted 2 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
-  for (cert = db; cert != NULL; cert = cert->next)
+  for (cert = db.certs; cert != NULL; cert = cert->next)

... like here…

Will do it.

Quoted 25 lines
Quoted 25 lines
grub-core/commands/appendedsig/appendedsig.c +17 -0
    {
      grub_printf ("Certificate %d:\n", cert_num);
      grub_printf ("\tSerial: ");
@@ -609,6 +810,238 @@ static struct grub_fs pseudo_fs = {
 
 static grub_command_t cmd_verify, cmd_list_db, cmd_dbx_cert, cmd_db_cert;
 
+/* Check the certificate hash presence in the PKS dbx list. */
+static bool
+is_dbx_cert_hash (const grub_uint8_t *data, const grub_size_t data_size)
+{
+  grub_err_t rc;
+  grub_size_t i, cert_hash_size = 0;
+  grub_uint8_t cert_hash[GRUB_MAX_HASH_SIZE] = { 0 };
+
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (grub_pks_keystore.dbx[i].data == NULL ||
+          grub_pks_keystore.dbx[i].data_size == 0)
+        continue;
+
+      rc = get_hash (&grub_pks_keystore.dbx[i].guid, data, data_size,
+                     cert_hash, &cert_hash_size);
+      if (rc != GRUB_ERR_NONE)
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 835

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+        continue;
+
+      if (cert_hash_size == grub_pks_keystore.dbx[i].data_size &&
+          grub_memcmp (grub_pks_keystore.dbx[i].data, cert_hash, cert_hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "a trusted certificate (%02x%02x%02x%02x) is ignored "

s/trusted//

Will do it.

Quoted 20 lines
Quoted 20 lines
grub-core/commands/appendedsig/appendedsig.c +19 -0
+                        "because this certificate hash is on the dbx list\n",
+                        cert_hash[0], cert_hash[1], cert_hash[2], cert_hash[3]);
+          return true;
+        }
+    }
+
+  return false;
+}
+
+/* Check the binary hash presence in the PKS dbx list. */
+static bool
+is_dbx_binary_hash (const grub_uint8_t *binary_hash, const grub_size_t binary_hash_size)
+{
+  grub_size_t i;
+
+  for (i = 0; i < grub_pks_keystore.dbx_entries; i++)
+    {
+      if (grub_pks_keystore.dbx[i].data == NULL ||
+          grub_pks_keystore.dbx[i].data_size == 0)
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 860

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+        continue;
+
+      if (binary_hash_size == grub_pks_keystore.dbx[i].data_size &&
+          grub_memcmp (grub_pks_keystore.dbx[i].data, binary_hash, binary_hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "a trusted binary hash (%02x%02x%02x%02x) is ignored"

s/trusted//

Will do it.

Quoted 83 lines
Quoted 9 lines
grub-core/commands/appendedsig/appendedsig.c +8 -0
+                        " because it is on the dbx list\n", binary_hash[0], binary_hash[1],
+                        binary_hash[2], binary_hash[3]);
+          return true;
+        }
+    }
+
+  return false;
+}

[...]

Quoted 71 lines

GRUB_MOD_INIT (appendedsig)
{
int rc;

grub-core/commands/appendedsig/appendedsig.c +24 -30
@@ -621,7 +1054,6 @@ GRUB_MOD_INIT (appendedsig)
  if (grub_ieee1275_is_secure_boot () == GRUB_SB_ENFORCED)
    check_sigs = true;
 
-  db = NULL;
  grub_register_variable_hook ("check_appended_signatures", grub_env_read_sec, grub_env_write_sec);
  grub_env_export ("check_appended_signatures");
 
@@ -630,38 +1062,54 @@ GRUB_MOD_INIT (appendedsig)
    grub_fatal ("error initing ASN.1 data structures: %d: %s\n", rc, asn1_strerror (rc));
 
  /*
-   * If signature verification is enabled,
-   * extract trusted keys from ELF Note and store them in the db.
+   * If signature verification is enabled with static key management mode,
+   * extract trusted keys from ELF Note and store them in the db list.
   */
-  if (check_sigs == true)
+  if (grub_pks_use_keystore == false && check_sigs == true)
    {
      FOR_MODULES (header)
-      {
-        struct grub_file pseudo_file;
-        struct x509_certificate *pk = NULL;
-        grub_err_t err;
-
-        /* Not an X.509 certificate, skip. */
-        if (header->type != OBJ_TYPE_X509_PUBKEY)
-          continue;
-
-        grub_memset (&pseudo_file, 0, sizeof (pseudo_file));
-        pseudo_file.fs = &pseudo_fs;
-        pseudo_file.size = header->size - sizeof (struct grub_module_header);
-        pseudo_file.data = (char *) header + sizeof (struct grub_module_header);
-
-        grub_dprintf ("appendedsig", "found an x509 key, size=%" PRIuGRUB_UINT64_T "\n",
-                      pseudo_file.size);
-
-        err = read_cert_from_file (&pseudo_file, &pk);
-        if (err != GRUB_ERR_NONE)
-          grub_fatal ("error loading initial key: %s", grub_errmsg);
-
-        grub_dprintf ("appendedsig", "loaded certificate CN='%s'\n", pk->subject);
-
-        pk->next = db;
-        db = pk;
-      }
+        {
+          /* Not an ELF module, skip.  */
+          if (header->type != OBJ_TYPE_X509_PUBKEY)
+            continue;
+          rc = build_static_db_list (header);
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              grub_error (rc, "static db list creation failed");
+            }
+          else
+            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " static keys\n",
+                          db.cert_entries);
+        }
+    }
+  /*
+   * If signature verification is enabled with dynamic key management mode,
+   * extract trusted and distrusted keys from PKS and store them in the db and dbx list.
+   */
+  else if (grub_pks_use_keystore == true && check_sigs == true)
+    {
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1095

Quoted 4 lines
grub-core/commands/appendedsig/appendedsig.c +4 -0
+      rc = create_db_list ();
+      if (rc != GRUB_ERR_NONE)
+        {
+          free_db_list ();

I would not call free_db_list() here because even partially populated list can be useful. Though we should be sure partial or even empty list does not lead to crashes.

Sure. Do I need to use partial or empty list even out of memory case?

Quoted 26 lines

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1100

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +5 -0
+          grub_error (rc, "db list creation failed");
+        }
+      else
+        {
+          rc = create_dbx_list ();

The dbx should be populated regardless of create_db_list() failure.

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1104

Quoted 4 lines
grub-core/commands/appendedsig/appendedsig.c +4 -0
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              free_dbx_list ();

Again, do not free lists in case of errors.

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +8 -0
+              grub_error (rc, "dbx list creation failed");
+            }
+          else
+            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " keys\n"
+                          "the dbx list now has %" PRIuGRUB_SIZE " keys\n",
+                          db.signature_entries + db.cert_entries, dbx.signature_entries);
+        }
+      grub_pks_free_keystore ();
    }
 
Quoted 3 lines

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1115

Quoted 1 line
grub-core/commands/appendedsig/appendedsig.c +0 -0
  cmd_db_cert = grub_register_command ("append_add_db_cert", grub_cmd_db_cert, N_("X509_CERTIFICATE"),

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#24 lore raw

Re: [PATCH v6 12/20] appended signatures: Create db and dbx lists

Quoted 4 lines · Sudhakar Kuppusamy

Thank you Daniel.

Quoted 2 lines · Daniel Kiper

On Tue, Jul 29, 2025 at 08:21:48PM +0530, Sudhakar Kuppusamy wrote:

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+  else if (grub_pks_use_keystore == true && check_sigs == true)
+    {
+      rc = create_db_list ();
+      if (rc != GRUB_ERR_NONE)
+        {
+          free_db_list ();
Quoted 5 lines
Quoted 3 lines

I would not call free_db_list() here because even partially populated list can be useful. Though we should be sure partial or even empty list does not lead to crashes.

Sure. Do I need to use partial or empty list even out of memory case?

I would not try to differentiate failures here. Releasing a few kB of memory will not help much in case of out of memory errors.

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#25 lore raw

PATCH v6 13/20 appended signatures: Using db and dbx lists for signature verification

In reply to Sudhakar Kuppusamy

Signature verification: verify the kernel against lists of hashes that are either in dbx or db list. If it is not in the dbx list then the trusted keys from the db list are used to verify the signature.

---
 grub-core/commands/appendedsig/appendedsig.c | 84 +++++++++++++++++++-
 1 file changed, 82 insertions(+), 2 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +82 -2
index 1e5610ddf..f696ef476 100644
@@ -460,6 +460,76 @@ verify_signature (const gcry_mpi_t *pkmpi, const gcry_mpi_t hmpi,
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+get_binary_hash (const grub_size_t binary_hash_size, const grub_uint8_t *data,
+                 const grub_size_t data_size, grub_uint8_t *hash, grub_size_t *hash_size)
+{
+  grub_packed_guid_t guid = { 0 };
+
+  /* support SHA256, SHA384 and SHA512 for binary hash */
+  if (binary_hash_size == 32)
+    grub_memcpy (&guid, &GRUB_PKS_CERT_SHA256_GUID, GRUB_PACKED_GUID_SIZE);
+  else if (binary_hash_size == 48)
+    grub_memcpy (&guid, &GRUB_PKS_CERT_SHA384_GUID, GRUB_PACKED_GUID_SIZE);
+  else if (binary_hash_size == 64)
+    grub_memcpy (&guid, &GRUB_PKS_CERT_SHA512_GUID, GRUB_PACKED_GUID_SIZE);
+  else
+    {
+      grub_dprintf ("appendedsig", "unsupported hash type (%" PRIuGRUB_SIZE ") and "
+                    "skipped\n", binary_hash_size);
+      return GRUB_ERR_UNKNOWN_COMMAND;
+    }
+
+  return get_hash (&guid, data, data_size, hash, hash_size);
+}
+
+/*
+ * Verify binary hash against the db and dbx list.
+ * The following errors can occur:
+ *  - GRUB_ERR_BAD_SIGNATURE: indicates that the hash is in dbx list.
+ *  - GRUB_ERR_EOF: the hash could not be found in the db and dbx list.
+ *  - GRUB_ERR_NONE: the hash is found in db list.
+ */
+static grub_err_t
+verify_binary_hash (const grub_uint8_t *data, const grub_size_t data_size)
+{
+  grub_err_t rc = GRUB_ERR_NONE;
+  grub_size_t i = 0, hash_size = 0;
+  grub_uint8_t hash[GRUB_MAX_HASH_SIZE] = { 0 };
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    {
+      rc = get_binary_hash (dbx.signature_size[i], data, data_size, hash, &hash_size);
+      if (rc != GRUB_ERR_NONE)
+        continue;
+
+      if (hash_size == dbx.signature_size[i] &&
+          grub_memcmp (dbx.signatures[i], hash, hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "the hash (%02x%02x%02x%02x) is present in the dbx list\n",
+                        hash[0], hash[1], hash[2], hash[3]);
+          return GRUB_ERR_BAD_SIGNATURE;
+        }
+    }
+
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      rc = get_binary_hash (db.signature_size[i], data, data_size, hash, &hash_size);
+      if (rc != GRUB_ERR_NONE)
+        continue;
+
+      if (hash_size == db.signature_size[i] &&
+          grub_memcmp (db.signatures[i], hash, hash_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "verified with a trusted hash (%02x%02x%02x%02x)\n",
+                        hash[0], hash[1], hash[2], hash[3]);
+          return GRUB_ERR_NONE;
+        }
+    }
+
+  return GRUB_ERR_EOF;
+}
+
 static grub_err_t
 grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
 {
@@ -472,8 +542,8 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
   struct pkcs7_signerInfo *si;
   int i;
 
-  if (!db.cert_entries)
-    return grub_error (GRUB_ERR_BAD_SIGNATURE, "no trusted keys to verify against");
+  if (!db.cert_entries && !db.signature_entries)
+    return grub_error (GRUB_ERR_BAD_SIGNATURE, "no trusted keys/hashes to verify against");
 
   err = extract_appended_signature (buf, bufsize, &sig);
   if (err != GRUB_ERR_NONE)
@@ -482,6 +552,16 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)
   append_sig_len = sig.signature_len;
   datasize = bufsize - sig.signature_len;
 
+  /* Verify binary hash against the db and dbx list. */
+  err = verify_binary_hash (buf, datasize);
+  if (err == GRUB_ERR_BAD_SIGNATURE)
+    {
+      pkcs7_signedData_release (&sig.pkcs7);
+      return grub_error (err,
+                         "failed to verify the binary hash against a trusted binary hash\n");
+    }
+
+  /* Verify signature using trusted keys from db list. */
   for (i = 0; i < sig.pkcs7.signerInfo_count; i++)
     {
       /*
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#26 lore raw

PATCH v6 14/20 powerpc_ieee1275: Introduce use_static_keys flag

In reply to Sudhakar Kuppusamy

Introduce the use_static_keys flag to indicate that static keys are to be used rather than keys from the PKS storage's db variable. This flag is set when Secure Boot is enabled with PKS but the db variable is not present in the PKS storage. The appendedsig module would use this flag to extract the static keys from the GRUB ELF Note and stored in the db list.

---
 grub-core/kern/powerpc/ieee1275/platform_keystore.c | 12 +++++++++++-
 include/grub/powerpc/ieee1275/platform_keystore.h   |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)
grub-core/kern/powerpc/ieee1275/platform_keystore.c +11 -1
index e3ffae16f..d09257e1f 100644
@@ -44,7 +44,8 @@ static grub_size_t pks_max_object_size;
 bool grub_pks_use_keystore = false;
 
 /* Platform Keystore. */
-grub_pks_t grub_pks_keystore = { .db = NULL, .dbx = NULL, .db_entries = 0, .dbx_entries = 0 };
+grub_pks_t grub_pks_keystore = { .db = NULL, .dbx = NULL, .db_entries = 0, .dbx_entries = 0,
+                                 .use_static_keys = false };
 
 /*
  * Import the Globally Unique Identifier (GUID), EFI Signature Database (ESD),
@@ -313,6 +314,15 @@ grub_pks_keystore_init (void)
       grub_memset (&grub_pks_keystore, 0, sizeof (grub_pks_t));
       /* Read db from PKS. */
       rc = read_secure_boot_variables (0, DB, &grub_pks_keystore.db, &grub_pks_keystore.db_entries);
+      if (rc == GRUB_ERR_UNKNOWN_COMMAND)
+        {
+          rc = GRUB_ERR_NONE;
+          /*
+           * The db variable won't be available by default in PKS.
+           * So, it will use the static key as a default key from the GRUB ELF Note.
+           */
+          grub_pks_keystore.use_static_keys = true;
+        }
       if (rc == GRUB_ERR_NONE)
         {
           /* Read dbx from PKS. */
include/grub/powerpc/ieee1275/platform_keystore.h +1 -0
index 557740c70..2b56ccc4a 100644
@@ -97,6 +97,7 @@ struct grub_pks
   grub_pks_sd_t *dbx;       /* Forbidden signature database. */
   grub_size_t db_entries;   /* Size of signature database. */
   grub_size_t dbx_entries;  /* Size of forbidden signature database. */
+  bool use_static_keys;     /* Flag to indicate use of static keys. */
 } GRUB_PACKED;
 typedef struct grub_pks grub_pks_t;
 
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#27 lore raw

PATCH v6 15/20 appended signatures: Read default db keys from the ELF Note

In reply to Sudhakar Kuppusamy

If Secure Boot is enabled with dynamic key management mode and the use_static_keys flag is set, then read the static keys as a db default keys from the ELF Note and add stored in the db list.

---
 grub-core/commands/appendedsig/appendedsig.c | 57 ++++++++++++++------
 1 file changed, 42 insertions(+), 15 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +42 -15
index f696ef476..fa908b963 100644
@@ -1051,7 +1051,7 @@ create_dbx_list (void)
  * parse it, and add it to the db list.
  */
 static grub_err_t
-build_static_db_list (const struct grub_module_header *header)
+build_static_db_list (const struct grub_module_header *header, const bool is_pks)
 {
   grub_err_t err;
   struct grub_file pseudo_file;
@@ -1070,6 +1070,12 @@ build_static_db_list (const struct grub_module_header *header)
   if (err != GRUB_ERR_NONE)
     return err;
 
+  if (is_pks == true)
+    {
+      if (is_dbx_cert_hash (cert_data, cert_data_size) == true)
+        return GRUB_ERR_ACCESS_DENIED;
+    }
+
   err = add_certificate (cert_data, cert_data_size, &db, true);
   grub_free (cert_data);
 
@@ -1122,6 +1128,25 @@ free_dbx_list (void)
   grub_memset (&dbx, 0, sizeof (dbx));
 }
 
+static grub_err_t
+load_static_keys (const struct grub_module_header *header, const bool is_pks)
+{
+  int rc = GRUB_ERR_NONE;
+
+  FOR_MODULES (header)
+    {
+      /* Not an ELF module, skip.  */
+      if (header->type != OBJ_TYPE_X509_PUBKEY)
+        continue;
+
+      rc = build_static_db_list (header, is_pks);
+      if (rc != GRUB_ERR_NONE)
+        return rc;
+    }
+
+  return rc;
+}
+
 GRUB_MOD_INIT (appendedsig)
 {
   int rc;
@@ -1147,21 +1172,15 @@ GRUB_MOD_INIT (appendedsig)
    */
   if (grub_pks_use_keystore == false && check_sigs == true)
     {
-      FOR_MODULES (header)
+      rc = load_static_keys (header, false);
+      if (rc != GRUB_ERR_NONE)
         {
-          /* Not an ELF module, skip.  */
-          if (header->type != OBJ_TYPE_X509_PUBKEY)
-            continue;
-          rc = build_static_db_list (header);
-          if (rc != GRUB_ERR_NONE)
-            {
-              free_db_list ();
-              grub_error (rc, "static db list creation failed");
-            }
-          else
-            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " static keys\n",
-                          db.cert_entries);
+          free_db_list ();
+          grub_error (rc, "static db list creation failed");
         }
+      else
+        grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " static keys\n",
+                      db.cert_entries);
     }
   /*
    * If signature verification is enabled with dynamic key management mode,
@@ -1169,7 +1188,15 @@ GRUB_MOD_INIT (appendedsig)
    */
   else if (grub_pks_use_keystore == true && check_sigs == true)
     {
-      rc = create_db_list ();
+      if (grub_pks_keystore.use_static_keys == true)
+        {
+          grub_dprintf ("appendedsig", "db variable is not available at PKS and "
+                        "using a static keys as a default key in db list\n");
+          rc = load_static_keys (header, grub_pks_keystore.use_static_keys);
+        }
+      else
+        rc = create_db_list ();
+
       if (rc != GRUB_ERR_NONE)
         {
           free_db_list ();
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#28 lore raw

Re: [PATCH v6 15/20] appended signatures: Read default db keys from the ELF Note

Quoted 3 lines · Sudhakar Kuppusamy

If Secure Boot is enabled with dynamic key management mode and the use_static_keys flag is set, then read the static keys as a db default keys from the ELF Note and add stored in the db list.

s/stored in the db list/them into the db/

Quoted 35 lines
---
 grub-core/commands/appendedsig/appendedsig.c | 57 ++++++++++++++------
 1 file changed, 42 insertions(+), 15 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +7 -1
index f696ef476..fa908b963 100644
@@ -1051,7 +1051,7 @@ create_dbx_list (void)
  * parse it, and add it to the db list.
  */
 static grub_err_t
-build_static_db_list (const struct grub_module_header *header)
+build_static_db_list (const struct grub_module_header *header, const bool is_pks)
 {
   grub_err_t err;
   struct grub_file pseudo_file;
@@ -1070,6 +1070,12 @@ build_static_db_list (const struct grub_module_header *header)
   if (err != GRUB_ERR_NONE)
     return err;
 
+  if (is_pks == true)
+    {
+      if (is_dbx_cert_hash (cert_data, cert_data_size) == true)
+        return GRUB_ERR_ACCESS_DENIED;
+    }
+
   err = add_certificate (cert_data, cert_data_size, &db, true);
   grub_free (cert_data);
 
@@ -1122,6 +1128,25 @@ free_dbx_list (void)
   grub_memset (&dbx, 0, sizeof (dbx));

grub-core/commands/appendedsig/appendedsig.c free_dbx_list (void) · line 1138

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +8 -0
 }
 
+static grub_err_t
+load_static_keys (const struct grub_module_header *header, const bool is_pks)
+{
+  int rc = GRUB_ERR_NONE;
+
+  FOR_MODULES (header)
+    {
+      /* Not an ELF module, skip.  */

Comment says different thing than the "if" below does...

Quoted 25 lines
grub-core/commands/appendedsig/appendedsig.c +13 -4
+      if (header->type != OBJ_TYPE_X509_PUBKEY)
+        continue;
+
+      rc = build_static_db_list (header, is_pks);
+      if (rc != GRUB_ERR_NONE)
+        return rc;
+    }
+
+  return rc;
+}
+
 GRUB_MOD_INIT (appendedsig)
 {
   int rc;
@@ -1147,21 +1172,15 @@ GRUB_MOD_INIT (appendedsig)
    */
   if (grub_pks_use_keystore == false && check_sigs == true)
     {
-      FOR_MODULES (header)
+      rc = load_static_keys (header, false);
+      if (rc != GRUB_ERR_NONE)
         {
-          /* Not an ELF module, skip.  */
-          if (header->type != OBJ_TYPE_X509_PUBKEY)
-            continue;

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1178

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +1 -9
-          rc = build_static_db_list (header);
-          if (rc != GRUB_ERR_NONE)
-            {
-              free_db_list ();
-              grub_error (rc, "static db list creation failed");
-            }
-          else
-            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " static keys\n",
-                          db.cert_entries);
+          free_db_list ();

Again, you should not free partial db lists. I would just print a warning in case of error...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#29 lore raw

Re: [PATCH v6 15/20] appended signatures: Read default db keys from the ELF Note

Quoted 10 lines · Daniel Kiper
Quoted 3 lines · Sudhakar Kuppusamy

If Secure Boot is enabled with dynamic key management mode and the use_static_keys flag is set, then read the static keys as a db default keys from the ELF Note and add stored in the db list.

s/stored in the db list/them into the db/

Sure, will do.

Quoted 38 lines
Quoted 38 lines

---
grub-core/commands/appendedsig/appendedsig.c | 57 ++++++++++++++------

1 file changed, 42 insertions(+), 15 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +0 -0
index f696ef476..fa908b963 100644
@@ -1051,7 +1051,7 @@ create_dbx_list (void)
 * parse it, and add it to the db list.
 */

static grub_err_t

grub-core/commands/appendedsig/appendedsig.c +1 -1
-build_static_db_list (const struct grub_module_header *header)
+build_static_db_list (const struct grub_module_header *header, const bool is_pks)

{
grub_err_t err;
struct grub_file pseudo_file;

grub-core/commands/appendedsig/appendedsig.c +6 -0
@@ -1070,6 +1070,12 @@ build_static_db_list (const struct grub_module_header *header)
  if (err != GRUB_ERR_NONE)
    return err;
 
+  if (is_pks == true)
+    {
+      if (is_dbx_cert_hash (cert_data, cert_data_size) == true)
+        return GRUB_ERR_ACCESS_DENIED;
+    }
+
  err = add_certificate (cert_data, cert_data_size, &db, true);
  grub_free (cert_data);
 
@@ -1122,6 +1128,25 @@ free_dbx_list (void)
  grub_memset (&dbx, 0, sizeof (dbx));
 }
 
Quoted 10 lines

grub-core/commands/appendedsig/appendedsig.c free_dbx_list (void) · line 1138

Quoted 8 lines
grub-core/commands/appendedsig/appendedsig.c +8 -0
+static grub_err_t
+load_static_keys (const struct grub_module_header *header, const bool is_pks)
+{
+  int rc = GRUB_ERR_NONE;
+
+  FOR_MODULES (header)
+    {
+      /* Not an ELF module, skip.  */

Comment says different thing than the "if" below does...

Sure. Will correct it.

Quoted 31 lines
Quoted 31 lines
grub-core/commands/appendedsig/appendedsig.c +13 -9
+      if (header->type != OBJ_TYPE_X509_PUBKEY)
+        continue;
+
+      rc = build_static_db_list (header, is_pks);
+      if (rc != GRUB_ERR_NONE)
+        return rc;
+    }
+
+  return rc;
+}
+
 GRUB_MOD_INIT (appendedsig)
 {
  int rc;
@@ -1147,21 +1172,15 @@ GRUB_MOD_INIT (appendedsig)
   */
  if (grub_pks_use_keystore == false && check_sigs == true)
    {
-      FOR_MODULES (header)
+      rc = load_static_keys (header, false);
+      if (rc != GRUB_ERR_NONE)
        {
-          /* Not an ELF module, skip.  */
-          if (header->type != OBJ_TYPE_X509_PUBKEY)
-            continue;
-          rc = build_static_db_list (header);
-          if (rc != GRUB_ERR_NONE)
-            {
-              free_db_list ();
-              grub_error (rc, "static db list creation failed");
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c GRUB_MOD_INIT (appendedsig) · line 1178

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +1 -4
-            }
-          else
-            grub_dprintf ("appendedsig", "the db list now has %" PRIuGRUB_SIZE " static keys\n",
-                          db.cert_entries);
+          free_db_list ();

Again, you should not free partial db lists. I would just print a warning in case of error…

Sure, will do it.

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#30 lore raw

PATCH v6 16/20 appended signatures: Introduce GRUB commands to access db and dbx

In reply to Sudhakar Kuppusamy

Introducing the following db and dbx commands

1. append_list_db:
Show the list of trusted certificates and binary hashes from the db list.
2. append_list_dbx:
Show the list of distrusted certificates and binary/certificate hashes from the dbx list.
3. append_add_db_cert:
Add the trusted certificate to the db list.
4. append_add_db_hash:
Add the trusted binary hash to the db list.
5. append_add_dbx_cert:
Add the distrusted certificate to the dbx list.
6. append_add_dbx_hash:
Add the distrusted certificate/binary hash to the dbx list.

Note that if signature verification (check_appended_signature) is set to enforce,
1. When append_add_db_cert or append_add_dbx_cert executes, then the certificate file must be properly signed.
2. When append_add_db_hash executes, then the binary hash file must be properly signed.
3. When append_add_dbx_hash executes, then the certificate/binary hash file must be signed.

---
 grub-core/commands/appendedsig/appendedsig.c | 377 ++++++++++++++++++-
 include/grub/file.h                          |   2 +
 2 files changed, 362 insertions(+), 17 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +360 -17
index fa908b963..1d7dc7ba1 100644
@@ -42,6 +42,9 @@ GRUB_MOD_LICENSE ("GPLv3+");
 /* Public key type. */
 #define GRUB_PKEY_ID_PKCS7 2
 
+#define OPTION_BINARY_HASH 0
+#define OPTION_CERT_HASH   1
+
 /* Appended signature magic string. */
 static const char magic[] = "~Module signature appended~\n";
 
@@ -106,6 +109,13 @@ static grub_size_t append_sig_len = 0;
  */
 static bool check_sigs = false;
 
+static const struct grub_arg_option options[] =
+{
+  {"binary-hash", 'b', 0, N_("hash file of the binary."), 0, ARG_TYPE_PATHNAME},
+  {"cert-hash", 'c', 1, N_("hash file of the certificate."), 0, ARG_TYPE_PATHNAME},
+  {0, 0, 0, 0, 0, 0}
+};
+
 static const char *
 grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),
                    const char *val __attribute__ ((unused)))
@@ -648,8 +658,8 @@ is_cert_present_in_db (const struct x509_certificate *cert_in)
   return false;
 }
 
-static bool
-is_cert_removed_from_db (const struct x509_certificate *cert)
+static void
+remove_cert_from_db (const struct x509_certificate *cert)
 {
   int i = 1;
   struct x509_certificate *curr_cert, *prev_cert;
@@ -664,17 +674,64 @@ is_cert_removed_from_db (const struct x509_certificate *cert)
             prev_cert->next = curr_cert->next;
 
           grub_dprintf ("appendedsig",
-                        "removed certificate with CN: %s from the db list\n", curr_cert->subject);
+                        "removed distrused certificate with CN: %s from the db list\n",
+                        curr_cert->subject);
           curr_cert->next = NULL;
           certificate_release (curr_cert);
           grub_free (curr_cert);
-          return true;
+          break;
 	}
       else
         prev_cert = curr_cert;
 
       i++;
     }
+}
+
+/*
+ * We cannot use hexdump() to display hash data because it is typically
+ * displayed in hexadecimal format, along with an ASCII representation of
+ * the same data.
+ * Example: sha256 hash data
+ * 00000000  52 b5 90 49 64 de 22 d7  4e 5f 4f b4 1b 51 9c 34  |R..Id.".N_O..Q.4|
+ * 00000010  b1 96 21 7c 91 78 a5 0d  20 8c e9 5c 22 54 53 f7  |..!|.x.. ..\"TS.|
+ *
+ * An appended signature only required to display the hexadecimal of the hash data
+ * by separating each byte with ":". So, we introduced a new method dump_ascii_to_hex
+ * to display it.
+ * Example: Sha256 hash data
+ *  52:b5:90:49:64:de:22:d7:4e:5f:4f:b4:1b:51:9c:34:
+ *  b1:96:21:7c:91:78:a5:0d:20:8c:e9:5c:22:54:53:f7
+ */
+static void
+dump_ascii_to_hex (const grub_uint8_t *data, const grub_size_t length)
+{
+  grub_size_t i, count = 0;
+
+  for (i = 0; i < length - 1; i++)
+    {
+      grub_printf ("%02x:", data[i]);
+      count++;
+      if (count == 16)
+        {
+          grub_printf ("\n\t      ");
+          count = 0;
+        }
+    }
+
+  grub_printf ("%02x\n", data[i]);
+}
+
+static bool
+is_cert_present_in_dbx (const struct x509_certificate *cert_in)
+{
+  struct x509_certificate *cert;
+
+  for (cert = dbx.certs; cert; cert = cert->next)
+    {
+      if (is_cert_match (cert, cert_in) == true)
+        return true;
+    }
 
   return false;
 }
@@ -738,12 +795,21 @@ grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **
       return err;
     }
 
-  if (is_cert_present_in_db (cert) == true)
+  if (is_cert_present_in_dbx (cert) == false)
+    {
+      if (is_cert_present_in_db (cert) == true)
+        err = grub_error (GRUB_ERR_STILL_REFERENCED,
+                          "could not add trusted certificate, as it is present in the db list");
+    }
+  else
+    err = grub_error (GRUB_ERR_STILL_REFERENCED,
+                      "could not add trusted certificate, as it is present in the dbx list");
+
+  if (err != GRUB_ERR_NONE)
     {
       certificate_release (cert);
       grub_free (cert);
-      return grub_error (GRUB_ERR_STILL_REFERENCED,
-                         "could not add the certificate, as it is present in the db list");
+      return err;
     }
 
   grub_dprintf ("appendedsig", "added certificate with CN: %s\n", cert->subject);
@@ -765,7 +831,7 @@ grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char *
   if (argc != 1)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
                        "a distrusted X.509 certificate file is expected in DER format\n"
-                       "Example:\n\tappend_rm_dbx_cert <X509_CERTIFICATE>\n");
+                       "Example:\n\tappend_add_dbx_cert <X509_CERTIFICATE>\n");
 
   if (*args == NULL)
     return grub_error (GRUB_ERR_BAD_FILENAME, "missing distrusted X.509 certificate file");
@@ -780,12 +846,24 @@ grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char *
   if (err != GRUB_ERR_NONE)
     grub_free (cert);
 
-  if (is_cert_removed_from_db (cert) == false)
-    err = grub_error (GRUB_ERR_EOF,
-                      "not found certificate with CN:%s in the db list", cert->subject);
+  if (is_cert_present_in_dbx (cert) == true)
+    {
+      certificate_release (cert);
+      grub_free (cert);
+      return grub_error (GRUB_ERR_STILL_REFERENCED,
+                         "could not add distrusted certificate, as it is present in the dbx list");
+    }
+
+  /* remove distrusted certificate from the db list if present. */
+  remove_cert_from_db (cert);
+
+  grub_dprintf ("appendedsig", "added distrusted certificate with CN: %s to the dbx list\n",
+                cert->subject);
+
+  cert->next = dbx.certs;
+  dbx.certs = cert;
+  dbx.cert_entries++;
 
-  certificate_release (cert);
-  grub_free (cert);
 
   return err;
 }
@@ -812,9 +890,255 @@ grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib
       cert_num++;
     }
 
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (db.signatures[i] != NULL)
+        {
+          grub_printf ("Binary hash %" PRIuGRUB_SIZE ":\n", i + 1);
+          grub_printf ("\thash: ");
+          dump_ascii_to_hex (db.signatures[i], db.signature_size[i]);
+        }
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_list_dbx (grub_command_t cmd __attribute__((unused)),
+                   int argc __attribute__((unused)), char **args __attribute__((unused)))
+{
+  struct x509_certificate *cert;
+  grub_size_t i, cert_num = 1;
+
+  for (cert = dbx.certs; cert; cert = cert->next)
+    {
+      grub_printf ("Certificate %" PRIuGRUB_SIZE ":\n", cert_num);
+      grub_printf ("\tserial: ");
+
+      for (i = 0; i < cert->serial_len - 1; i++)
+        grub_printf ("%02x:", cert->serial[i]);
+
+      grub_printf ("%02x\n", cert->serial[cert->serial_len - 1]);
+      grub_printf ("\tissuer: %s\n", cert->issuer);
+      grub_printf ("\tCN: %s\n\n", cert->subject);
+      cert_num++;
+    }
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    {
+      if (dbx.signatures[i] != NULL)
+        {
+          grub_printf ("Certificate/Binary hash %" PRIuGRUB_SIZE ":\n", i + 1);
+          grub_printf ("\thash: ");
+          dump_ascii_to_hex (dbx.signatures[i], dbx.signature_size[i]);
+        }
+    }
+
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+read_hash_from_file (char *file_path, grub_uint8_t **hash_data, grub_size_t *hash_data_size)
+{
+  grub_err_t rc;
+  grub_file_t hash_file;
+
+  hash_file = grub_file_open (file_path, GRUB_FILE_TYPE_HASH_TRUST | GRUB_FILE_TYPE_NO_DECOMPRESS);
+  if (hash_file == NULL)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "unable to open %s file", file_path);
+
+  rc = file_read_whole (hash_file, hash_data, hash_data_size);
+  grub_file_close (hash_file);
+
+  if (check_sigs == true)
+    *hash_data_size -= append_sig_len;
+
+  return rc;
+}
+
+static bool
+is_hash_present_in_db (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (grub_memcmp (db.signatures[i], hash_data, hash_data_size) == 0)
+        return true;
+    }
+
+  return false;
+}
+
+static bool
+is_hash_present_in_dbx (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    {
+      if (grub_memcmp (dbx.signatures[i], hash_data, hash_data_size) == 0)
+        return true;
+    }
+
+  return false;
+}
+
+static void
+remove_hash_from_db (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (grub_memcmp (db.signatures[i], hash_data, hash_data_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "removed distrusted hash %02x%02x%02x%02x.. from the db list\n",
+                        db.signatures[i][0], db.signatures[i][1], db.signatures[i][2],
+                        db.signatures[i][3]);
+          grub_free (db.signatures[i]);
+          db.signatures[i] = NULL;
+          db.signature_size[i] = 0;
+          break;
+	}
+    }
+}
+
+static grub_err_t
+grub_cmd_db_hash (grub_command_t cmd __attribute__((unused)), int argc, char**args)
+{
+  grub_err_t rc;
+  grub_uint8_t *hash_data = NULL;
+  grub_size_t hash_data_size = 0;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                       "a trusted binary hash file is expected in ASCII text format\n"
+                       "Example:\n\tappend_add_db_hash <BINARY HASH FILE>\n");
+
+  if (*args == NULL)
+    return grub_error (GRUB_ERR_BAD_FILENAME, "missing trusted binary hash file");
+
+  rc = read_hash_from_file (args[0], &hash_data, &hash_data_size);
+  if (rc == GRUB_ERR_NONE)
+    {
+      grub_dprintf ("appendedsig",
+                    "adding a trusted binary hash %02x%02x%02x%02x...\n with size of %" PRIuGRUB_SIZE "\n",
+                    hash_data[0], hash_data[1], hash_data[2], hash_data[3], hash_data_size);
+
+      /* Only accept SHA256, SHA384 and SHA512 binary hash */
+      if (hash_data_size != 32 && hash_data_size != 48 && hash_data_size != 64)
+        {
+          grub_free (hash_data);
+          return grub_error (GRUB_ERR_BAD_SIGNATURE, "unacceptable trusted binary hash type");
+        }
+
+      if (is_hash_present_in_dbx (hash_data, hash_data_size) == true)
+        {
+          grub_free (hash_data);
+          return grub_error (GRUB_ERR_STILL_REFERENCED,
+                             "could not add trusted binary hash, as it is present in the dbx list");
+        }
+
+      if (is_hash_present_in_db (hash_data, hash_data_size) == false)
+        {
+          rc = add_hash ((const grub_uint8_t **) &hash_data, hash_data_size, &db.signatures,
+                         &db.signature_size, &db.signature_entries);
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              free_dbx_list ();
+              rc = grub_error (rc, "adding of trusted binary hash failed");
+            }
+          else
+            grub_dprintf ("appendedsig",
+                          "added trusted binary hash %02x%02x%02x%02x...\n to the db list\n",
+                          hash_data[0], hash_data[1], hash_data[2], hash_data[3]);
+         }
+       else
+         rc = grub_error (GRUB_ERR_STILL_REFERENCED,
+                          "could not add trusted binary hash, as it is present in the db list");
+    }
+
+  grub_free (hash_data);
+
+  return rc;
+}
+
+static grub_err_t
+grub_cmd_dbx_hash (grub_extcmd_context_t ctxt, int argc __attribute__ ((unused)),
+                   char **args __attribute__ ((unused)))
+{
+  grub_err_t rc;
+  grub_uint8_t *hash_data = NULL;
+  grub_size_t hash_data_size = 0;
+  char *file_path;
+
+  if (!ctxt->state[OPTION_BINARY_HASH].set && !ctxt->state[OPTION_CERT_HASH].set)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                       "a distrusted certificate/binary hash file is expected in ASCII text format\n"
+                       "Example:\n\tappend_add_dbx_hash [option] <FILE>\n"
+                       "option:\n[-b|--binary-hash] FILE [BINARY HASH FILE]\n"
+                       "[-c|--cert-hash] FILE [CERTFICATE HASH FILE]\n");
+
+  if (ctxt->state[OPTION_BINARY_HASH].arg == NULL && ctxt->state[OPTION_CERT_HASH].arg == NULL)
+    return grub_error (GRUB_ERR_BAD_FILENAME, "missing distrusted certificate/binary hash file");
+
+  if (ctxt->state[OPTION_BINARY_HASH].arg != NULL)
+    file_path = ctxt->state[OPTION_BINARY_HASH].arg;
+  else
+    file_path = ctxt->state[OPTION_CERT_HASH].arg;
+
+  rc = read_hash_from_file (file_path, &hash_data, &hash_data_size);
+  if (rc == GRUB_ERR_NONE)
+    {
+      grub_dprintf ("appendedsig",
+                    "adding a distrusted certificate/binary hash %02x%02x%02x%02x...\n"
+                    " with size of %" PRIuGRUB_SIZE "\n", hash_data[0], hash_data[1],
+                    hash_data[2], hash_data[3], hash_data_size);
+
+      if (ctxt->state[OPTION_BINARY_HASH].set || ctxt->state[OPTION_CERT_HASH].set)
+        {
+          /* Only accept SHA256, SHA384 and SHA512 certificate/binary hash */
+          if (hash_data_size != 32 && hash_data_size != 48 && hash_data_size != 64)
+            {
+              rc = grub_error (GRUB_ERR_BAD_SIGNATURE,
+                               "unacceptable distrusted certificate/binary hash type");
+              goto clean;
+            }
+        }
+
+      if (is_hash_present_in_dbx (hash_data, hash_data_size) == true)
+        {
+          rc = grub_error (GRUB_ERR_STILL_REFERENCED,
+                           "could not add distrusted certificate/binary hash, "
+                           "as it is present in the dbx list");
+          goto clean;
+        }
+
+        /* remove distrusted hash from the db list if present. */
+        remove_hash_from_db (hash_data, hash_data_size);
+
+      rc = add_hash ((const grub_uint8_t **) &hash_data, hash_data_size, &dbx.signatures,
+                     &dbx.signature_size, &dbx.signature_entries);
+      if (rc != GRUB_ERR_NONE)
+        {
+          free_db_list ();
+          free_dbx_list ();
+          rc = grub_error (rc, "adding of distrusted binary/certificate hash failed");
+        }
+      else
+        grub_dprintf ("appendedsig",
+                      "added distrusted binary/certificate hash %02x%02x%02x%02x...\n to the dbx list\n",
+                      hash_data[0], hash_data[1], hash_data[2], hash_data[3]);
+    }
+
+ clean:
+  grub_free (hash_data);
+
+  return rc;
+}
+
 static grub_err_t
 appendedsig_init (grub_file_t io __attribute__ ((unused)), enum grub_file_type type,
                   void **context __attribute__ ((unused)), enum grub_verify_flags *flags)
@@ -835,6 +1159,11 @@ appendedsig_init (grub_file_t io __attribute__ ((unused)), enum grub_file_type t
          * verifier, but we lack the hubris required to take this on. Instead,
          * require that it have an appended signature.
          */
+      case GRUB_FILE_TYPE_HASH_TRUST:
+        /*
+         * This is a certificate/binary hash to add to db/dbx.
+         * This needs to be verified or blocked.
+         */
       case GRUB_FILE_TYPE_LINUX_KERNEL:
       case GRUB_FILE_TYPE_GRUB_MODULE:
         /*
@@ -888,8 +1217,6 @@ static struct grub_fs pseudo_fs = {
   .fs_read = pseudo_read
 };
 
-static grub_command_t cmd_verify, cmd_list_db, cmd_dbx_cert, cmd_db_cert;
-
 /* Check the certificate hash presence in the PKS dbx list. */
 static bool
 is_dbx_cert_hash (const grub_uint8_t *data, const grub_size_t data_size)
@@ -1147,6 +1474,10 @@ load_static_keys (const struct grub_module_header *header, const bool is_pks)
   return rc;
 }
 
+static grub_extcmd_t cmd_dbx_hash;
+static grub_command_t cmd_verify, cmd_list_db, cmd_db_cert, cmd_db_hash,
+                      cmd_list_dbx, cmd_dbx_cert;
+
 GRUB_MOD_INIT (appendedsig)
 {
   int rc;
@@ -1221,12 +1552,21 @@ GRUB_MOD_INIT (appendedsig)
 
   cmd_db_cert = grub_register_command ("append_add_db_cert", grub_cmd_db_cert, N_("X509_CERTIFICATE"),
                                        N_("Add trusted X509_CERTIFICATE to the db list"));
-  cmd_dbx_cert = grub_register_command ("append_rm_dbx_cert", grub_cmd_dbx_cert, N_("X509_CERTIFICATE"),
-                                        N_("Remove distrusted X509_CERTIFICATE from the db list"));
+  cmd_dbx_cert = grub_register_command ("append_add_dbx_cert", grub_cmd_dbx_cert, N_("X509_CERTIFICATE"),
+                                        N_("Add distrusted X509_CERTIFICATE to the dbx list"));
   cmd_list_db = grub_register_command ("append_list_db", grub_cmd_list_db, 0,
                                        N_("Show the list of trusted X.509 certificates from the db list"));
   cmd_verify = grub_register_command ("append_verify", grub_cmd_verify_signature, N_("SIGNED_FILE"),
                                       N_("Verify SIGNED_FILE against the trusted X.509 certificates in the db list"));
+  cmd_list_dbx = grub_register_command ("append_list_dbx", grub_cmd_list_dbx, 0,
+                                        N_("Show the list of distrusted certificates and"
+                                           " certificate/binary hashes from the dbx list"));
+  cmd_db_hash = grub_register_command ("append_add_db_hash", grub_cmd_db_hash, N_("BINARY HASH FILE"),
+                                       N_("Add trusted BINARY HASH to the db list."));
+  cmd_dbx_hash = grub_register_extcmd ("append_add_dbx_hash", grub_cmd_dbx_hash, 0,
+                                       N_("[-b|--binary-hash] FILE [BINARY HASH FILE]\n"
+                                          "[-c|--cert-hash] FILE [CERTFICATE HASH FILE]"),
+                                       N_("Add distrusted CERTFICATE/BINARY HASH to the dbx list."), options);
 
   grub_verifier_register (&grub_appendedsig_verifier);
   grub_dl_set_persistent (mod);
@@ -1243,4 +1583,7 @@ GRUB_MOD_FINI (appendedsig)
   grub_unregister_command (cmd_list_db);
   grub_unregister_command (cmd_db_cert);
   grub_unregister_command (cmd_dbx_cert);
+  grub_unregister_command (cmd_list_dbx);
+  grub_unregister_command (cmd_db_hash);
+  grub_unregister_extcmd (cmd_dbx_hash);
 }
include/grub/file.h +2 -0
index d678de063..16a4b7d26 100644
@@ -115,6 +115,8 @@ enum grub_file_type
     GRUB_FILE_TYPE_HASHLIST,
     /* File hashed by hashsum.  */
     GRUB_FILE_TYPE_TO_HASH,
+    /* File holding certificiate/binary hash to add to db/dbx. */
+    GRUB_FILE_TYPE_HASH_TRUST,
     /* Keyboard layout.  */
     GRUB_FILE_TYPE_KEYBOARD_LAYOUT,
     /* Picture file.  */
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#31 lore raw

Re: [PATCH v6 16/20] appended signatures: Introduce GRUB commands to access db and dbx

Quoted 62 lines · Sudhakar Kuppusamy

Introducing the following db and dbx commands

1. append_list_db:
Show the list of trusted certificates and binary hashes from the db list.
2. append_list_dbx:
Show the list of distrusted certificates and binary/certificate hashes from the dbx list.
3. append_add_db_cert:
Add the trusted certificate to the db list.
4. append_add_db_hash:
Add the trusted binary hash to the db list.
5. append_add_dbx_cert:
Add the distrusted certificate to the dbx list.
6. append_add_dbx_hash:
Add the distrusted certificate/binary hash to the dbx list.

Note that if signature verification (check_appended_signature) is set to enforce,
1. When append_add_db_cert or append_add_dbx_cert executes, then the certificate file must be properly signed.
2. When append_add_db_hash executes, then the binary hash file must be properly signed.
3. When append_add_dbx_hash executes, then the certificate/binary hash file must be signed.

---
 grub-core/commands/appendedsig/appendedsig.c | 377 ++++++++++++++++++-
 include/grub/file.h                          |   2 +
 2 files changed, 362 insertions(+), 17 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +10 -0
index fa908b963..1d7dc7ba1 100644
@@ -42,6 +42,9 @@ GRUB_MOD_LICENSE ("GPLv3+");
 /* Public key type. */
 #define GRUB_PKEY_ID_PKCS7 2
 
+#define OPTION_BINARY_HASH 0
+#define OPTION_CERT_HASH   1
+
 /* Appended signature magic string. */
 static const char magic[] = "~Module signature appended~\n";
 
@@ -106,6 +109,13 @@ static grub_size_t append_sig_len = 0;
  */
 static bool check_sigs = false;
 
+static const struct grub_arg_option options[] =
+{
+  {"binary-hash", 'b', 0, N_("hash file of the binary."), 0, ARG_TYPE_PATHNAME},
+  {"cert-hash", 'c', 1, N_("hash file of the certificate."), 0, ARG_TYPE_PATHNAME},
+  {0, 0, 0, 0, 0, 0}
+};
+
 static const char *
 grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),
                    const char *val __attribute__ ((unused)))

grub-core/commands/appendedsig/appendedsig.c is_cert_present_in_db (const struct x509_certificate *cert_in) · line 662

Quoted 7 lines
grub-core/commands/appendedsig/appendedsig.c +2 -2
@@ -648,8 +658,8 @@ is_cert_present_in_db (const struct x509_certificate *cert_in)
   return false;
 }
 
-static bool
-is_cert_removed_from_db (const struct x509_certificate *cert)
+static void
+remove_cert_from_db (const struct x509_certificate *cert)

This...

grub-core/commands/appendedsig/appendedsig.c is_cert_removed_from_db (const struct x509_certificate *cert) · line 678

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +2 -1
 {
   int i = 1;
   struct x509_certificate *curr_cert, *prev_cert;
@@ -664,17 +674,64 @@ is_cert_removed_from_db (const struct x509_certificate *cert)
             prev_cert->next = curr_cert->next;
 
           grub_dprintf ("appendedsig",
-                        "removed certificate with CN: %s from the db list\n", curr_cert->subject);
+                        "removed distrused certificate with CN: %s from the db list\n",
+                        curr_cert->subject);

... this...

grub-core/commands/appendedsig/appendedsig.c is_cert_removed_from_db (const struct x509_certificate *cert) · line 682

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
           curr_cert->next = NULL;
           certificate_release (curr_cert);
           grub_free (curr_cert);
-          return true;
+          break;

... and this change does not belong to this patch...

Quoted 15 lines
grub-core/commands/appendedsig/appendedsig.c +9 -0
 	}
       else
         prev_cert = curr_cert;
 
       i++;
     }
+}
+
+/*
+ * We cannot use hexdump() to display hash data because it is typically
+ * displayed in hexadecimal format, along with an ASCII representation of
+ * the same data.
+ * Example: sha256 hash data
+ * 00000000  52 b5 90 49 64 de 22 d7  4e 5f 4f b4 1b 51 9c 34  |R..Id.".N_O..Q.4|
+ * 00000010  b1 96 21 7c 91 78 a5 0d  20 8c e9 5c 22 54 53 f7  |..!|.x.. ..\"TS.|

grub-core/commands/appendedsig/appendedsig.c is_cert_removed_from_db (const struct x509_certificate *cert) · line 707

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+ *
+ * An appended signature only required to display the hexadecimal of the hash data
+ * by separating each byte with ":". So, we introduced a new method dump_ascii_to_hex
+ * to display it.
+ * Example: Sha256 hash data
+ *  52:b5:90:49:64:de:22:d7:4e:5f:4f:b4:1b:51:9c:34:
+ *  b1:96:21:7c:91:78:a5:0d:20:8c:e9:5c:22:54:53:f7
+ */
+static void
+dump_ascii_to_hex (const grub_uint8_t *data, const grub_size_t length)

s/dump_ascii_to_hex/dump_data_hex/

Quoted 31 lines
grub-core/commands/appendedsig/appendedsig.c +27 -0
+{
+  grub_size_t i, count = 0;
+
+  for (i = 0; i < length - 1; i++)
+    {
+      grub_printf ("%02x:", data[i]);
+      count++;
+      if (count == 16)
+        {
+          grub_printf ("\n\t      ");
+          count = 0;
+        }
+    }
+
+  grub_printf ("%02x\n", data[i]);
+}
+
+static bool
+is_cert_present_in_dbx (const struct x509_certificate *cert_in)
+{
+  struct x509_certificate *cert;
+
+  for (cert = dbx.certs; cert; cert = cert->next)
+    {
+      if (is_cert_match (cert, cert_in) == true)
+        return true;
+    }
 
   return false;
 }

grub-core/commands/appendedsig/appendedsig.c grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char ** · line 801

Quoted 8 lines
grub-core/commands/appendedsig/appendedsig.c +4 -1
@@ -738,12 +795,21 @@ grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **
       return err;
     }
 
-  if (is_cert_present_in_db (cert) == true)
+  if (is_cert_present_in_dbx (cert) == false)
+    {
+      if (is_cert_present_in_db (cert) == true)
+        err = grub_error (GRUB_ERR_STILL_REFERENCED,

s/GRUB_ERR_STILL_REFERENCED/GRUB_ERR_EXISTS/? And probably in other similar places too...

grub-core/commands/appendedsig/appendedsig.c grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char ** · line 805

Quoted 4 lines
grub-core/commands/appendedsig/appendedsig.c +4 -0
+                          "could not add trusted certificate, as it is present in the db list");
+    }
+  else
+    err = grub_error (GRUB_ERR_STILL_REFERENCED,

s/GRUB_ERR_STILL_REFERENCED/GRUB_ERR_ACCESS_DENIED/? And probably in other similar places too...

Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c +4 -2
+                      "could not add trusted certificate, as it is present in the dbx list");
+
+  if (err != GRUB_ERR_NONE)
     {
       certificate_release (cert);
       grub_free (cert);
-      return grub_error (GRUB_ERR_STILL_REFERENCED,
-                         "could not add the certificate, as it is present in the db list");
+      return err;
     }
 
   grub_dprintf ("appendedsig", "added certificate with CN: %s\n", cert->subject);

grub-core/commands/appendedsig/appendedsig.c grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char * · line 834

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
@@ -765,7 +831,7 @@ grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char *
   if (argc != 1)
     return grub_error (GRUB_ERR_BAD_ARGUMENT,
                        "a distrusted X.509 certificate file is expected in DER format\n"
-                       "Example:\n\tappend_rm_dbx_cert <X509_CERTIFICATE>\n");
+                       "Example:\n\tappend_add_dbx_cert <X509_CERTIFICATE>\n");

Hmmm... This looks strange for me...

Quoted 9 lines
grub-core/commands/appendedsig/appendedsig.c +0 -2
   if (*args == NULL)
     return grub_error (GRUB_ERR_BAD_FILENAME, "missing distrusted X.509 certificate file");
@@ -780,12 +846,24 @@ grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char *
   if (err != GRUB_ERR_NONE)
     grub_free (cert);
 
-  if (is_cert_removed_from_db (cert) == false)
-    err = grub_error (GRUB_ERR_EOF,

grub-core/commands/appendedsig/appendedsig.c grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char * · line 857

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +9 -1
-                      "not found certificate with CN:%s in the db list", cert->subject);
+  if (is_cert_present_in_dbx (cert) == true)
+    {
+      certificate_release (cert);
+      grub_free (cert);
+      return grub_error (GRUB_ERR_STILL_REFERENCED,
+                         "could not add distrusted certificate, as it is present in the dbx list");
+    }
+
+  /* remove distrusted certificate from the db list if present. */

s/remove/Remove/

Quoted 27 lines
grub-core/commands/appendedsig/appendedsig.c +17 -2
+  remove_cert_from_db (cert);
+
+  grub_dprintf ("appendedsig", "added distrusted certificate with CN: %s to the dbx list\n",
+                cert->subject);
+
+  cert->next = dbx.certs;
+  dbx.certs = cert;
+  dbx.cert_entries++;
 
-  certificate_release (cert);
-  grub_free (cert);
 
   return err;
 }
@@ -812,9 +890,255 @@ grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib
       cert_num++;
     }
 
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (db.signatures[i] != NULL)
+        {
+          grub_printf ("Binary hash %" PRIuGRUB_SIZE ":\n", i + 1);
+          grub_printf ("\thash: ");
+          dump_ascii_to_hex (db.signatures[i], db.signature_size[i]);
+        }
+    }

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 911

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_list_dbx (grub_command_t cmd __attribute__((unused)),
+                   int argc __attribute__((unused)), char **args __attribute__((unused)))
+{
+  struct x509_certificate *cert;
+  grub_size_t i, cert_num = 1;

It seems to me this is defined as grub_size_t due to dbx.signature_entries being grub_size_t. However, I do not think it should be larger than grub_uint32_t. So, please change all of this everywhere to grub_uint32_t...

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 912

Quoted 1 line
grub-core/commands/appendedsig/appendedsig.c +1 -0
+  for (cert = dbx.certs; cert; cert = cert->next)

for (cert = dbx.certs; cert; cert = cert->next, cert_num++)

... and you can drop "cert_num++" below...

Quoted 31 lines
grub-core/commands/appendedsig/appendedsig.c +28 -0
+    {
+      grub_printf ("Certificate %" PRIuGRUB_SIZE ":\n", cert_num);
+      grub_printf ("\tserial: ");
+
+      for (i = 0; i < cert->serial_len - 1; i++)
+        grub_printf ("%02x:", cert->serial[i]);
+
+      grub_printf ("%02x\n", cert->serial[cert->serial_len - 1]);
+      grub_printf ("\tissuer: %s\n", cert->issuer);
+      grub_printf ("\tCN: %s\n\n", cert->subject);
+      cert_num++;
+    }
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    {
+      if (dbx.signatures[i] != NULL)
+        {
+          grub_printf ("Certificate/Binary hash %" PRIuGRUB_SIZE ":\n", i + 1);
+          grub_printf ("\thash: ");
+          dump_ascii_to_hex (dbx.signatures[i], dbx.signature_size[i]);
+        }
+    }
+
   return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+read_hash_from_file (char *file_path, grub_uint8_t **hash_data, grub_size_t *hash_data_size)
+{
+  grub_err_t rc;
+  grub_file_t hash_file;

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 954

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+
+  hash_file = grub_file_open (file_path, GRUB_FILE_TYPE_HASH_TRUST | GRUB_FILE_TYPE_NO_DECOMPRESS);
+  if (hash_file == NULL)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "unable to open %s file", file_path);
+
+  rc = file_read_whole (hash_file, hash_data, hash_data_size);
+  grub_file_close (hash_file);
+
+  if (check_sigs == true)
+    *hash_data_size -= append_sig_len;

Please comment this "if"...

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 968

Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c +13 -0
+  return rc;
+}
+
+static bool
+is_hash_present_in_db (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (grub_memcmp (db.signatures[i], hash_data, hash_data_size) == 0)
+        return true;
+    }

You can drop curly braces here...

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 982

Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c +13 -0
+  return false;
+}
+
+static bool
+is_hash_present_in_dbx (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    {
+      if (grub_memcmp (dbx.signatures[i], hash_data, hash_data_size) == 0)
+        return true;
+    }

Ditto...

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+  return false;
+}
+
+static void
+remove_hash_from_db (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < db.signature_entries; i++)
+    {

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 1003

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+      if (grub_memcmp (db.signatures[i], hash_data, hash_data_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "removed distrusted hash %02x%02x%02x%02x.. from the db list\n",
+                        db.signatures[i][0], db.signatures[i][1], db.signatures[i][2],
+                        db.signatures[i][3]);
+          grub_free (db.signatures[i]);
+          db.signatures[i] = NULL;
+          db.signature_size[i] = 0;
+          break;
+	}

Something is off with indention here...

Quoted 37 lines
grub-core/commands/appendedsig/appendedsig.c +37 -0
+    }
+}
+
+static grub_err_t
+grub_cmd_db_hash (grub_command_t cmd __attribute__((unused)), int argc, char**args)
+{
+  grub_err_t rc;
+  grub_uint8_t *hash_data = NULL;
+  grub_size_t hash_data_size = 0;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                       "a trusted binary hash file is expected in ASCII text format\n"
+                       "Example:\n\tappend_add_db_hash <BINARY HASH FILE>\n");
+
+  if (*args == NULL)
+    return grub_error (GRUB_ERR_BAD_FILENAME, "missing trusted binary hash file");
+
+  rc = read_hash_from_file (args[0], &hash_data, &hash_data_size);
+  if (rc == GRUB_ERR_NONE)
+    {
+      grub_dprintf ("appendedsig",
+                    "adding a trusted binary hash %02x%02x%02x%02x...\n with size of %" PRIuGRUB_SIZE "\n",
+                    hash_data[0], hash_data[1], hash_data[2], hash_data[3], hash_data_size);
+
+      /* Only accept SHA256, SHA384 and SHA512 binary hash */
+      if (hash_data_size != 32 && hash_data_size != 48 && hash_data_size != 64)
+        {
+          grub_free (hash_data);
+          return grub_error (GRUB_ERR_BAD_SIGNATURE, "unacceptable trusted binary hash type");
+        }
+
+      if (is_hash_present_in_dbx (hash_data, hash_data_size) == true)
+        {
+          grub_free (hash_data);
+          return grub_error (GRUB_ERR_STILL_REFERENCED,
+                             "could not add trusted binary hash, as it is present in the dbx list");

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 1050

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+        }
+
+      if (is_hash_present_in_db (hash_data, hash_data_size) == false)
+        {
+          rc = add_hash ((const grub_uint8_t **) &hash_data, hash_data_size, &db.signatures,
+                         &db.signature_size, &db.signature_entries);
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              free_dbx_list ();

Again, please do not free partial db/dbx... I think this should not be done in almost everywhere...

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+              rc = grub_error (rc, "adding of trusted binary hash failed");
+            }
+          else
+            grub_dprintf ("appendedsig",
+                          "added trusted binary hash %02x%02x%02x%02x...\n to the db list\n",
+                          hash_data[0], hash_data[1], hash_data[2], hash_data[3]);

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 1066

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+         }
+       else
+         rc = grub_error (GRUB_ERR_STILL_REFERENCED,
+                          "could not add trusted binary hash, as it is present in the db list");
+    }
+
+  grub_free (hash_data);
+
+  return rc;
+}

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#32 lore raw

Re: [PATCH v6 16/20] appended signatures: Introduce GRUB commands to access db and dbx

Thank you Daniel.

Quoted 88 lines · Daniel Kiper
Quoted 62 lines · Sudhakar Kuppusamy

Introducing the following db and dbx commands

1. append_list_db:
Show the list of trusted certificates and binary hashes from the db list.
2. append_list_dbx:
Show the list of distrusted certificates and binary/certificate hashes from the dbx list.
3. append_add_db_cert:
Add the trusted certificate to the db list.
4. append_add_db_hash:
Add the trusted binary hash to the db list.
5. append_add_dbx_cert:
Add the distrusted certificate to the dbx list.
6. append_add_dbx_hash:
Add the distrusted certificate/binary hash to the dbx list.

Note that if signature verification (check_appended_signature) is set to enforce,
1. When append_add_db_cert or append_add_dbx_cert executes, then the certificate file must be properly signed.
2. When append_add_db_hash executes, then the binary hash file must be properly signed.
3. When append_add_dbx_hash executes, then the certificate/binary hash file must be signed.

---
grub-core/commands/appendedsig/appendedsig.c | 377 ++++++++++++++++++-
include/grub/file.h | 2 +

2 files changed, 362 insertions(+), 17 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +10 -0
index fa908b963..1d7dc7ba1 100644
@@ -42,6 +42,9 @@ GRUB_MOD_LICENSE ("GPLv3+");
 /* Public key type. */
 #define GRUB_PKEY_ID_PKCS7 2
 
+#define OPTION_BINARY_HASH 0
+#define OPTION_CERT_HASH   1
+
 /* Appended signature magic string. */
 static const char magic[] = "~Module signature appended~\n";
 
@@ -106,6 +109,13 @@ static grub_size_t append_sig_len = 0;
 */
 static bool check_sigs = false;
 
+static const struct grub_arg_option options[] =
+{
+  {"binary-hash", 'b', 0, N_("hash file of the binary."), 0, ARG_TYPE_PATHNAME},
+  {"cert-hash", 'c', 1, N_("hash file of the certificate."), 0, ARG_TYPE_PATHNAME},
+  {0, 0, 0, 0, 0, 0}
+};
+

static const char *
grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),
const char *val __attribute__ ((unused)))

grub-core/commands/appendedsig/appendedsig.c is_cert_present_in_db (const struct x509_certificate *cert_in) · line 662

Quoted 7 lines
grub-core/commands/appendedsig/appendedsig.c +2 -2
@@ -648,8 +658,8 @@ is_cert_present_in_db (const struct x509_certificate *cert_in)
  return false;
 }
 
-static bool
-is_cert_removed_from_db (const struct x509_certificate *cert)
+static void
+remove_cert_from_db (const struct x509_certificate *cert)

This...

grub-core/commands/appendedsig/appendedsig.c is_cert_removed_from_db (const struct x509_certificate *cert) · line 678

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +2 -1
 {
  int i = 1;
  struct x509_certificate *curr_cert, *prev_cert;
@@ -664,17 +674,64 @@ is_cert_removed_from_db (const struct x509_certificate *cert)
            prev_cert->next = curr_cert->next;
 
          grub_dprintf ("appendedsig",
-                        "removed certificate with CN: %s from the db list\n", curr_cert->subject);
+                        "removed distrused certificate with CN: %s from the db list\n",
+                        curr_cert->subject);

... this...

Quoted 7 lines

grub-core/commands/appendedsig/appendedsig.c is_cert_removed_from_db (const struct x509_certificate *cert) · line 682

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
          curr_cert->next = NULL;
          certificate_release (curr_cert);
          grub_free (curr_cert);
-          return true;
+          break;

... and this change does not belong to this patch…

Sure, will move it to patch 10.

Quoted 20 lines
Quoted 20 lines
grub-core/commands/appendedsig/appendedsig.c +13 -0
 }
      else
        prev_cert = curr_cert;
 
      i++;
    }
+}
+
+/*
+ * We cannot use hexdump() to display hash data because it is typically
+ * displayed in hexadecimal format, along with an ASCII representation of
+ * the same data.
+ * Example: sha256 hash data
+ * 00000000  52 b5 90 49 64 de 22 d7  4e 5f 4f b4 1b 51 9c 34  |R..Id.".N_O..Q.4|
+ * 00000010  b1 96 21 7c 91 78 a5 0d  20 8c e9 5c 22 54 53 f7  |..!|.x.. ..\"TS.|
+ *
+ * An appended signature only required to display the hexadecimal of the hash data
+ * by separating each byte with ":". So, we introduced a new method dump_ascii_to_hex
+ * to display it.
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c is_cert_removed_from_db (const struct x509_certificate *cert) · line 707

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+ * Example: Sha256 hash data
+ *  52:b5:90:49:64:de:22:d7:4e:5f:4f:b4:1b:51:9c:34:
+ *  b1:96:21:7c:91:78:a5:0d:20:8c:e9:5c:22:54:53:f7
+ */
+static void
+dump_ascii_to_hex (const grub_uint8_t *data, const grub_size_t length)

s/dump_ascii_to_hex/dump_data_hex/

Will do it.

Quoted 35 lines
Quoted 35 lines
grub-core/commands/appendedsig/appendedsig.c +27 -0
+{
+  grub_size_t i, count = 0;
+
+  for (i = 0; i < length - 1; i++)
+    {
+      grub_printf ("%02x:", data[i]);
+      count++;
+      if (count == 16)
+        {
+          grub_printf ("\n\t      ");
+          count = 0;
+        }
+    }
+
+  grub_printf ("%02x\n", data[i]);
+}
+
+static bool
+is_cert_present_in_dbx (const struct x509_certificate *cert_in)
+{
+  struct x509_certificate *cert;
+
+  for (cert = dbx.certs; cert; cert = cert->next)
+    {
+      if (is_cert_match (cert, cert_in) == true)
+        return true;
+    }
 
  return false;
 }
@@ -738,12 +795,21 @@ grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char **
      return err;
    }
 
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char ** · line 801

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +4 -1
-  if (is_cert_present_in_db (cert) == true)
+  if (is_cert_present_in_dbx (cert) == false)
+    {
+      if (is_cert_present_in_db (cert) == true)
+        err = grub_error (GRUB_ERR_STILL_REFERENCED,

s/GRUB_ERR_STILL_REFERENCED/GRUB_ERR_EXISTS/? And probably in other similar places too…

Sure, will do it.

Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_db_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char ** · line 805

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +4 -0
+                          "could not add trusted certificate, as it is present in the db list");
+    }
+  else
+    err = grub_error (GRUB_ERR_STILL_REFERENCED,

s/GRUB_ERR_STILL_REFERENCED/GRUB_ERR_ACCESS_DENIED/? And probably in other similar places too…

Sure will do it.

Quoted 12 lines
Quoted 12 lines
grub-core/commands/appendedsig/appendedsig.c +4 -2
+                      "could not add trusted certificate, as it is present in the dbx list");
+
+  if (err != GRUB_ERR_NONE)
    {
      certificate_release (cert);
      grub_free (cert);
-      return grub_error (GRUB_ERR_STILL_REFERENCED,
-                         "could not add the certificate, as it is present in the db list");
+      return err;
    }
 
Quoted 9 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char * · line 834

Quoted 7 lines
grub-core/commands/appendedsig/appendedsig.c +1 -1
  grub_dprintf ("appendedsig", "added certificate with CN: %s\n", cert->subject);
@@ -765,7 +831,7 @@ grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char *
  if (argc != 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
                       "a distrusted X.509 certificate file is expected in DER format\n"
-                       "Example:\n\tappend_rm_dbx_cert <X509_CERTIFICATE>\n");
+                       "Example:\n\tappend_add_dbx_cert <X509_CERTIFICATE>\n");

Hmmm... This looks strange for me…

Patches 1 to 10 are static key management, which has only db. So the append_rm_dbx_cert command removes the given cert from the db. However, patches 11 to 16 are dynamic key management, which has db and dbx, so here the append_add_dbx_cert command inserts a given cert into dbx and removes it from the db. So, the command name was changed from "append_rm_dbx_cert" to "append_add_dbx_cert" in this patch.

Quoted 8 lines
Quoted 8 lines
grub-core/commands/appendedsig/appendedsig.c +0 -0
  if (*args == NULL)
    return grub_error (GRUB_ERR_BAD_FILENAME, "missing distrusted X.509 certificate file");
@@ -780,12 +846,24 @@ grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char *
  if (err != GRUB_ERR_NONE)
    grub_free (cert);
 
Quoted 14 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)), int argc, char * · line 857

Quoted 12 lines
grub-core/commands/appendedsig/appendedsig.c +9 -3
-  if (is_cert_removed_from_db (cert) == false)
-    err = grub_error (GRUB_ERR_EOF,
-                      "not found certificate with CN:%s in the db list", cert->subject);
+  if (is_cert_present_in_dbx (cert) == true)
+    {
+      certificate_release (cert);
+      grub_free (cert);
+      return grub_error (GRUB_ERR_STILL_REFERENCED,
+                         "could not add distrusted certificate, as it is present in the dbx list");
+    }
+
+  /* remove distrusted certificate from the db list if present. */

s/remove/Remove/

Will do it.

Quoted 35 lines
Quoted 35 lines
grub-core/commands/appendedsig/appendedsig.c +24 -2
+  remove_cert_from_db (cert);
+
+  grub_dprintf ("appendedsig", "added distrusted certificate with CN: %s to the dbx list\n",
+                cert->subject);
+
+  cert->next = dbx.certs;
+  dbx.certs = cert;
+  dbx.cert_entries++;
 
-  certificate_release (cert);
-  grub_free (cert);
 
  return err;
 }
@@ -812,9 +890,255 @@ grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib
      cert_num++;
    }
 
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (db.signatures[i] != NULL)
+        {
+          grub_printf ("Binary hash %" PRIuGRUB_SIZE ":\n", i + 1);
+          grub_printf ("\thash: ");
+          dump_ascii_to_hex (db.signatures[i], db.signature_size[i]);
+        }
+    }
+
+  return GRUB_ERR_NONE;
+}
+
+static grub_err_t
+grub_cmd_list_dbx (grub_command_t cmd __attribute__((unused)),
+                   int argc __attribute__((unused)), char **args __attribute__((unused)))
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 911

Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c +3 -0
+{
+  struct x509_certificate *cert;
+  grub_size_t i, cert_num = 1;

It seems to me this is defined as grub_size_t due to dbx.signature_entries being grub_size_t. However, I do not think it should be larger than grub_uint32_t. So, please change all of this everywhere to grub_uint32_t…

Sure, will do it.

Quoted 6 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 912

Quoted 2 lines
grub-core/commands/appendedsig/appendedsig.c +1 -0
+  for (cert = dbx.certs; cert; cert = cert->next)

for (cert = dbx.certs; cert; cert = cert->next, cert_num++)

... and you can drop "cert_num++" below…

Sure, will drop it.

Quoted 36 lines
Quoted 36 lines
grub-core/commands/appendedsig/appendedsig.c +32 -0
+    {
+      grub_printf ("Certificate %" PRIuGRUB_SIZE ":\n", cert_num);
+      grub_printf ("\tserial: ");
+
+      for (i = 0; i < cert->serial_len - 1; i++)
+        grub_printf ("%02x:", cert->serial[i]);
+
+      grub_printf ("%02x\n", cert->serial[cert->serial_len - 1]);
+      grub_printf ("\tissuer: %s\n", cert->issuer);
+      grub_printf ("\tCN: %s\n\n", cert->subject);
+      cert_num++;
+    }
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    {
+      if (dbx.signatures[i] != NULL)
+        {
+          grub_printf ("Certificate/Binary hash %" PRIuGRUB_SIZE ":\n", i + 1);
+          grub_printf ("\thash: ");
+          dump_ascii_to_hex (dbx.signatures[i], dbx.signature_size[i]);
+        }
+    }
+
  return GRUB_ERR_NONE;
 }
 
+static grub_err_t
+read_hash_from_file (char *file_path, grub_uint8_t **hash_data, grub_size_t *hash_data_size)
+{
+  grub_err_t rc;
+  grub_file_t hash_file;
+
+  hash_file = grub_file_open (file_path, GRUB_FILE_TYPE_HASH_TRUST | GRUB_FILE_TYPE_NO_DECOMPRESS);
+  if (hash_file == NULL)
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "unable to open %s file", file_path);
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 954

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+
+  rc = file_read_whole (hash_file, hash_data, hash_data_size);
+  grub_file_close (hash_file);
+
+  if (check_sigs == true)
+    *hash_data_size -= append_sig_len;

Please comment this "if"…

Sure, will add comments.

Quoted 16 lines
Quoted 4 lines
grub-core/commands/appendedsig/appendedsig.c +3 -0
+  return rc;
+}
+

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 968

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+static bool
+is_hash_present_in_db (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (grub_memcmp (db.signatures[i], hash_data, hash_data_size) == 0)
+        return true;
+    }

You can drop curly braces here…

Will do it.

Quoted 16 lines
Quoted 4 lines
grub-core/commands/appendedsig/appendedsig.c +3 -0
+  return false;
+}
+

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 982

Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+static bool
+is_hash_present_in_dbx (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < dbx.signature_entries; i++)
+    {
+      if (grub_memcmp (dbx.signatures[i], hash_data, hash_data_size) == 0)
+        return true;
+    }

Ditto…

Will do it.

Quoted 15 lines
Quoted 15 lines
grub-core/commands/appendedsig/appendedsig.c +14 -0
+  return false;
+}
+
+static void
+remove_hash_from_db (const grub_uint8_t *hash_data, const grub_size_t hash_data_size)
+{
+  int i;
+
+  for (i = 0; i < db.signature_entries; i++)
+    {
+      if (grub_memcmp (db.signatures[i], hash_data, hash_data_size) == 0)
+        {
+          grub_dprintf ("appendedsig", "removed distrusted hash %02x%02x%02x%02x.. from the db list\n",
+                        db.signatures[i][0], db.signatures[i][1], db.signatures[i][2],
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 1003

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+                        db.signatures[i][3]);
+          grub_free (db.signatures[i]);
+          db.signatures[i] = NULL;
+          db.signature_size[i] = 0;
+          break;
+ }

Something is off with indention here…

Will correct it.

Quoted 43 lines
Quoted 43 lines
grub-core/commands/appendedsig/appendedsig.c +42 -0
+    }
+}
+
+static grub_err_t
+grub_cmd_db_hash (grub_command_t cmd __attribute__((unused)), int argc, char**args)
+{
+  grub_err_t rc;
+  grub_uint8_t *hash_data = NULL;
+  grub_size_t hash_data_size = 0;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                       "a trusted binary hash file is expected in ASCII text format\n"
+                       "Example:\n\tappend_add_db_hash <BINARY HASH FILE>\n");
+
+  if (*args == NULL)
+    return grub_error (GRUB_ERR_BAD_FILENAME, "missing trusted binary hash file");
+
+  rc = read_hash_from_file (args[0], &hash_data, &hash_data_size);
+  if (rc == GRUB_ERR_NONE)
+    {
+      grub_dprintf ("appendedsig",
+                    "adding a trusted binary hash %02x%02x%02x%02x...\n with size of %" PRIuGRUB_SIZE "\n",
+                    hash_data[0], hash_data[1], hash_data[2], hash_data[3], hash_data_size);
+
+      /* Only accept SHA256, SHA384 and SHA512 binary hash */
+      if (hash_data_size != 32 && hash_data_size != 48 && hash_data_size != 64)
+        {
+          grub_free (hash_data);
+          return grub_error (GRUB_ERR_BAD_SIGNATURE, "unacceptable trusted binary hash type");
+        }
+
+      if (is_hash_present_in_dbx (hash_data, hash_data_size) == true)
+        {
+          grub_free (hash_data);
+          return grub_error (GRUB_ERR_STILL_REFERENCED,
+                             "could not add trusted binary hash, as it is present in the dbx list");
+        }
+
+      if (is_hash_present_in_db (hash_data, hash_data_size) == false)
+        {
+          rc = add_hash ((const grub_uint8_t **) &hash_data, hash_data_size, &db.signatures,
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 1050

Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +5 -0
+                         &db.signature_size, &db.signature_entries);
+          if (rc != GRUB_ERR_NONE)
+            {
+              free_db_list ();
+              free_dbx_list ();

Again, please do not free partial db/dbx... I think this should not be done in almost everywhere…

Sure, will do it.

Thanks,
Sudhakar

Quoted 11 lines
Quoted 11 lines
grub-core/commands/appendedsig/appendedsig.c +10 -0
+              rc = grub_error (rc, "adding of trusted binary hash failed");
+            }
+          else
+            grub_dprintf ("appendedsig",
+                          "added trusted binary hash %02x%02x%02x%02x...\n to the db list\n",
+                          hash_data[0], hash_data[1], hash_data[2], hash_data[3]);
+         }
+       else
+         rc = grub_error (GRUB_ERR_STILL_REFERENCED,
+                          "could not add trusted binary hash, as it is present in the db list");
Quoted 8 lines

grub-core/commands/appendedsig/appendedsig.c grub_cmd_list_db (grub_command_t cmd __attribute__ ((unused)), int argc __attrib · line 1066

Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6 -0
+    }
+
+  grub_free (hash_data);
+
+  return rc;
+}

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#33 lore raw

PATCH v6 17/20 appended signatures: Verification tests

In reply to Sudhakar Kuppusamy

These tests are run through all_functional_test and test a range of commands and behaviours.

---
 grub-core/Makefile.core.def               |   7 +
 grub-core/tests/appended_signature_test.c | 249 ++++++
 grub-core/tests/appended_signatures.h     | 975 ++++++++++++++++++++++
 grub-core/tests/lib/functional_test.c     |   5 +
 4 files changed, 1236 insertions(+)
 create mode 100644 grub-core/tests/appended_signature_test.c
 create mode 100644 grub-core/tests/appended_signatures.h
grub-core/Makefile.core.def +7 -0
index f1068eadd..3e8ccea38 100644
@@ -2213,6 +2213,13 @@ module = {
   cppflags = '$(CPPFLAGS_GCRY)';
 };
 
+module = {
+  name = appended_signature_test;
+  common = tests/appended_signature_test.c;
+  common = tests/appended_signatures.h;
+  enable = powerpc_ieee1275;
+};
+
 module = {
   name = signature_test;
   common = tests/signature_test.c;
grub-core/tests/appended_signature_test.c new file +249 -0

249 lines not on this page · raw message

grub-core/tests/appended_signatures.h new file +975 -0

975 lines not on this page · raw message

grub-core/tests/lib/functional_test.c +5 -0
index 38e981f2c..e71f53758 100644
@@ -73,6 +73,11 @@ grub_functional_all_tests (grub_extcmd_context_t ctxt __attribute__ ((unused)),
   grub_dl_load ("xnu_uuid_test");
   grub_dl_load ("pbkdf2_test");
   grub_dl_load ("signature_test");
+
+#if defined(__powerpc__)
+  grub_dl_load ("appended_signature_test");
+#endif
+
   grub_dl_load ("sleep_test");
   grub_dl_load ("bswap_test");
   grub_dl_load ("ctz_test");
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#34 lore raw

PATCH v6 18/20 docs/grub: Document signing GRUB under UEFI

In reply to Sudhakar Kuppusamy

From: Daniel Axtens <[email protected]>

Before adding information about how GRUB is signed with an appended signature scheme, it's worth adding some information about how it can currently be signed for UEFI.

---
 docs/grub.texi | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
docs/grub.texi +17 -1
index 15f087f27..72ee8d08c 100644
@@ -8675,6 +8675,7 @@ environment variables and commands are listed in the same order.
 * Measured Boot::                      Measuring boot components
 * Lockdown::                           Lockdown when booting on a secure setup
 * TPM2 key protector::                 Managing disk key with TPM2 key protector
+* Signing GRUB itself::                Ensuring the integrity of the GRUB core image
 @end menu
 
 @node Authentication and authorisation
@@ -8755,7 +8756,7 @@ commands.
 
 GRUB's @file{core.img} can optionally provide enforcement that all files
 subsequently read from disk are covered by a valid digital signature.
-This document does @strong{not} cover how to ensure that your
+This section does @strong{not} cover how to ensure that your
 platform's firmware (e.g., Coreboot) validates @file{core.img}.
 
 If environment variable @code{check_signatures}
@@ -9363,6 +9364,21 @@ which increases the risk of password leakage during the process. Moreover, the
 superuser list must be well maintained, and the password used cannot be
 synchronized with LUKS key rotation.
 
+@node Signing GRUB itself
+@section Signing GRUB itself
+To ensure a complete secure-boot chain, there must be a way for the code that
+loads GRUB to verify the integrity of the core image.
+This is ultimately platform-specific and individual platforms can define their
+own mechanisms. However, there are general-purpose mechanisms that can be used
+with GRUB.
+@section Signing GRUB for UEFI secure boot
+On UEFI platforms, @file{core.img} is a PE binary. Therefore, it can be signed
+with a tool such as @command{pesign} or @command{sbsign}. Refer to the
+suggestions in @pxref{UEFI secure boot and shim} to ensure that the final
+image works under UEFI secure boot and can maintain the secure-boot chain. It
+will also be necessary to enroll the public key used into a relevant firmware
+key database.
+
 @node Platform limitations
 @chapter Platform limitations
 
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#35 lore raw

PATCH v6 19/20 docs/grub: Document signing GRUB with an appended signature

In reply to Sudhakar Kuppusamy

Signing GRUB for firmware that verifies an appended signature is a bit fiddly. I don't want people to have to figure it out from scratch so document it here.

---
 docs/grub.texi | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
docs/grub.texi +98 -0
index 72ee8d08c..2ff867cc5 100644
@@ -9379,6 +9379,104 @@ image works under UEFI secure boot and can maintain the secure-boot chain. It
 will also be necessary to enroll the public key used into a relevant firmware
 key database.
 
+@section Signing GRUB with an appended signature
+The @file{core.elf} itself can be signed with a Linux kernel module-style
+appended signature.
+To support IEEE1275 platforms where the boot image is often loaded directly
+from a disk partition rather than from a file system, the @file{core.elf}
+can specify the size and location of the appended signature with an ELF
+Note added by @command{grub-install} or @command{grub-mkimage}.
+An image can be signed this way using the @command{sign-file} command from
+the Linux kernel:
+
+@itemize
+@item Signing a GRUB image using single signer key. The grub.key is your
+private key, certificate.der is your GRUB signing public key, and
+kernel.der is your kernel signing public key.
+@example
+@group
+# Determine the size of the appended signature. It depends on the
+# signing certificate and the hash algorithm.
+#
+# Signing the /dev/null with an appended signature.
+
+sign-file SHA256 grub.key certificate.der /dev/null ./empty.sig
+
+# Get the size of the signature.
+
+EMPTY_SIG_SIZE=`stat -c '%s' ./empty.sig`
+
+# Remove the empty file signature.
+
+rm ./empty.sig
+
+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature.
+
+grub-install --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+                      or
+grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der \
+  -p /grub --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+
+# Signing a GRUB image with an appended signature.
+
+sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed
+
+@end group
+@end example
+@item Signing a GRUB image using more than one signer key. The grub1.key and
+grub2.key are your private keys, certificate1.der and certificate2.der
+are your GRUB signing public keys. kernel.der and kernel2.der are your
+kernel signing public key.
+@example
+@group
+# Generate a raw signature for /dev/null signing using OpenSSL.
+
+openssl cms -sign -binary -nocerts -in /dev/null -signer \
+  certificate1.pem -inkey grub1.key -signer certificate2.pem \
+  -inkey grub2.key -out ./empty.p7s -outform DER -noattr -md sha256
+
+# Signing the /dev/null with an appended signature.
+
+sign-file -s ./empty.p7s sha256 /dev/null /dev/null ./empty.signed
+
+# Get the size of the signature.
+
+EMPTY_SIG_SIZE=`stat -c '%s' ./empty.signed`
+
+# Remove the empty file signatures.
+
+rm ./empty.signed ./empty.p7s
+
+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature.
+
+grub-install --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+                         or
+grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der \
+  -p /grub --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+
+# Generate a raw signature for GRUB image signing using OpenSSL.
+
+openssl cms -sign -binary -nocerts -in core.elf.unsigned -signer \
+  certificate.pem -inkey grub.key -signer certificate1.pem -inkey \
+  grub1.key -out core.p7s -outform DER -noattr -md sha256
+
+# Signing a GRUB image with an appended signature.
+
+sign-file -s core.p7s sha256 /dev/null core.elf.unsigned core.elf.signed
+
+@end group
+@end example
+@item Don't forget to install the signed image as required
+(e.g. on powerpc-ieee1275, to the PReP partition).
+@end itemize
+
+As with UEFI secure boot, it is necessary to build-in the required modules,
+or sign them separately.
+
 @node Platform limitations
 @chapter Platform limitations
 
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#36 lore raw

Re: [PATCH v6 19/20] docs/grub: Document signing GRUB with an appended signature

Quoted 23 lines · Sudhakar Kuppusamy

Signing GRUB for firmware that verifies an appended signature is a bit fiddly. I don't want people to have to figure it out from scratch so document it here.

---
 docs/grub.texi | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
docs/grub.texi +3 -0
index 72ee8d08c..2ff867cc5 100644
@@ -9379,6 +9379,104 @@ image works under UEFI secure boot and can maintain the secure-boot chain. It
 will also be necessary to enroll the public key used into a relevant firmware
 key database.
 
+@section Signing GRUB with an appended signature
+The @file{core.elf} itself can be signed with a Linux kernel module-style
+appended signature.

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9394

Quoted 10 lines
docs/grub.texi +10 -0
+To support IEEE1275 platforms where the boot image is often loaded directly
+from a disk partition rather than from a file system, the @file{core.elf}
+can specify the size and location of the appended signature with an ELF
+Note added by @command{grub-install} or @command{grub-mkimage}.
+An image can be signed this way using the @command{sign-file} command from
+the Linux kernel:
+
+@itemize
+@item Signing a GRUB image using single signer key. The grub.key is your
+private key, certificate.der is your GRUB signing public key, and

I would be more consistent and use grub.der instead of certificate.der here and below...

And I would propose updated sentence...

Signing a GRUB image using single signer key. The grub.key is your private key used for GRUB signing, grub.der is corresponding public key (certificate) used for GRUB signature verification, and kernel.der is your public key (certificate) used for kernel signature verification.

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9399

Quoted 5 lines
docs/grub.texi +5 -0
+kernel.der is your kernel signing public key.
+@example
+@group
+# Determine the size of the appended signature. It depends on the
+# signing certificate and the hash algorithm.

s/certificate/key (certificate)/

Quoted 22 lines
docs/grub.texi +22 -0
+#
+# Signing the /dev/null with an appended signature.
+
+sign-file SHA256 grub.key certificate.der /dev/null ./empty.sig
+
+# Get the size of the signature.
+
+EMPTY_SIG_SIZE=`stat -c '%s' ./empty.sig`
+
+# Remove the empty file signature.
+
+rm ./empty.sig
+
+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature.
+
+grub-install --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+                      or
+grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der \
+  -p /grub --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9431

Quoted 10 lines
docs/grub.texi +10 -0
+# Signing a GRUB image with an appended signature.
+
+sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed
+
+@end group
+@end example
+@item Signing a GRUB image using more than one signer key. The grub1.key and
+grub2.key are your private keys, certificate1.der and certificate2.der
+are your GRUB signing public keys. kernel.der and kernel2.der are your
+kernel signing public key.

These sentences are complete mess. Please fix them. You have an example how it should be done properly above...

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9437

Quoted 6 lines
docs/grub.texi +6 -0
+@example
+@group
+# Generate a raw signature for /dev/null signing using OpenSSL.
+
+openssl cms -sign -binary -nocerts -in /dev/null -signer \
+  certificate1.pem -inkey grub1.key -signer certificate2.pem \

You say *.der above and here is *.pem... Something is off... Same below... What about earlier examples?

Quoted 31 lines
docs/grub.texi +31 -0
+  -inkey grub2.key -out ./empty.p7s -outform DER -noattr -md sha256
+
+# Signing the /dev/null with an appended signature.
+
+sign-file -s ./empty.p7s sha256 /dev/null /dev/null ./empty.signed
+
+# Get the size of the signature.
+
+EMPTY_SIG_SIZE=`stat -c '%s' ./empty.signed`
+
+# Remove the empty file signatures.
+
+rm ./empty.signed ./empty.p7s
+
+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature.
+
+grub-install --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+                         or
+grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der \
+  -p /grub --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+
+# Generate a raw signature for GRUB image signing using OpenSSL.
+
+openssl cms -sign -binary -nocerts -in core.elf.unsigned -signer \
+  certificate.pem -inkey grub.key -signer certificate1.pem -inkey \
+  grub1.key -out core.p7s -outform DER -noattr -md sha256
+
+# Signing a GRUB image with an appended signature.
+

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9478

Quoted 10 lines
docs/grub.texi +10 -0
+sign-file -s core.p7s sha256 /dev/null core.elf.unsigned core.elf.signed
+
+@end group
+@end example
+@item Don't forget to install the signed image as required
+(e.g. on powerpc-ieee1275, to the PReP partition).
+@end itemize
+
+As with UEFI secure boot, it is necessary to build-in the required modules,
+or sign them separately.

s/separately/if they are not part of the GRUB image/

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#37 lore raw

Re: [PATCH v6 19/20] docs/grub: Document signing GRUB with an appended signature

Thank you Daniel.

Quoted 40 lines · Daniel Kiper
Quoted 23 lines · Sudhakar Kuppusamy

Signing GRUB for firmware that verifies an appended signature is a bit fiddly. I don't want people to have to figure it out from scratch so document it here.

---
docs/grub.texi | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 98 insertions(+)
docs/grub.texi +0 -0
index 72ee8d08c..2ff867cc5 100644
@@ -9379,6 +9379,104 @@ image works under UEFI secure boot and can maintain the secure-boot chain. It

will also be necessary to enroll the public key used into a relevant firmware key database.

+@section Signing GRUB with an appended signature

docs/grub.texi +2 -0
+The @file{core.elf} itself can be signed with a Linux kernel module-style
+appended signature.

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9394

Quoted 10 lines
docs/grub.texi +10 -0
+To support IEEE1275 platforms where the boot image is often loaded directly
+from a disk partition rather than from a file system, the @file{core.elf}
+can specify the size and location of the appended signature with an ELF
+Note added by @command{grub-install} or @command{grub-mkimage}.
+An image can be signed this way using the @command{sign-file} command from
+the Linux kernel:
+
+@itemize
+@item Signing a GRUB image using single signer key. The grub.key is your
+private key, certificate.der is your GRUB signing public key, and

I would be more consistent and use grub.der instead of certificate.der here and below...

Quoted 6 lines

And I would propose updated sentence...

Signing a GRUB image using single signer key. The grub.key is your private key used for GRUB signing, grub.der is corresponding public key (certificate) used for GRUB signature verification, and kernel.der is your public key (certificate) used for kernel signature verification.

Sure, will do it. Thank you Daniel for the updated sentence.

Quoted 8 lines

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9399

Quoted 6 lines
docs/grub.texi +5 -0
+kernel.der is your kernel signing public key.
+@example
+@group
+# Determine the size of the appended signature. It depends on the
+# signing certificate and the hash algorithm.

s/certificate/key (certificate)/

Will do it.

Quoted 28 lines
Quoted 28 lines
docs/grub.texi +27 -0
+#
+# Signing the /dev/null with an appended signature.
+
+sign-file SHA256 grub.key certificate.der /dev/null ./empty.sig
+
+# Get the size of the signature.
+
+EMPTY_SIG_SIZE=`stat -c '%s' ./empty.sig`
+
+# Remove the empty file signature.
+
+rm ./empty.sig
+
+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature.
+
+grub-install --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+                      or
+grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der \
+  -p /grub --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+
+# Signing a GRUB image with an appended signature.
+
+sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed
+
+@end group
Quoted 8 lines

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9431

Quoted 5 lines

+@end example
+@item Signing a GRUB image using more than one signer key. The grub1.key and

docs/grub.texi +3 -0
+grub2.key are your private keys, certificate1.der and certificate2.der
+are your GRUB signing public keys. kernel.der and kernel2.der are your
+kernel signing public key.

These sentences are complete mess. Please fix them. You have an example how it should be done properly above…

Sure, will do it.

Quoted 10 lines

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9437

Quoted 7 lines

+@example
+@group

docs/grub.texi +4 -0
+# Generate a raw signature for /dev/null signing using OpenSSL.
+
+openssl cms -sign -binary -nocerts -in /dev/null -signer \
+  certificate1.pem -inkey grub1.key -signer certificate2.pem \

You say *.der above and here is *.pem... Something is off... Same below... What about earlier examples?

Oh sorry typo error. Will correct it.

I am documenting two examples for GRUB image signing, which are Core Image signing with a single signer key and Core Image signing with more than one signer key. An earlier example is GRUB image signing using a single signer key.
This example is GRUB image signing using more than one signer key.

Quoted 36 lines
Quoted 36 lines
docs/grub.texi +35 -0
+  -inkey grub2.key -out ./empty.p7s -outform DER -noattr -md sha256
+
+# Signing the /dev/null with an appended signature.
+
+sign-file -s ./empty.p7s sha256 /dev/null /dev/null ./empty.signed
+
+# Get the size of the signature.
+
+EMPTY_SIG_SIZE=`stat -c '%s' ./empty.signed`
+
+# Remove the empty file signatures.
+
+rm ./empty.signed ./empty.p7s
+
+# Build a GRUB image with $EMPTY_SIG_SIZE reserved for the signature.
+
+grub-install --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+                         or
+grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der \
+  -p /grub --appended-signature-size $EMPTY_SIG_SIZE \
+  --modules="appendedsig ..." ...
+
+# Generate a raw signature for GRUB image signing using OpenSSL.
+
+openssl cms -sign -binary -nocerts -in core.elf.unsigned -signer \
+  certificate.pem -inkey grub.key -signer certificate1.pem -inkey \
+  grub1.key -out core.p7s -outform DER -noattr -md sha256
+
+# Signing a GRUB image with an appended signature.
+
+sign-file -s core.p7s sha256 /dev/null core.elf.unsigned core.elf.signed
+
+@end group
+@end example
Quoted 8 lines

docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9478

Quoted 6 lines

+@item Don't forget to install the signed image as required

docs/grub.texi +5 -0
+(e.g. on powerpc-ieee1275, to the PReP partition).
+@end itemize
+
+As with UEFI secure boot, it is necessary to build-in the required modules,
+or sign them separately.

s/separately/if they are not part of the GRUB image/

Will do it.

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#38 lore raw

PATCH v6 20/20 docs/grub: Document appended signature

In reply to Sudhakar Kuppusamy

This explains how appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.

---
 docs/grub.texi | 232 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 232 insertions(+)
docs/grub.texi +232 -0
index 2ff867cc5..0301b164b 100644
@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB.
 
 @menu
 * biosnum::
+* check_appended_signatures::
 * check_signatures::
 * chosen::
 * cmdpath::
@@ -3343,6 +3344,10 @@ this.
 For an alternative approach which also changes BIOS drive mappings for the
 chain-loaded system, @pxref{drivemap}.
 
+@node check_appended_signatures
+@subsection check_appended_signatures
+This variable controls whether GRUB enforces appended signature validation on
+loaded kernel and GRUB module files. @xref{Using appended signatures}.
 
 @node check_signatures
 @subsection check_signatures
@@ -6414,6 +6419,13 @@ you forget a command, you can run the command @command{help}
 @menu
 * [::                           Check file types and compare values
 * acpi::                        Load ACPI tables
+* append_add_db_cert::          Add trusted certificate to the db list
+* append_add_db_hash::          Add trusted certificate/binary hash to the db list
+* append_add_dbx_cert::         Add distrusted certificate to the dbx list
+* append_add_dbx_hash::         Add distrusted certificate/binary hash to the dbx list
+* append_list_db::              List all trusted certificates from the db list
+* append_list_dbx::             List all distrusted certificates and binary/certificate hashes from the dbx list
+* append_verify::               Verify appended digital signature using db and dbx lists
 * authenticate::                Check whether user is in user list
 * background_color::            Set background color for active terminal
 * background_image::            Load background image for active terminal
@@ -6535,6 +6547,120 @@ Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}).
       unsigned code.
 @end deffn
 
+@node append_add_db_cert
+@subsection append_add_db_cert
+
+@deffn Command append_add_db_cert <X509_certificate>
+Read DER-formatted X.509 certificate from the file @var{X509_certificate}
+and add it to GRUB's internal db list of trusted certificates.
+These certificates are used to validate appended signatures when the
+environment variable @code{check_appended_signatures} (@pxref{check_appended_signatures})
+is set to @code{enforce} or the @command{append_verify} (@pxref{append_verify})
+command is executed from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_db_cert} executes, then @var{X509_certificate} must
+be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_add_db_hash
+@subsection append_add_db_hash
+
+@deffn Command append_add_db_hash <hash_file>
+Read ASCII text formatted binary hash from the file @var{hash_file}
+and add it to GRUB's internal db list of trusted binary hashes. These
+hashes are used to validate the Linux kernel binary hashes when the
+environment variable @code{check_appended_signatures}
+(@pxref{check_appended_signatures}) is set to @code{enforce} or the
+@command{append_verify} (@pxref{append_verify}) command is executed
+from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_db_hash} executes, then @var{hash_file}
+must be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_add_dbx_cert
+@subsection append_add_dbx_cert
+
+@deffn Command append_add_dbx_cert <X509_certificate>
+Read DER-formatted X.509 certificate from the file @var{X509_certificate}
+and add it to GRUB's internal dbx list of distrusted certificates.
+These certificates are used to block adding the distrusted certificates to
+the db list in the future and also ensure that the distrusted certificates
+are not used for appended signatures validation when the environment variable
+@code{check_appended_signatures} is set to @code{enforce}
+(@pxref{check_appended_signatures}) or the @command{append_verify}
+(@pxref{append_verify}) command is executed from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_dbx_cert} executes, then @var{X509_certificate} must
+be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_add_dbx_hash
+@subsection append_add_dbx_hash
+
+@deffn Command append_add_dbx_hash [@option{-b}|@option{-c}] <hash_file>
+Read ASCII text formatted binary/certificate hash from the file @var{hash_file}
+and add it to GRUB's internal dbx list of distrusted binary/certificate hashes.
+These hashes are used to block adding the distrusted binary hashes and
+certificates to the db list in the future, and also ensure that the distrusted
+binary hashes/certificates are not used for Linux kernel binary hashes and
+appended signatures validation when the environment variable
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) is set to
+@code{enforce} or the @command{append_verify} (@pxref{append_verify}) command
+is executed from the GRUB console.
+
+The @option{-b} (@option{--binary-hash}) can be used to specify binary hash file and
+@option{-c} (@option{--cert-hash}) can be used to specify certificate hash file..
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_dbx_sig} executes, then @var{hash_file} must be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_list_db
+@subsection append_list_db
+
+@deffn Command append_list_db
+List all X.509 certificates and binary hashes trusted by GRUB for validating
+appended signatures. The output is a numbered list of certificates and binary hashes,
+showing the certificate's serial number, issuer and Common Name.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_list_dbx
+@subsection append_list_dbx
+
+@deffn Command append_list_dbx
+List all the distrusted X.509 certificates and binary/certificate hashes.
+The output is a numbered list of certificates and binary/certificate hashes,
+showing the certificate's serial number, issuer and Common Name.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_verify
+@subsection append_verify
+
+@deffn Command append_verify <signed_file>
+Verifies an appended signature on @var{signed_file} against the trusted X.509 certificates
+known to GRUB (@pxref{append_list_db},@pxref{append_list_dbx}, @pxref{append_add_db_cert},
+@pxref{append_add_db_hash}, @pxref{append_add_dbx_hash}, and @pxref{append_add_dbx_cert}).
+Exit code @code{$?} is set to 0 if the signature validates successfully.
+If validation fails, it is set to a non-zero value.
+
+@xref{Using appended signatures}, for more information.
+@end deffn
 
 @node authenticate
 @subsection authenticate
@@ -7307,6 +7433,13 @@ configurations, but to allow the user to select from among multiple
 configurations, and to enable ``one-shot'' boot attempts and
 ``savedefault'' behavior.  @xref{Using GPG-style digital signatures}, for more
 information.
+
+When combining this command with appended signatures (@pxref{Using appended signatures}),
+not allowed to change the value of environment variable @code{check_appended_signatures}
+to @code{no} or @code{enforce} even with the @option{--skip-sig} option
+when the environment variable @code{check_appended_signatures}
+(@pxref{check_appended_signatures}) is set to @code{enforce} and GRUB is locked down.
+However, the environment block file is not validated by an appended signature.
 @end deffn
 
 
@@ -8670,6 +8803,7 @@ environment variables and commands are listed in the same order.
 @menu
 * Authentication and authorisation::   Users and access control
 * Using GPG-style digital signatures:: Booting digitally signed code
+* Using appended signatures::          An alternative approach to booting digitally signed code
 * UEFI secure boot and shim::          Booting digitally signed PE files
 * Secure Boot Advanced Targeting::     Embedded information for generation number based revocation
 * Measured Boot::                      Measuring boot components
@@ -8835,6 +8969,104 @@ or BIOS) configuration to cause the machine to boot from a different
 (attacker-controlled) device.  GRUB is at best only one link in a
 secure boot chain.
 
+@node Using appended signatures
+@section Using appended signatures in GRUB
+
+GRUB supports verifying Linux-style 'appended signatures' for Linux on Power LPAR
+secure boot. Appended signatures are PKCS#7 messages containing a signature over the
+contents of a file, plus some metadata, appended to the end of a file. A file
+with an appended signature ends with the magic string:
+
+@example
+~Module signature appended~\n
+@end example
+
+where @code{\n} represents the line feed character, @code{0x0a}.
+
+Linux on Power LPAR secure boot is controlled by @strong{'ibm,secure-boot'}
+device tree property and if this property is set to @code{2} (@samp{2 - enforced}),
+GRUB enters lockdown. There are three secure boot modes. They are
+
+@itemize
+@item @samp{0 - disabled}: Secure boot is disabled. This is the default.
+@item @samp{1 - audit}: Enforce signature verification by setting
+      @code{check_appended_signatures} (@pxref{check_appended_signatures}) to
+      @code{enforce} and not to lockdown the GRUB.
+@item @samp{2 - enforced}: Lockdown the GRUB and enforce signature verification by setting
+      @code{check_appended_signatures} (@pxref{check_appended_signatures}) to @code{enforce}.
+@end itemize
+
+Note that Linux on Power LPAR only @strong{supports disabled and enforced}.
+
+To enable appended signature verification, load the appendedsig module and an
+X.509 certificate for verification. Building the appendedsig module into the
+core GRUB image is recommended.
+
+In static key management mode, certificates will be built into the core image using
+the @code{--x509} parameter to @command{grub-install} or @command{grub-mkimage}.
+It allows listing the trusted certificates at boot time using @command{append_list_db}
+(@pxref{append_list_db}).
+
+In dynamic key management mode, db and dbx are read from the Platform KeyStore(PKS). If
+db is not present in PKS, static key (built-in keys) is used as the default key.
+It allows listing the trusted certificates and binary hashes at boot time using
+@command{append_list_dbx} (@pxref{append_list_db}) and listing distrusted
+certificates and binary/certificate hashes at boot time using @command{append_list_dbx}
+(@pxref{append_list_dbx}).
+
+Enforcement of signature verification is controlled by the
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) variable.
+
+@itemize
+@item @samp{no}: No verification is performed. This is the default.
+@item @samp{enforce}: Signature verification is performed and if signature verification fails,
+      post the errors and stop the boot. Signature verification cannot be disabled by setting
+      the @code{check_appended_signatures} variable back to @samp{no}.
+@end itemize
+
+A file can be explicitly verified using the @command{append_verify} (@pxref{append_verify}).
+The trusted certificates and binary hashes can be explicitly added using the
+@command{append_add_db_cert} (@pxref{append_add_db_cert}) and @command{append_add_db_hash}
+(@pxref{append_add_db_hash}). The distrusted certificates can be explicitly added using
+the @command{append_add_dbx_cert} (@pxref{append_add_dbx_cert}) and the distrusted
+certificate/binary hases can be explicitly addded using @command{append_add_dbx_hash}
+(@pxref{append_add_dbx_hash}).
+
+Only signatures generated using SHA-256 or SHA-512 hash algorithms are supported,
+and only RSA signatures generated using 2048, 3076, or 4096 bit key are supported.
+Only binary/certificate hash generated using SHA-256, SHA-384, or SHA-512 hash
+algorithms are supported.
+
+A file can be signed with the @command{sign-file} utility supplied with the
+Linux kernel source. For example, if you have @code{signing.key} as the private
+key and @code{certificate.der} as the X.509 certificate containing the public key:
+
+@example
+sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed
+@end example
+
+Once signature verification is turned on, the following file types must carry
+appended signatures:
+
+@enumerate
+@item Linux kernels
+@item GRUB modules, except those built in to the core image
+@item Any new certificate and binary hash files to be trusted
+@item Any new certificate/binary hash files to be distrusted
+@end enumerate
+
+When GRUB is locked down (when secure boot modes is @code{enforced}),
+signature verification cannot be @strong{disabled} by setting the
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) variable
+to @code{no} or using the @command{load_env} (@pxref{load_env}) command from
+the GRUB console.
+
+@example
+set check_appended_signatures=no
+        or
+load_env --file grubenv --skip-sig
+@end example
+
 @node UEFI secure boot and shim
 @section UEFI secure boot and shim support
 
Signature · 7 lines
2.39.5 (Apple Git-154)


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#39 lore raw

Re: [PATCH v6 20/20] docs/grub: Document appended signature

Quoted 24 lines · Sudhakar Kuppusamy

This explains how appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.

---
 docs/grub.texi | 232 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 232 insertions(+)
docs/grub.texi +1 -0
index 2ff867cc5..0301b164b 100644
@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB.
 
 @menu
 * biosnum::
+* check_appended_signatures::
 * check_signatures::
 * chosen::
 * cmdpath::

docs/grub.texi this. · line 3350

Quoted 7 lines
docs/grub.texi +4 -0
@@ -3343,6 +3344,10 @@ this.
 For an alternative approach which also changes BIOS drive mappings for the
 chain-loaded system, @pxref{drivemap}.
 
+@node check_appended_signatures
+@subsection check_appended_signatures
+This variable controls whether GRUB enforces appended signature validation on
+loaded kernel and GRUB module files. @xref{Using appended signatures}.

What are valid values for this variable and what are they meaning? OK, I can see this below. Though I think at least listing allowed values makes sense here...

Quoted 24 lines
docs/grub.texi +11 -0
 @node check_signatures
 @subsection check_signatures
@@ -6414,6 +6419,13 @@ you forget a command, you can run the command @command{help}
 @menu
 * [::                           Check file types and compare values
 * acpi::                        Load ACPI tables
+* append_add_db_cert::          Add trusted certificate to the db list
+* append_add_db_hash::          Add trusted certificate/binary hash to the db list
+* append_add_dbx_cert::         Add distrusted certificate to the dbx list
+* append_add_dbx_hash::         Add distrusted certificate/binary hash to the dbx list
+* append_list_db::              List all trusted certificates from the db list
+* append_list_dbx::             List all distrusted certificates and binary/certificate hashes from the dbx list
+* append_verify::               Verify appended digital signature using db and dbx lists
 * authenticate::                Check whether user is in user list
 * background_color::            Set background color for active terminal
 * background_image::            Load background image for active terminal
@@ -6535,6 +6547,120 @@ Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}).
       unsigned code.
 @end deffn
 
+@node append_add_db_cert
+@subsection append_add_db_cert
+
+@deffn Command append_add_db_cert <X509_certificate>

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6563

Quoted 10 lines
docs/grub.texi +10 -0
+Read DER-formatted X.509 certificate from the file @var{X509_certificate}
+and add it to GRUB's internal db list of trusted certificates.
+These certificates are used to validate appended signatures when the
+environment variable @code{check_appended_signatures} (@pxref{check_appended_signatures})
+is set to @code{enforce} or the @command{append_verify} (@pxref{append_verify})
+command is executed from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_db_cert} executes, then @var{X509_certificate} must
+be properly signed.

What does it mean "properly signed"? By whom? How the signature is verified and when it is considered valid?

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6572

Quoted 8 lines
docs/grub.texi +8 -0
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_add_db_hash
+@subsection append_add_db_hash
+
+@deffn Command append_add_db_hash <hash_file>
+Read ASCII text formatted binary hash from the file @var{hash_file}

What do you mean by "ASCII text formatted"? PEM? If yes then use phrase "in PEM format" or something like that...

And I am not sure why sometimes you use PEM and sometimes DER. Is it possible to stick to one format to avoid confusion?

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6582

Quoted 10 lines
docs/grub.texi +10 -0
+and add it to GRUB's internal db list of trusted binary hashes. These
+hashes are used to validate the Linux kernel binary hashes when the
+environment variable @code{check_appended_signatures}
+(@pxref{check_appended_signatures}) is set to @code{enforce} or the
+@command{append_verify} (@pxref{append_verify}) command is executed
+from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_db_hash} executes, then @var{hash_file}
+must be properly signed.

Again, I think you should be precise what "properly signed" means. Though it is OK to write it down once and refer to it later in the documentation...

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6595

Quoted 12 lines
docs/grub.texi +12 -0
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_add_dbx_cert
+@subsection append_add_dbx_cert
+
+@deffn Command append_add_dbx_cert <X509_certificate>
+Read DER-formatted X.509 certificate from the file @var{X509_certificate}
+and add it to GRUB's internal dbx list of distrusted certificates.
+These certificates are used to block adding the distrusted certificates to
+the db list in the future and also ensure that the distrusted certificates
+are not used for appended signatures validation when the environment variable

s/not used for/rejected during/

And I think rejection should be mentioned first...

Quoted 6 lines
docs/grub.texi +6 -0
+@code{check_appended_signatures} is set to @code{enforce}
+(@pxref{check_appended_signatures}) or the @command{append_verify}
+(@pxref{append_verify}) command is executed from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_dbx_cert} executes, then @var{X509_certificate} must

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6611

Quoted 10 lines
docs/grub.texi +10 -0
+be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_add_dbx_hash
+@subsection append_add_dbx_hash
+
+@deffn Command append_add_dbx_hash [@option{-b}|@option{-c}] <hash_file>
+Read ASCII text formatted binary/certificate hash from the file @var{hash_file}

Again, PEM...

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6615

Quoted 4 lines
docs/grub.texi +4 -0
+and add it to GRUB's internal dbx list of distrusted binary/certificate hashes.
+These hashes are used to block adding the distrusted binary hashes and
+certificates to the db list in the future, and also ensure that the distrusted
+binary hashes/certificates are not used for Linux kernel binary hashes and

s/not used for/rejected during/

And I think rejection should be mentioned first...

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6622

Quoted 7 lines
docs/grub.texi +7 -0
+appended signatures validation when the environment variable
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) is set to
+@code{enforce} or the @command{append_verify} (@pxref{append_verify}) command
+is executed from the GRUB console.
+
+The @option{-b} (@option{--binary-hash}) can be used to specify binary hash file and
+@option{-c} (@option{--cert-hash}) can be used to specify certificate hash file..

What are the formats for these files and how they should be generated?

Quoted 24 lines
docs/grub.texi +24 -0
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_dbx_sig} executes, then @var{hash_file} must be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_list_db
+@subsection append_list_db
+
+@deffn Command append_list_db
+List all X.509 certificates and binary hashes trusted by GRUB for validating
+appended signatures. The output is a numbered list of certificates and binary hashes,
+showing the certificate's serial number, issuer and Common Name.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_list_dbx
+@subsection append_list_dbx
+
+@deffn Command append_list_dbx
+List all the distrusted X.509 certificates and binary/certificate hashes.
+The output is a numbered list of certificates and binary/certificate hashes,
+showing the certificate's serial number, issuer and Common Name.

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6657

Quoted 10 lines
docs/grub.texi +10 -0
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_verify
+@subsection append_verify
+
+@deffn Command append_verify <signed_file>
+Verifies an appended signature on @var{signed_file} against the trusted X.509 certificates
+known to GRUB (@pxref{append_list_db},@pxref{append_list_dbx}, @pxref{append_add_db_cert},

s/known/and hashes known/

Quoted 10 lines
docs/grub.texi +6 -0
+@pxref{append_add_db_hash}, @pxref{append_add_dbx_hash}, and @pxref{append_add_dbx_cert}).
+Exit code @code{$?} is set to 0 if the signature validates successfully.
+If validation fails, it is set to a non-zero value.
+
+@xref{Using appended signatures}, for more information.
+@end deffn
 
 @node authenticate
 @subsection authenticate

docs/grub.texi configurations, but to allow the user to select from among multiple · line 7438

Quoted 6 lines
docs/grub.texi +3 -0
@@ -7307,6 +7433,13 @@ configurations, but to allow the user to select from among multiple
 configurations, and to enable ``one-shot'' boot attempts and
 ``savedefault'' behavior.  @xref{Using GPG-style digital signatures}, for more
 information.
+
+When combining this command with appended signatures (@pxref{Using appended signatures}),
+not allowed to change the value of environment variable @code{check_appended_signatures}

??? "not allowed"??? Something is missing here...

docs/grub.texi configurations, but to allow the user to select from among multiple · line 7442

Quoted 4 lines
docs/grub.texi +4 -0
+to @code{no} or @code{enforce} even with the @option{--skip-sig} option
+when the environment variable @code{check_appended_signatures}
+(@pxref{check_appended_signatures}) is set to @code{enforce} and GRUB is locked down.
+However, the environment block file is not validated by an appended signature.

I am not sure what you mean here. I think this paragraph should be rephrased...

Quoted 28 lines
docs/grub.texi +14 -0
 @end deffn
 
 
@@ -8670,6 +8803,7 @@ environment variables and commands are listed in the same order.
 @menu
 * Authentication and authorisation::   Users and access control
 * Using GPG-style digital signatures:: Booting digitally signed code
+* Using appended signatures::          An alternative approach to booting digitally signed code
 * UEFI secure boot and shim::          Booting digitally signed PE files
 * Secure Boot Advanced Targeting::     Embedded information for generation number based revocation
 * Measured Boot::                      Measuring boot components
@@ -8835,6 +8969,104 @@ or BIOS) configuration to cause the machine to boot from a different
 (attacker-controlled) device.  GRUB is at best only one link in a
 secure boot chain.
 
+@node Using appended signatures
+@section Using appended signatures in GRUB
+
+GRUB supports verifying Linux-style 'appended signatures' for Linux on Power LPAR
+secure boot. Appended signatures are PKCS#7 messages containing a signature over the
+contents of a file, plus some metadata, appended to the end of a file. A file
+with an appended signature ends with the magic string:
+
+@example
+~Module signature appended~\n
+@end example
+
+where @code{\n} represents the line feed character, @code{0x0a}.

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8994

Quoted 10 lines
docs/grub.texi +10 -0
+
+Linux on Power LPAR secure boot is controlled by @strong{'ibm,secure-boot'}
+device tree property and if this property is set to @code{2} (@samp{2 - enforced}),
+GRUB enters lockdown. There are three secure boot modes. They are
+
+@itemize
+@item @samp{0 - disabled}: Secure boot is disabled. This is the default.
+@item @samp{1 - audit}: Enforce signature verification by setting
+      @code{check_appended_signatures} (@pxref{check_appended_signatures}) to
+      @code{enforce} and not to lockdown the GRUB.

s/and not/and do not/

I think you are sometimes missing verbs... 🤔

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8999

Quoted 5 lines
docs/grub.texi +5 -0
+@item @samp{2 - enforced}: Lockdown the GRUB and enforce signature verification by setting
+      @code{check_appended_signatures} (@pxref{check_appended_signatures}) to @code{enforce}.
+@end itemize
+
+Note that Linux on Power LPAR only @strong{supports disabled and enforced}.

s/enforced/enforce/?

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9007

Quoted 7 lines
docs/grub.texi +7 -0
+To enable appended signature verification, load the appendedsig module and an
+X.509 certificate for verification. Building the appendedsig module into the
+core GRUB image is recommended.
+
+In static key management mode, certificates will be built into the core image using
+the @code{--x509} parameter to @command{grub-install} or @command{grub-mkimage}.
+It allows listing the trusted certificates at boot time using @command{append_list_db}

You can list the trusted certificates available at boot time using...

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9012

Quoted 5 lines
docs/grub.texi +5 -0
+(@pxref{append_list_db}).
+
+In dynamic key management mode, db and dbx are read from the Platform KeyStore(PKS). If
+db is not present in PKS, static key (built-in keys) is used as the default key.
+It allows listing the trusted certificates and binary hashes at boot time using

s/allows listing/is possible to list/...
s/at boot/available at boot/

Similar changes are worth doing here and there...

Quoted 6 lines
docs/grub.texi +6 -0
+@command{append_list_dbx} (@pxref{append_list_db}) and listing distrusted
+certificates and binary/certificate hashes at boot time using @command{append_list_dbx}
+(@pxref{append_list_dbx}).
+
+Enforcement of signature verification is controlled by the
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) variable.

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9028

Quoted 10 lines
docs/grub.texi +10 -0
+
+@itemize
+@item @samp{no}: No verification is performed. This is the default.
+@item @samp{enforce}: Signature verification is performed and if signature verification fails,
+      post the errors and stop the boot. Signature verification cannot be disabled by setting
+      the @code{check_appended_signatures} variable back to @samp{no}.
+@end itemize
+
+A file can be explicitly verified using the @command{append_verify} (@pxref{append_verify}).
+The trusted certificates and binary hashes can be explicitly added using the

"added" to what?

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9030

Quoted 2 lines
docs/grub.texi +2 -0
+@command{append_add_db_cert} (@pxref{append_add_db_cert}) and @command{append_add_db_hash}
+(@pxref{append_add_db_hash}). The distrusted certificates can be explicitly added using

Ditto...

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9032

Quoted 2 lines
docs/grub.texi +2 -0
+the @command{append_add_dbx_cert} (@pxref{append_add_dbx_cert}) and the distrusted
+certificate/binary hases can be explicitly addded using @command{append_add_dbx_hash}

Ditto...

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9037

Quoted 5 lines
docs/grub.texi +5 -0
+(@pxref{append_add_dbx_hash}).
+
+Only signatures generated using SHA-256 or SHA-512 hash algorithms are supported,
+and only RSA signatures generated using 2048, 3076, or 4096 bit key are supported.
+Only binary/certificate hash generated using SHA-256, SHA-384, or SHA-512 hash

s/SHA-512 hash/SHA-512/

Quoted 21 lines
docs/grub.texi +21 -0
+algorithms are supported.
+
+A file can be signed with the @command{sign-file} utility supplied with the
+Linux kernel source. For example, if you have @code{signing.key} as the private
+key and @code{certificate.der} as the X.509 certificate containing the public key:
+
+@example
+sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed
+@end example
+
+Once signature verification is turned on, the following file types must carry
+appended signatures:
+
+@enumerate
+@item Linux kernels
+@item GRUB modules, except those built in to the core image
+@item Any new certificate and binary hash files to be trusted
+@item Any new certificate/binary hash files to be distrusted
+@end enumerate
+
+When GRUB is locked down (when secure boot modes is @code{enforced}),

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9068

Quoted 10 lines
docs/grub.texi +10 -0
+signature verification cannot be @strong{disabled} by setting the
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) variable
+to @code{no} or using the @command{load_env} (@pxref{load_env}) command from
+the GRUB console.
+
+@example
+set check_appended_signatures=no
+        or
+load_env --file grubenv --skip-sig
+@end example

This example does not make a lot of sense for me...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#40 lore raw

Re: [PATCH v6 20/20] docs/grub: Document appended signature

Thank you Daniel.

Quoted 30 lines · Daniel Kiper
Quoted 27 lines · Sudhakar Kuppusamy

This explains how appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.

---
docs/grub.texi | 232 +++++++++++++++++++++++++++++++++++++++++++++++++

1 file changed, 232 insertions(+)
docs/grub.texi +0 -0
index 2ff867cc5..0301b164b 100644
@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB.
 

@menu
* biosnum::
+* check_appended_signatures::
* check_signatures::
* chosen::
* cmdpath::

docs/grub.texi +0 -0
@@ -3343,6 +3344,10 @@ this.

For an alternative approach which also changes BIOS drive mappings for the chain-loaded system, @pxref{drivemap}.

Quoted 8 lines

docs/grub.texi this. · line 3350

Quoted 4 lines

+@node check_appended_signatures
+@subsection check_appended_signatures

docs/grub.texi +2 -0
+This variable controls whether GRUB enforces appended signature validation on
+loaded kernel and GRUB module files. @xref{Using appended signatures}.

What are valid values for this variable and what are they meaning? OK, I can see this below. Though I think at least listing allowed values makes sense here…

Sure, will add it.

Quoted 30 lines
Quoted 30 lines

@node check_signatures
@subsection check_signatures

docs/grub.texi +0 -0
@@ -6414,6 +6419,13 @@ you forget a command, you can run the command @command{help}

@menu
* [:: Check file types and compare values
* acpi:: Load ACPI tables

docs/grub.texi +7 -0
+* append_add_db_cert::          Add trusted certificate to the db list
+* append_add_db_hash::          Add trusted certificate/binary hash to the db list
+* append_add_dbx_cert::         Add distrusted certificate to the dbx list
+* append_add_dbx_hash::         Add distrusted certificate/binary hash to the dbx list
+* append_list_db::              List all trusted certificates from the db list
+* append_list_dbx::             List all distrusted certificates and binary/certificate hashes from the dbx list
+* append_verify::               Verify appended digital signature using db and dbx lists
* authenticate::                Check whether user is in user list
* background_color::            Set background color for active terminal
* background_image::            Load background image for active terminal
docs/grub.texi +0 -0
@@ -6535,6 +6547,120 @@ Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}).
      unsigned code.

@end deffn

+@node append_add_db_cert
+@subsection append_add_db_cert

docs/grub.texi +7 -0
+
+@deffn Command append_add_db_cert <X509_certificate>
+Read DER-formatted X.509 certificate from the file @var{X509_certificate}
+and add it to GRUB's internal db list of trusted certificates.
+These certificates are used to validate appended signatures when the
+environment variable @code{check_appended_signatures} (@pxref{check_appended_signatures})
+is set to @code{enforce} or the @command{append_verify} (@pxref{append_verify})
Quoted 8 lines

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6563

Quoted 5 lines
docs/grub.texi +5 -0
+command is executed from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_db_cert} executes, then @var{X509_certificate} must
+be properly signed.

What does it mean "properly signed"? By whom? How the signature is verified and when it is considered valid?

When the appended signature verification is enforced and executes append_add_db_cert from the GRUB console, the x509 certificate must be signed with a sign-file utility, and
it will be verified by the appended signature module. If signature validation succeeds, then add it to the db.

Quoted 12 lines

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6572

Quoted 9 lines

+@xref{Using appended signatures} for more information. +@end deffn

docs/grub.texi +6 -0
+
+@node append_add_db_hash
+@subsection append_add_db_hash
+
+@deffn Command append_add_db_hash <hash_file>
+Read ASCII text formatted binary hash from the file @var{hash_file}

What do you mean by "ASCII text formatted"? PEM? If yes then use phrase "in PEM format" or something like that...

Here the hash file will contain hash data in ASCII text format, not DER or PEM format. Also, the DER or PEM format is only used for x509 certificates.
When executing the append_add_db_hash command from the GRUB console,
it read the binary hash data in ASCII text format and added it to db.

Quoted 3 lines

And I am not sure why sometimes you use PEM and sometimes DER. Is it possible to stick to one format to avoid confusion?

Sure, will stick to one format.

Quoted 15 lines

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6582

Quoted 11 lines
docs/grub.texi +10 -0
+and add it to GRUB's internal db list of trusted binary hashes. These
+hashes are used to validate the Linux kernel binary hashes when the
+environment variable @code{check_appended_signatures}
+(@pxref{check_appended_signatures}) is set to @code{enforce} or the
+@command{append_verify} (@pxref{append_verify}) command is executed
+from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_db_hash} executes, then @var{hash_file}
+must be properly signed.

Again, I think you should be precise what "properly signed" means. Though it is OK to write it down once and refer to it later in the documentation...

Sure, will do it.

Quoted 16 lines

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6595

Quoted 13 lines

+@xref{Using appended signatures} for more information. +@end deffn

docs/grub.texi +10 -0
+
+@node append_add_dbx_cert
+@subsection append_add_dbx_cert
+
+@deffn Command append_add_dbx_cert <X509_certificate>
+Read DER-formatted X.509 certificate from the file @var{X509_certificate}
+and add it to GRUB's internal dbx list of distrusted certificates.
+These certificates are used to block adding the distrusted certificates to
+the db list in the future and also ensure that the distrusted certificates
+are not used for appended signatures validation when the environment variable

s/not used for/rejected during/

Quoted 1 line

And I think rejection should be mentioned first…

Will do it.

Quoted 20 lines
Quoted 7 lines

+@code{check_appended_signatures} is set to @code{enforce}

docs/grub.texi +5 -0
+(@pxref{check_appended_signatures}) or the @command{append_verify}
+(@pxref{append_verify}) command is executed from the GRUB console.
+
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_dbx_cert} executes, then @var{X509_certificate} must

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6611

Quoted 10 lines
docs/grub.texi +10 -0
+be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_add_dbx_hash
+@subsection append_add_dbx_hash
+
+@deffn Command append_add_dbx_hash [@option{-b}|@option{-c}] <hash_file>
+Read ASCII text formatted binary/certificate hash from the file @var{hash_file}

Again, PEM...

Quoted 8 lines

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6615

Quoted 4 lines
docs/grub.texi +4 -0
+and add it to GRUB's internal dbx list of distrusted binary/certificate hashes.
+These hashes are used to block adding the distrusted binary hashes and
+certificates to the db list in the future, and also ensure that the distrusted
+binary hashes/certificates are not used for Linux kernel binary hashes and

s/not used for/rejected during/

And I think rejection should be mentioned first…

Will do it.

Quoted 10 lines

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6622

Quoted 8 lines
docs/grub.texi +7 -0
+appended signatures validation when the environment variable
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) is set to
+@code{enforce} or the @command{append_verify} (@pxref{append_verify}) command
+is executed from the GRUB console.
+
+The @option{-b} (@option{--binary-hash}) can be used to specify binary hash file and
+@option{-c} (@option{--cert-hash}) can be used to specify certificate hash file..

What are the formats for these files and how they should be generated?

These files are generated using secvarctl tool (https://github.com/open-power/secvarctl) and which are in ASCII text format.

Quoted 29 lines
Quoted 29 lines
docs/grub.texi +28 -0
+Note that if @code{check_appended_signatures} is set to @code{enforce}
+when @command{append_add_dbx_sig} executes, then @var{hash_file} must be properly signed.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_list_db
+@subsection append_list_db
+
+@deffn Command append_list_db
+List all X.509 certificates and binary hashes trusted by GRUB for validating
+appended signatures. The output is a numbered list of certificates and binary hashes,
+showing the certificate's serial number, issuer and Common Name.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
+@node append_list_dbx
+@subsection append_list_dbx
+
+@deffn Command append_list_dbx
+List all the distrusted X.509 certificates and binary/certificate hashes.
+The output is a numbered list of certificates and binary/certificate hashes,
+showing the certificate's serial number, issuer and Common Name.
+
+@xref{Using appended signatures} for more information.
+@end deffn
+
Quoted 8 lines

docs/grub.texi Note: The command is not allowed when lockdown is enforced (@pxref{Lockdown}). · line 6657

Quoted 6 lines

+@node append_verify
+@subsection append_verify

docs/grub.texi +4 -0
+
+@deffn Command append_verify <signed_file>
+Verifies an appended signature on @var{signed_file} against the trusted X.509 certificates
+known to GRUB (@pxref{append_list_db},@pxref{append_list_dbx}, @pxref{append_add_db_cert},

s/known/and hashes known/

Will do it.

Quoted 20 lines
Quoted 14 lines

+@pxref{append_add_db_hash}, @pxref{append_add_dbx_hash}, and @pxref{append_add_dbx_cert}).

docs/grub.texi +5 -0
+Exit code @code{$?} is set to 0 if the signature validates successfully.
+If validation fails, it is set to a non-zero value.
+
+@xref{Using appended signatures}, for more information.
+@end deffn

@node authenticate
@subsection authenticate

docs/grub.texi +0 -0
@@ -7307,6 +7433,13 @@ configurations, but to allow the user to select from among multiple

configurations, and to enable ``one-shot'' boot attempts and ``savedefault'' behavior. @xref{Using GPG-style digital signatures}, for more information.

docs/grub.texi configurations, but to allow the user to select from among multiple · line 7438

Quoted 3 lines
docs/grub.texi +3 -0
+
+When combining this command with appended signatures (@pxref{Using appended signatures}),
+not allowed to change the value of environment variable @code{check_appended_signatures}

??? "not allowed"??? Something is missing here...

Quoted 6 lines

docs/grub.texi configurations, but to allow the user to select from among multiple · line 7442

Quoted 4 lines
docs/grub.texi +4 -0
+to @code{no} or @code{enforce} even with the @option{--skip-sig} option
+when the environment variable @code{check_appended_signatures}
+(@pxref{check_appended_signatures}) is set to @code{enforce} and GRUB is locked down.
+However, the environment block file is not validated by an appended signature.

I am not sure what you mean here. I think this paragraph should be rephrased…

Sure, will rephrase it.

Quoted 33 lines
Quoted 33 lines

@end deffn

docs/grub.texi +0 -0
@@ -8670,6 +8803,7 @@ environment variables and commands are listed in the same order.

@menu
* Authentication and authorisation:: Users and access control
* Using GPG-style digital signatures:: Booting digitally signed code
+* Using appended signatures:: An alternative approach to booting digitally signed code

* UEFI secure boot and shim::          Booting digitally signed PE files
* Secure Boot Advanced Targeting::     Embedded information for generation number based revocation
* Measured Boot::                      Measuring boot components
docs/grub.texi +0 -0
@@ -8835,6 +8969,104 @@ or BIOS) configuration to cause the machine to boot from a different

(attacker-controlled) device. GRUB is at best only one link in a secure boot chain.

+@node Using appended signatures
+@section Using appended signatures in GRUB

docs/grub.texi +15 -0
+
+GRUB supports verifying Linux-style 'appended signatures' for Linux on Power LPAR
+secure boot. Appended signatures are PKCS#7 messages containing a signature over the
+contents of a file, plus some metadata, appended to the end of a file. A file
+with an appended signature ends with the magic string:
+
+@example
+~Module signature appended~\n
+@end example
+
+where @code{\n} represents the line feed character, @code{0x0a}.
+
+Linux on Power LPAR secure boot is controlled by @strong{'ibm,secure-boot'}
+device tree property and if this property is set to @code{2} (@samp{2 - enforced}),
+GRUB enters lockdown. There are three secure boot modes. They are
Quoted 8 lines

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8994

Quoted 6 lines
docs/grub.texi +6 -0
+
+@itemize
+@item @samp{0 - disabled}: Secure boot is disabled. This is the default.
+@item @samp{1 - audit}: Enforce signature verification by setting
+      @code{check_appended_signatures} (@pxref{check_appended_signatures}) to
+      @code{enforce} and not to lockdown the GRUB.

s/and not/and do not/

Sorry will do it

Quoted 10 lines

I think you are sometimes missing verbs... 🤔

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 8999

Quoted 5 lines

+@item @samp{2 - enforced}: Lockdown the GRUB and enforce signature verification by setting

docs/grub.texi +4 -0
+      @code{check_appended_signatures} (@pxref{check_appended_signatures}) to @code{enforce}.
+@end itemize
+
+Note that Linux on Power LPAR only @strong{supports disabled and enforced}.

s/enforced/enforce/?

Here enforced mode

Quoted 20 lines

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9007

Quoted 8 lines
docs/grub.texi +7 -0
+To enable appended signature verification, load the appendedsig module and an
+X.509 certificate for verification. Building the appendedsig module into the
+core GRUB image is recommended.
+
+In static key management mode, certificates will be built into the core image using
+the @code{--x509} parameter to @command{grub-install} or @command{grub-mkimage}.
+It allows listing the trusted certificates at boot time using @command{append_list_db}

You can list the trusted certificates available at boot time using...

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9012

Quoted 5 lines
docs/grub.texi +5 -0
+(@pxref{append_list_db}).
+
+In dynamic key management mode, db and dbx are read from the Platform KeyStore(PKS). If
+db is not present in PKS, static key (built-in keys) is used as the default key.
+It allows listing the trusted certificates and binary hashes at boot time using

s/allows listing/is possible to list/...
s/at boot/available at boot/

Quoted 1 line

Similar changes are worth doing here and there…

Sure, will do it.

Quoted 11 lines
Quoted 11 lines

+@command{append_list_dbx} (@pxref{append_list_db}) and listing distrusted

docs/grub.texi +9 -0
+certificates and binary/certificate hashes at boot time using @command{append_list_dbx}
+(@pxref{append_list_dbx}).
+
+Enforcement of signature verification is controlled by the
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) variable.
+
+@itemize
+@item @samp{no}: No verification is performed. This is the default.
+@item @samp{enforce}: Signature verification is performed and if signature verification fails,
Quoted 8 lines

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9028

Quoted 6 lines
docs/grub.texi +6 -0
+      post the errors and stop the boot. Signature verification cannot be disabled by setting
+      the @code{check_appended_signatures} variable back to @samp{no}.
+@end itemize
+
+A file can be explicitly verified using the @command{append_verify} (@pxref{append_verify}).
+The trusted certificates and binary hashes can be explicitly added using the

"added" to what?

Added to db list

Quoted 5 lines
Quoted 3 lines

+@command{append_add_db_cert} (@pxref{append_add_db_cert}) and @command{append_add_db_hash} +(@pxref{append_add_db_hash}). The distrusted certificates can be explicitly added using

Ditto…

Added to dbx list

Quoted 5 lines

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9032

Quoted 3 lines
docs/grub.texi +2 -0
+the @command{append_add_dbx_cert} (@pxref{append_add_dbx_cert}) and the distrusted
+certificate/binary hases can be explicitly addded using @command{append_add_dbx_hash}

Ditto…

Added to dbx list

Quoted 8 lines

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9037

Quoted 6 lines
docs/grub.texi +5 -0
+(@pxref{append_add_dbx_hash}).
+
+Only signatures generated using SHA-256 or SHA-512 hash algorithms are supported,
+and only RSA signatures generated using 2048, 3076, or 4096 bit key are supported.
+Only binary/certificate hash generated using SHA-256, SHA-384, or SHA-512 hash

s/SHA-512 hash/SHA-512/

Will do it.

Quoted 26 lines
Quoted 26 lines
docs/grub.texi +25 -0
+algorithms are supported.
+
+A file can be signed with the @command{sign-file} utility supplied with the
+Linux kernel source. For example, if you have @code{signing.key} as the private
+key and @code{certificate.der} as the X.509 certificate containing the public key:
+
+@example
+sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed
+@end example
+
+Once signature verification is turned on, the following file types must carry
+appended signatures:
+
+@enumerate
+@item Linux kernels
+@item GRUB modules, except those built in to the core image
+@item Any new certificate and binary hash files to be trusted
+@item Any new certificate/binary hash files to be distrusted
+@end enumerate
+
+When GRUB is locked down (when secure boot modes is @code{enforced}),
+signature verification cannot be @strong{disabled} by setting the
+@code{check_appended_signatures} (@pxref{check_appended_signatures}) variable
+to @code{no} or using the @command{load_env} (@pxref{load_env}) command from
+the GRUB console.
Quoted 8 lines

docs/grub.texi or BIOS) configuration to cause the machine to boot from a different · line 9068

Quoted 6 lines
docs/grub.texi +6 -0
+
+@example
+set check_appended_signatures=no
+        or
+load_env --file grubenv --skip-sig
+@end example

This example does not make a lot of sense for me…

Shall I drop it.

Thanks,
Sudhakar

Quoted 2 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel