Appended Signature Secure Boot Support for PowerPC
v4 cover Appended Signature Secure Boot Support for PowerPC
This patch set contains v4 of the consolidated version of the patch sets for secure boot using appended signatures on powerpc, rebased on top of git HEAD.
The v3 series is at
https://lists.gnu.org/archive/html/grub-devel/2025-06/msg00049.html
Changes since v3:
- Daniel Kiper review comments addressed:
- v4 patch 1: corrected the unreliable check conditions.
- v4 patch 3: added the GRUB signing process for single and multi signature.
- v4 patch 6: expanded the commit message .
- v4 patch 10 and 11: dropping some more N_() from various cryptic error messages.
- v4 patch 12: corrected the coding style, used grub_crypto_pk_rsa and added the explicit module dependencies using depends.
- v4 patch 14: rename appended signature GRUB commands with short names.
- v4 patch 16: merged from v3 patch 17 and 18
- v4 patch 17: corrected the coding style and GRUB_PKS_* constant are moved to "include/grub/efi/pks.h"
Also, fixed the warrnings and errors
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 - 3)
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 appended signature verification using builtin keys (Patches 4 - 14).
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 4, 5 and 6 are small refactorings.
- patch 7 allows x509 certificates to be built in to the grub core in much the same way as PGP keys.
- patch 8 brings in the code from GNUTLS that allows us to parse PKCS#7 and x509 with libtasn1.
- patch 9, 10 and 11 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 12 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.
- patch 13 adds tests, and patch 14 adds documentation.
3.) Enable lockdown if secure boot is enabled (Patch 15)
If the 'ibm,secure-boot' property of the root node is 2 or greater, enter lockdown.The main appended signature module now tests for lockdown to enter 'forced' mode.
4.) Enable accessing keys dynamically from Platform KeyStore (Patch 16 - 23)
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 16 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 17 is create the db and dbx lists from PKS.
- patch 18 is verify the kernel using db and dbx lists
- patch 19 sets the use_static_keys flag if DB not available in PKS, and patch 20 is reads the DB default keys from ELF Note and store it in trusted lists if use_static_keys flag is set.
- patch 21 adds db and dbx commands, and patch 22 adds documentation.
- patch 23 replacing the certificate number with an x.509 certificate in append_rm_dbx_cert test .
Thanks to Daniel Kiper for providing review comments on v3.
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 (6):
docs/grub: Document signing GRUB under UEFI
pgp: Factor out rsa_pad
crypto: Move storage for grub_crypto_pk_* to crypto.c appended signatures: Verification tests appended signatures: Documentation
ieee1275: Enter lockdown based on /ibm,secure-boot
Sudhakar Kuppusamy (17):
powerpc-ieee1275: Add support for signing GRUB with an appended
signature
docs/grub: Document 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 appended signatures: Support verifying appended signatures
ieee1275: Read the DB and DBX secure boot variables
appendedsig: create db and dbx lists
appendedsig: verify the kernel using db and dbx lists
powerpc_ieee1275: Introduce use_static_keys flag
appendedsig: Read default DB keys from the ELF Note
appendedsig: Introduce GRUB commands to access db and dbx
appendedsig: Documentation
appendedsig test: Replace the certificate number with an x.509
certificate
docs/grub.texi | 327 +++- grub-core/Makefile.am | 2 + grub-core/Makefile.core.def | 33 + grub-core/commands/appendedsig/appendedsig.c | 1432 +++++++++++++++++ grub-core/commands/appendedsig/appendedsig.h | 108 ++ 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 | 953 +++++++++++ grub-core/commands/pgp.c | 34 +- grub-core/kern/ieee1275/ieee1275.c | 1 - grub-core/kern/ieee1275/init.c | 54 + grub-core/kern/powerpc/ieee1275/ieee1275.c | 139 ++ .../kern/powerpc/ieee1275/platform_keystore.c | 353 ++++ grub-core/lib/crypto.c | 4 + grub-core/lib/pkcs1_v15.c | 64 + grub-core/tests/appended_signature_test.c | 248 +++ 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 | 2 + include/grub/ieee1275/ieee1275.h | 3 + include/grub/kernel.h | 3 +- include/grub/lockdown.h | 3 +- include/grub/pkcs1_v15.h | 27 + include/grub/powerpc/ieee1275/ieee1275.h | 21 + .../grub/powerpc/ieee1275/platform_keystore.h | 127 ++ include/grub/util/install.h | 13 +- include/grub/util/mkimage.h | 4 +- util/grub-install-common.c | 43 +- util/grub-mkimage.c | 33 +- util/grub-mkimagexx.c | 40 +- util/mkimage.c | 50 +- 35 files changed, 6325 insertions(+), 72 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/lib/pkcs1_v15.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/pkcs1_v15.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
PATCH v4 1/23 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 | 10 ++++++++-- include/grub/util/mkimage.h | 4 ++-- util/grub-install-common.c | 24 +++++++++++++++++++--- util/grub-mkimage.c | 19 ++++++++++++++++++ util/grub-mkimagexx.c | 40 ++++++++++++++++++++++++++++++++++++- util/mkimage.c | 9 ++++++--- 6 files changed, 95 insertions(+), 11 deletions(-)
include/grub/util/install.h +8 -2
@@ -69,9 +69,14 @@ 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 } +/* The max_appended_signature_size = 4096 bit key signature size x number of signature. */ +#define GRUB_APPSIG_SIZE_MAX ((size_t) (824 * 200)) + int grub_install_parse (int key, char *arg);
@@ -132,7 +137,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 +198,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
@@ -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 +21 -3
@@ -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,20 @@ 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 || appsig_size >GRUB_APPSIG_SIZE_MAX ) + { +grub_util_error (_ ("appended signature size `%s' not in the range of 1 byte to %lu byte" ), + arg,GRUB_APPSIG_SIZE_MAX ); +return 0 ; + } +return 1 ;default :return 0 ; }
@@ -683,9 +700,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 +716,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 +19 -0
@@ -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 argumentsint 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,21 @@ 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 || arguments->appsig_size >GRUB_APPSIG_SIZE_MAX ) + { +grub_util_error (_ ("appended signature size `%s' not in the range of 1 byte to %lu byte" ), + arg,GRUB_APPSIG_SIZE_MAX ); +return 1 ; + } +break ; +case 'm' :if (arguments->memdisk)free (arguments->memdisk);
@@ -330,6 +348,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
@@ -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_descmemcpy (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
@@ -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
PATCH v4 2/23 docs/grub: Document signing GRUB under UEFI
In reply to Sudhakar KuppusamyFrom: 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
@@ -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
PATCH v4 3/23 docs/grub: Document signing GRUB with an appended signature
In reply to Sudhakar KuppusamySigning 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 | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
docs/grub.texi +86 -0
@@ -9379,6 +9379,92 @@ 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.img} 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.img} +can specify the size and location of the appended signature with an ELF +note added by @command{grub-install}. +An image can be signed this way using the @command{sign-file} command from +the Linux kernel: +@example +@group +# Signing a GRUB image with only one signer key in the appended signature: +# The grub.key is your private key and certificate.der is your GRUB signing public key +# kernel.der is your kernel signing public key. +# +# 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 /dev/empty.sig + +# Get the size of the signature. + +EMPTY_SIG_SIZE=`stat -c '%s' /dev/empty.sig` + +# Remove the empty files. + +rm -rf /dev/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 /grub2 \ +--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 + +# +# Signing a GRUB image with more than one signer key in the appended signature: +# 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. + +# 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 /dev/empty.p7s -outform DER -noattr -md sha256 + +# Signing the /dev/null with an appended signature. + +sign-file -s /dev/empty.p7s sha256 /dev/null /dev/null /dev/empty.signed + +# Get the size of the signature. + +EMPTY_SIG_SIZE=`stat -c '%s' /dev/empty.signed` + +# Remove the empty files. + +rm -rf /dev/empty.signed /dev/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 /grub2 \ +--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 + +# Don't forget to install the signed image as required (e.g. on powerpc-ieee1275, to the PReP partition) + +@end group +@end example +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
PATCH v4 4/23 pgp: Factor out rsa_pad
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
rsa_pad does the PKCS#1 v1.5 padding for the RSA signature scheme. We want to use it in other RSA signature verification applications.
I considered and rejected putting it in lib/crypto.c. That file doesn't currently require any MPI functions, but rsa_pad does. That's not so much of a problem for the grub kernel and modules, but crypto.c also gets built into all the grub utilities. So - despite the utils not using any asymmetric ciphers - we would need to built the entire MPI infrastructure in to them.
A better and simpler solution is just to spin rsa_pad out into its own PKCS#1 v1.5 module.
--- grub-core/Makefile.core.def | 9 ++++++ grub-core/commands/pgp.c | 28 ++-------------- grub-core/lib/pkcs1_v15.c | 64 +++++++++++++++++++++++++++++++++++++ include/grub/pkcs1_v15.h | 27 ++++++++++++++++ 4 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 grub-core/lib/pkcs1_v15.c create mode 100644 include/grub/pkcs1_v15.h
grub-core/Makefile.core.def +9 -0
@@ -2545,6 +2545,15 @@ module = { cppflags = '$(CPPFLAGS_GCRY)'; }; +module = { + name = pkcs1_v15; + common = lib/pkcs1_v15.c; + + cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls -Wno-sign-compare'; + cppflags = '$(CPPFLAGS_GCRY)'; + depends = mpi; +}; + module = { name = all_video; common = lib/fake_module.c;
grub-core/commands/pgp.c +2 -26
@@ -24,6 +24,7 @@#include <grub/file.h> #include <grub/command.h> #include <grub/crypto.h> +#include <grub/pkcs1_v15.h> #include <grub/i18n.h> #include <grub/gcrypt/gcrypt.h> #include <grub/pubkey.h>
@@ -411,32 +412,7 @@ static intrsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, const gcry_md_spec_t *hash, struct grub_public_subkey *sk) { -grub_size_t tlen, emlen, fflen; -grub_uint8_t *em, *emptr; -unsigned nbits =gcry_mpi_get_nbits (sk->mpis[0 ]); -int ret; - tlen = hash->mdlen + hash->asnlen; - emlen = (nbits +7 ) /8 ; -if (emlen < tlen +11 ) -return 1 ; - - em =grub_malloc (emlen); -if (!em) -return 1 ; - - em[0 ] =0x00 ; - em[1 ] =0x01 ; - fflen = emlen - tlen -3 ; -for (emptr = em +2 ; emptr < em +2 + fflen; emptr++) - *emptr =0xff ; - *emptr++ =0x00 ; -grub_memcpy (emptr, hash->asnoid, hash->asnlen); - emptr += hash->asnlen; -grub_memcpy (emptr, hval, hash->mdlen); - - ret =gcry_mpi_scan (hmpi,GCRYMPI_FMT_USG , em, emlen,0 ); -grub_free (em); -return ret; +return grub_crypto_rsa_pad (hmpi, hval, hash, sk->mpis[0 ]); }struct grub_pubkey_context
grub-core/lib/pkcs1_v15.c new file +64 -0
64 lines not on this page · raw message
include/grub/pkcs1_v15.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2013 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +/* +* Given a hash value 'hval', of hash specification 'hash', perform +* the EMSA-PKCS1-v1_5 padding suitable for a key with modulus 'mod' +* (See RFC 8017 s 9.2). +*/ +extern gcry_err_code_t +grub_crypto_rsa_pad (gcry_mpi_t *hmpi,grub_uint8_t *hval, +const gcry_md_spec_t *hash,gcry_mpi_t mod); +
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v4 04/23] pgp: Factor out rsa_pad
Quoted 12 lines · Sudhakar Kuppusamy
From: Daniel Axtens <[email protected]>
rsa_paddoes the PKCS#1 v1.5 padding for the RSA signature scheme. We want to use it in other RSA signature verification applications.I considered and rejected putting it in
lib/crypto.c. That file doesn't currently require any MPI functions, butrsa_paddoes. That's not so much of a problem for the grub kernel and modules, butcrypto.calso gets built into all the grub utilities. So - despite the utils not using any asymmetric ciphers - we would need to built the entire MPI infrastructure in to them.
Quoted 3 lines
A better and simpler solution is just to spin
rsa_padout into its own PKCS#1 v1.5 module.
The libgcrypt 1.11.0 patches were merged recently, and it seems not necessary to factor out rsa_pad anymore. The verification can be done with S-expression like this:
_gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s %M)))", si->sig_mpi);_gcry_sexp_build (&s_data, NULL, "(data (flags pkcs1)(hash %s %b))", si->hash->name, si->hash->mdlen, hash); _gcry_sexp_build (&s_key, NULL, "(public-key(rsa(n%M)(e%M)))", cert->mpis[0], cert->mpis[1]); _gcry_pubkey_spec_rsa.verify (s_sig, s_data, s_key);
Gary Lin
Quoted 191 lines
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>Reviewed-by Daniel Kiper <[email protected]>--- grub-core/Makefile.core.def | 9 ++++++ grub-core/commands/pgp.c | 28 ++-------------- grub-core/lib/pkcs1_v15.c | 64 +++++++++++++++++++++++++++++++++++++ include/grub/pkcs1_v15.h | 27 ++++++++++++++++ 4 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 grub-core/lib/pkcs1_v15.c create mode 100644 include/grub/pkcs1_v15.hgrub-core/Makefile.core.def +9
-0@@ -2545,6 +2545,15 @@ module = { cppflags = '$(CPPFLAGS_GCRY)'; }; +module = { + name = pkcs1_v15; + common = lib/pkcs1_v15.c; + + cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls -Wno-sign-compare'; + cppflags = '$(CPPFLAGS_GCRY)'; + depends = mpi; +}; + module = { name = all_video; common = lib/fake_module.c;grub-core/commands/pgp.c +2
-26@@ -24,6 +24,7 @@#include <grub/file.h> #include <grub/command.h> #include <grub/crypto.h> +#include <grub/pkcs1_v15.h> #include <grub/i18n.h> #include <grub/gcrypt/gcrypt.h> #include <grub/pubkey.h> @@ -411,32 +412,7 @@ static intrsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, const gcry_md_spec_t *hash, struct grub_public_subkey *sk) { -grub_size_t tlen, emlen, fflen; -grub_uint8_t *em, *emptr; -unsigned nbits =gcry_mpi_get_nbits (sk->mpis[0 ]); -int ret; - tlen = hash->mdlen + hash->asnlen; - emlen = (nbits +7 ) /8 ; -if (emlen < tlen +11 ) -return 1 ; - - em =grub_malloc (emlen); -if (!em) -return 1 ; - - em[0 ] =0x00 ; - em[1 ] =0x01 ; - fflen = emlen - tlen -3 ; -for (emptr = em +2 ; emptr < em +2 + fflen; emptr++) - *emptr =0xff ; - *emptr++ =0x00 ; -grub_memcpy (emptr, hash->asnoid, hash->asnlen); - emptr += hash->asnlen; -grub_memcpy (emptr, hval, hash->mdlen); - - ret =gcry_mpi_scan (hmpi,GCRYMPI_FMT_USG , em, emlen,0 ); -grub_free (em); -return ret; +return grub_crypto_rsa_pad (hmpi, hval, hash, sk->mpis[0 ]); }struct grub_pubkey_context grub-core/lib/pkcs1_v15.c new file +64
-0@@ -0,0 +1,64 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2013 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/dl.h> +#include <grub/gcrypt/gcrypt.h> +#include <grub/pkcs1_v15.h> + +GRUB_MOD_LICENSE ("GPLv3+" ); + +/* +* Given a hash value 'hval', of hash specification 'hash', perform +* the EMSA-PKCS1-v1_5 padding suitable for a key with modulus 'mod' +* (see RFC 8017 s 9.2) and place the result in 'hmpi'. +*/ +gcry_err_code_t +grub_crypto_rsa_pad (gcry_mpi_t *hmpi,grub_uint8_t *hval, +const gcry_md_spec_t *hash,gcry_mpi_t mod) +{ +grub_size_t tlen, emlen, fflen; +grub_uint8_t *em, *emptr; +unsigned nbits =gcry_mpi_get_nbits (mod); +int ret; + + tlen = hash->mdlen + hash->asnlen; + emlen = (nbits +7 ) /8 ; +if (emlen < tlen +11 ) +return GPG_ERR_TOO_SHORT ; + + em =grub_malloc (emlen); +if (em ==NULL ) +return GPG_ERR_OUT_OF_MEMORY ; + + em[0 ] =0x00 ; + em[1 ] =0x01 ; + fflen = emlen - tlen -3 ; + +for (emptr = em +2 ; emptr < em +2 + fflen; emptr++) + *emptr =0xff ; + + *emptr++ =0x00 ; +grub_memcpy (emptr, hash->asnoid, hash->asnlen); + emptr += hash->asnlen; +grub_memcpy (emptr, hval, hash->mdlen); + + ret =gcry_mpi_scan (hmpi,GCRYMPI_FMT_USG , em, emlen,0 ); +grub_free (em); + +return ret; +}include/grub/pkcs1_v15.h new file +27
-0@@ -0,0 +1,27 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2013 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +/* +* Given a hash value 'hval', of hash specification 'hash', perform +* the EMSA-PKCS1-v1_5 padding suitable for a key with modulus 'mod' +* (See RFC 8017 s 9.2). +*/ +extern gcry_err_code_t +grub_crypto_rsa_pad (gcry_mpi_t *hmpi,grub_uint8_t *hval, +const gcry_md_spec_t *hash,gcry_mpi_t mod); +Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v4 04/23] pgp: Factor out rsa_pad
Quoted 25 lines · Gary Lin
Quoted 12 lines · Sudhakar Kuppusamy
From: Daniel Axtens <[email protected]>
rsa_paddoes the PKCS#1 v1.5 padding for the RSA signature scheme. We want to use it in other RSA signature verification applications.I considered and rejected putting it in
lib/crypto.c. That file doesn't currently require any MPI functions, butrsa_paddoes. That's not so much of a problem for the grub kernel and modules, butcrypto.calso gets built into all the grub utilities. So - despite the utils not using any asymmetric ciphers - we would need to built the entire MPI infrastructure in to them.Quoted 3 lines
A better and simpler solution is just to spin
rsa_padout into its own PKCS#1 v1.5 module.The libgcrypt 1.11.0 patches were merged recently, and it seems not necessary to factor out
rsa_padanymore. The verification can be done with S-expression like this:
Quoted 6 lines
_gcry_sexp_build(&s_sig, NULL, "(sig-val(rsa(s %M)))",si->sig_mpi);_gcry_sexp_build(&s_data, NULL, "(data (flags pkcs1)(hash %s %b))",si->hash->name,si->hash->mdlen, hash);_gcry_sexp_build(&s_key, NULL, "(public-key(rsa(n%M)(e%M)))", cert->mpis[0], cert->mpis[1]); _gcry_pubkey_spec_rsa.verify (s_sig,s_data,s_key);Gary Lin
Hi Gary Lin,
Thanks for pointing this out. Updated the patch to reflect this change on v5 (https://lists.gnu.org/archive/html/grub-devel/2025-07/msg00137.html).
And also recently posted v6. Appreciate the heads-up!
V6:
https://lists.gnu.org/archive/html/grub-devel/2025-07/msg00255.html
Thanks,
Sudhakar
Quoted 188 lines
Quoted 188 lines
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>Reviewed-by Daniel Kiper <[email protected]>---
grub-core/Makefile.core.def | 9 ++++++ grub-core/commands/pgp.c | 28 ++-------------- grub-core/lib/pkcs1_v15.c | 64 +++++++++++++++++++++++++++++++++++++ include/grub/pkcs1_v15.h | 27 ++++++++++++++++ 4 files changed, 102 insertions(+), 26 deletions(-)create mode 100644
grub-core/lib/pkcs1_v15.c
create mode 100644include/grub/pkcs1_v15.hgrub-core/Makefile.core.def +9
-0@@ -2545,6 +2545,15 @@ module = { cppflags = '$(CPPFLAGS_GCRY)'; }; +module = { + name = pkcs1_v15; + common = lib/pkcs1_v15.c; + + cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls -Wno-sign-compare'; + cppflags = '$(CPPFLAGS_GCRY)'; + depends = mpi; +}; + module = { name = all_video; common = lib/fake_module.c;grub-core/commands/pgp.c +2
-26@@ -24,6 +24,7 @@#include <grub/file.h> #include <grub/command.h> #include <grub/crypto.h> +#include <grub/pkcs1_v15.h> #include <grub/i18n.h> #include <grub/gcrypt/gcrypt.h> #include <grub/pubkey.h> @@ -411,32 +412,7 @@ static intrsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval, const gcry_md_spec_t *hash, struct grub_public_subkey *sk) { -grub_size_t tlen, emlen, fflen; -grub_uint8_t *em, *emptr; -unsigned nbits =gcry_mpi_get_nbits (sk->mpis[0 ]); -int ret; - tlen = hash->mdlen + hash->asnlen; - emlen = (nbits +7 ) /8 ; -if (emlen < tlen +11 ) -return 1 ; - - em =grub_malloc (emlen); -if (!em) -return 1 ; - - em[0 ] =0x00 ; - em[1 ] =0x01 ; - fflen = emlen - tlen -3 ; -for (emptr = em +2 ; emptr < em +2 + fflen; emptr++) - *emptr =0xff ; - *emptr++ =0x00 ; -grub_memcpy (emptr, hash->asnoid, hash->asnlen); - emptr += hash->asnlen; -grub_memcpy (emptr, hval, hash->mdlen); - - ret =gcry_mpi_scan (hmpi,GCRYMPI_FMT_USG , em, emlen,0 ); -grub_free (em); -return ret; +return grub_crypto_rsa_pad (hmpi, hval, hash, sk->mpis[0 ]); }struct
grub_pubkey_contextgrub-core/lib/pkcs1_v15.c new file +64
-0@@ -0,0 +1,64 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2013 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/dl.h> +#include <grub/gcrypt/gcrypt.h> +#include <grub/pkcs1_v15.h> + +GRUB_MOD_LICENSE ("GPLv3+" ); + +/* +* Given a hash value 'hval', of hash specification 'hash', perform +* the EMSA-PKCS1-v1_5 padding suitable for a key with modulus 'mod' +* (see RFC 8017 s 9.2) and place the result in 'hmpi'. +*/ +gcry_err_code_t +grub_crypto_rsa_pad (gcry_mpi_t *hmpi,grub_uint8_t *hval, +const gcry_md_spec_t *hash,gcry_mpi_t mod) +{ +grub_size_t tlen, emlen, fflen; +grub_uint8_t *em, *emptr; +unsigned nbits =gcry_mpi_get_nbits (mod); +int ret; + + tlen = hash->mdlen + hash->asnlen; + emlen = (nbits +7 ) /8 ; +if (emlen < tlen +11 ) +return GPG_ERR_TOO_SHORT ; + + em =grub_malloc (emlen); +if (em ==NULL ) +return GPG_ERR_OUT_OF_MEMORY ; + + em[0 ] =0x00 ; + em[1 ] =0x01 ; + fflen = emlen - tlen -3 ; + +for (emptr = em +2 ; emptr < em +2 + fflen; emptr++) + *emptr =0xff ; + + *emptr++ =0x00 ; +grub_memcpy (emptr, hash->asnoid, hash->asnlen); + emptr += hash->asnlen; +grub_memcpy (emptr, hval, hash->mdlen); + + ret =gcry_mpi_scan (hmpi,GCRYMPI_FMT_USG , em, emlen,0 ); +grub_free (em); + +return ret; +}include/grub/pkcs1_v15.h new file +27
-0@@ -0,0 +1,27 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2013 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +/* +* Given a hash value 'hval', of hash specification 'hash', perform +* the EMSA-PKCS1-v1_5 padding suitable for a key with modulus 'mod' +* (See RFC 8017 s 9.2). +*/ +extern gcry_err_code_t +grub_crypto_rsa_pad (gcry_mpi_t *hmpi,grub_uint8_t *hval, +const gcry_md_spec_t *hash,gcry_mpi_t mod); +Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 4 lines
Quoted 4 lines
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v4 04/23] pgp: Factor out rsa_pad
Quoted 30 lines · Sudhakar Kuppusamy
Quoted 30 lines · Gary Lin
Quoted 12 lines · Sudhakar Kuppusamy
From: Daniel Axtens <[email protected]>
rsa_paddoes the PKCS#1 v1.5 padding for the RSA signature scheme. We want to use it in other RSA signature verification applications.I considered and rejected putting it in
lib/crypto.c. That file doesn't currently require any MPI functions, butrsa_paddoes. That's not so much of a problem for the grub kernel and modules, butcrypto.calso gets built into all the grub utilities. So - despite the utils not using any asymmetric ciphers - we would need to built the entire MPI infrastructure in to them.Quoted 3 lines
A better and simpler solution is just to spin
rsa_padout into its own PKCS#1 v1.5 module.The libgcrypt 1.11.0 patches were merged recently, and it seems not necessary to factor out
rsa_padanymore. The verification can be done with S-expression like this:
_gcry_sexp_build(&s_sig, NULL, "(sig-val(rsa(s %M)))",si->sig_mpi);_gcry_sexp_build(&s_data, NULL, "(data (flags pkcs1)(hash %s %b))",si->hash->name,si->hash->mdlen, hash);_gcry_sexp_build(&s_key, NULL, "(public-key(rsa(n%M)(e%M)))", cert->mpis[0], cert->mpis[1]); _gcry_pubkey_spec_rsa.verify (s_sig,s_data,s_key);
Quoted 7 lines
Quoted 1 line
Gary Lin
Hi Gary Lin,
Thanks for pointing this out. Updated the patch to reflect this change on v5 (https://lists.gnu.org/archive/html/grub-devel/2025-07/msg00137.html).
And also recently posted v6. Appreciate the heads-up!
Oops, I overlooked v5. Thanks for updating the patch!
Gary Lin
Quoted 7 lines
V6:
https://lists.gnu.org/archive/html/grub-devel/2025-07/msg00255.htmlThanks,
Sudhakar
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v4 5/23 crypto: Move storage for grub_crypto_pk_* to crypto.c
In reply to Sudhakar KuppusamyFrom: 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
@@ -147,10 +147,6 @@ const char *hashes[] = { [0x0b] = "sha224" }; -struct gcry_pk_spec *grub_crypto_pk_dsa; -struct gcry_pk_spec *grub_crypto_pk_ecdsa; -struct gcry_pk_spec *grub_crypto_pk_rsa; -static int dsa_pad (gcry_mpi_t *hmpi,grub_uint8_t *hval,const gcry_md_spec_t *hash,struct grub_public_subkey *sk);
grub-core/lib/crypto.c +4 -0
@@ -121,6 +121,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
PATCH v4 6/23 pgp: Rename OBJ_TYPE_PUBKEY to OBJ_TYPE_GPG_PUBKEY
In reply to Sudhakar KuppusamyPrior 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.
--- grub-core/commands/pgp.c | 2 +- include/grub/kernel.h | 2 +- util/grub-mkimage.c | 2 +- util/mkimage.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
grub-core/commands/pgp.c +1 -1
@@ -944,7 +944,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
@@ -28,7 +28,7 @@ enumOBJ_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
@@ -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
@@ -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
PATCH v4 7/23 grub-install: Support embedding x509 certificates
In reply to Sudhakar KuppusamyTo 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
@@ -29,6 +29,7 @@ enumOBJ_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
@@ -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, \
@@ -196,6 +198,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
@@ -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++;
@@ -649,6 +655,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);
@@ -689,6 +698,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++ =' ' ;
@@ -715,7 +732,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
@@ -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 argumentschar *dtb;char **pubkeys;size_t npubkeys; +char **x509keys; +size_t nx509keys;char *font;char *config;char *sbat;
@@ -216,6 +219,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);
@@ -346,7 +355,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
@@ -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
PATCH v4 8/23 appended signatures: Import GNUTLS's ASN.1 description files
In reply to Sudhakar KuppusamyIn 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
@@ -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
PATCH v4 9/23 appended signatures: Parse ASN1 node
In reply to Sudhakar KuppusamyThis 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
@@ -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
PATCH v4 10/23 appended signatures: Parse PKCS#7 signedData
In reply to Sudhakar KuppusamyThis 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
@@ -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
PATCH v4 11/23 appended signatures: Parse X.509 certificates
In reply to Sudhakar KuppusamyThis 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 | 30 + grub-core/commands/appendedsig/x509.c | 953 +++++++++++++++++++ 2 files changed, 983 insertions(+) create mode 100644 grub-core/commands/appendedsig/x509.c
grub-core/commands/appendedsig/appendedsig.h +30 -0
@@ -25,6 +25,21 @@ 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, CN and RSA public key. +*/ +struct x509_certificate +{ +struct x509_certificate *next; +grub_uint8_t *serial; +grub_size_t serial_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 +58,21 @@ struct pkcs7_signedDatastruct 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 +953 -0
953 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
PATCH v4 12/23 appended signatures: Support verifying appended signatures
In reply to Sudhakar KuppusamyBuilding on the parsers and the ability to embed x509 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 GRUB is locked down when the module is loaded, verification will be enabled and locked automatically.
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.
--- grub-core/Makefile.core.def | 14 + grub-core/commands/appendedsig/appendedsig.c | 594 +++++++++++++++++++ include/grub/file.h | 2 + 3 files changed, 610 insertions(+) create mode 100644 grub-core/commands/appendedsig/appendedsig.c
grub-core/Makefile.core.def +14 -0
@@ -979,6 +979,20 @@ module = { cppflags = '-I$(srcdir)/lib/posix_wrap'; }; +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_POSIX)'; + cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/libtasn1-grub'; + depends = crypto, gcry_rsa, gcry_sha256, gcry_sha512, pkcs1_v15, mpi, asn1; +}; + module = { name = hdparm; common = commands/hdparm.c;
grub-core/commands/appendedsig/appendedsig.c new file +594 -0
594 lines not on this page · raw message
include/grub/file.h +2 -0
@@ -80,6 +80,8 @@ enum grub_file_typeGRUB_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
PATCH v4 13/23 appended signatures: Verification tests
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
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 | 250 ++++++ grub-core/tests/appended_signatures.h | 975 ++++++++++++++++++++++ grub-core/tests/lib/functional_test.c | 5 + 4 files changed, 1237 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
@@ -2194,6 +2194,13 @@ module = { common = tests/setjmp_test.c; }; +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 +250 -0
250 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
@@ -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
PATCH v4 14/23 appended signatures: Documentation
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
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 | 189 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 168 insertions(+), 21 deletions(-)
docs/grub.texi +168 -21
@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB. @menu * biosnum:: +* check_appended_signatures:: * check_signatures:: * chosen:: * cmdpath::
@@ -3343,12 +3344,16 @@ 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 vmlinux file. @xref{Using appended signatures}. @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
@@ -6414,6 +6419,10 @@ 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 an X.509 certificate to the db list +* append_list_db:: List trusted certificates from the db list +* append_rm_dbx_cert:: Remove a certificate from the db list +* append_verify:: Verify appended digital signature using db list * 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 +6544,68 @@ 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 a DER-formatted X.509 certificate from the file @var{X509_certificate} +and add it to GRUB's internal db list of trusted X.509 certificates. These +certificates are used to validate appended signatures when the environment +variable @code{check_appended_signatures} is set to @code{enforce}. + +Note that if @code{check_appended_signatures} is set to @code{enforce} +when @command{append_add_db_cert} is executed, then @var{X509_certificate} +must itself bear an appended signature. (It is not sufficient that +@var{X509_certificate} be signed by a trusted certificate according to the +X.509 rules: GRUB does not include support for validating signatures within X.509 +certificates themselves.) + +See @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 trusted by GRUB for validating appended signatures. +The output is a numbered list of certificates, showing the certificate's serial +number and Common Name. + +The certificate number can be used as an argument to +@command{append_rm_dbx_cert} (@pxref{append_rm_dbx_cert}). + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_rm_dbx_cert +@subsection append_rm_dbx_cert + +@deffn Command append_rm_dbx_cert cert_number +Remove the X.509 certificate numbered @var{cert_number} from GRUB's keyring of +db for verifying appended signatures. + +@var{cert_number} is the certificate number as listed by +@command{append_list_db} (@pxref{append_list_db}). + +These certificates are used to validate appended signatures when environment +variable @code{check_appended_signatures} is set to @code{enforce} +(@pxref{check_appended_signatures}), and by @command{append_verify} +(@pxref{append_verify}). See @xref{Using appended signatures} for more +information. +@end deffn + +@node append_verify +@subsection append_verify + +@deffn Command append_verify file +Verifies an appended signature on @var{file} against the trusted X.509 certificates +known to GRUB (See @pxref{append_list_db}, @pxref{append_add_db_cert}, and +@pxref{append_rm_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. + +See @xref{Using appended signatures}, for more information. +@end deffn @node authenticate @subsection authenticate
@@ -6854,7 +6925,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
@@ -7250,7 +7321,6 @@ without any options, the @command{keystatus} command returns true if and only if checking key modifier status is supported. @end deffn - @node list_env @subsection list_env
@@ -7270,7 +7340,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,8 +7375,11 @@ 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. +Extra care should be taken when combining this command with appended signatures +(@pxref{Using appended signatures}), as this file is not validated by an +appended signature and could set @code{check_appended_signatures=no}. @end deffn
@@ -7677,7 +7750,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,11 +8240,10 @@ 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 - @node unset @subsection unset
@@ -8190,7 +8262,6 @@ only on PC BIOS platforms. @end deffn @end ignore - @node verify_detached @subsection verify_detached
@@ -8208,7 +8279,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,14 +8739,15 @@ 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 -* Signing GRUB itself:: Ensuring the integrity of the GRUB core image +* 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 +* 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
@@ -8751,8 +8823,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.
@@ -8835,6 +8907,81 @@ 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 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}. + +To enable appended signature verification, load the appendedsig module and an +x509 certificate for verification. Building the appendedsig module into the +core grub image is recommended. + +Certificates can be managed at boot time using the @pxref{append_add_db_cert}, +@pxref{append_rm_dbx_cert} and @pxref{append_list_db} commands. +Certificates can also be built in to the core image using the @code{--x509} +parameter to @command{grub-install} or @command{grub-mkimage}. +A file can be explicitly verified using the @pxref{append_verify} command. + +Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported, +and only RSA signatures 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 x509 certificate containing the public key: + +@example +sign-file SHA256 signing.key certificate.der vmlinux vmlinux.signed +@end example + +Enforcement of signature verification is controlled by the +@code{check_appended_signatures} variable. Verification will only take place +when files are loaded if the variable is set to @code{enforce}. If a +certificate is built into the grub core image with the @code{--x509} parameter, +the variable will be automatically set to @code{enforce} when the appendedsig +module is loaded. + +Unlike GPG-style signatures, not all files loaded by GRUB are required to be +signed. Once verification is turned on, the following file types must carry +appended signatures: + +@enumerate +@item Linux, Multiboot, BSD, XNU and Plan9 kernels +@item Grub modules, except those built in to the core image +@item Any new certificate files to be trusted +@end enumerate + +ACPI tables and Device Tree images will not be checked for appended signatures +but must be verified by another mechanism such as GPG-style signatures before +they will be loaded. + +No attempt is made to validate any other file type. In particular, +chain-loaded binaries are not verified - if your platform supports +chain-loading and this cannot be disabled, consider an alternative secure +boot mechanism. + +As with GPG-style appended signatures, signature checking does @strong{not} +stop an attacker with console access from dropping manually to the GRUB +console and executing: + +@example +set check_appended_signatures=no +@end example + +Refer to the section on password-protecting GRUB (@pxref{Authentication +and authorisation}) for more information on preventing this. + +Additionally, special care must be taken around the @command{loadenv} command, +which can be used to turn off @code{check_appended_signature}. + @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
PATCH v4 15/23 ieee1275: Enter lockdown based on /ibm,secure-boot
In reply to Sudhakar KuppusamyFrom: Daniel Axtens <[email protected]>
If the 'ibm,secure-boot' property of the root node is 2 or greater, enter lockdown.
--- docs/grub.texi | 2 +- grub-core/Makefile.core.def | 1 + grub-core/kern/ieee1275/init.c | 42 ++++++++++++++++++++++++++++++++++ include/grub/lockdown.h | 3 ++- 4 files changed, 46 insertions(+), 2 deletions(-)
docs/grub.texi +1 -1
@@ -9060,7 +9060,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
@@ -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 +42 -0
60 lines not on this page · raw message
include/grub/lockdown.h +2 -1
@@ -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
PATCH v4 16/23 ieee1275: Read the DB and DBX secure boot variables
In reply to Sudhakar KuppusamyEnhancing 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 PKS, it will read secure boot variables such as db and dbx from PKS and extract ESL's from it.
The ESL's 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 mode will be activated:
1. When Secure Boot is enabled with static keys
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 - Secure Boot mode
1 - PKS
0 - static key (embeded key)
--- grub-core/Makefile.am | 2 + grub-core/Makefile.core.def | 2 + grub-core/kern/ieee1275/ieee1275.c | 1 - grub-core/kern/ieee1275/init.c | 18 +- grub-core/kern/powerpc/ieee1275/ieee1275.c | 139 +++++++ .../kern/powerpc/ieee1275/platform_keystore.c | 343 ++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + include/grub/powerpc/ieee1275/ieee1275.h | 21 ++ .../grub/powerpc/ieee1275/platform_keystore.h | 126 +++++++ 9 files changed, 651 insertions(+), 4 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
@@ -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
@@ -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
@@ -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 +15 -3
@@ -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. */
@@ -1004,7 +1006,8 @@ grub_get_ieee1275_secure_boot (void) {grub_ieee1275_phandle_t root;int rc; -grub_uint32_t is_sb; +grub_err_t err; +grub_uint32_t is_sb =SB_DISABLED ; rc =grub_ieee1275_finddevice ("/" , &root);if (rc !=0 )
@@ -1028,8 +1031,17 @@ grub_get_ieee1275_secure_boot (void)* * We only support enforce. */ -if (is_sb >=2 ) - grub_lockdown (); +if (is_sb ==SB_ENFORCED ) + { +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"); }#endif
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 +343 -0
343 lines not on this page · raw message
include/grub/ieee1275/ieee1275.h +3 -0
@@ -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 ((grub_ieee1275_cell_t) -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 +21 -0
@@ -28,4 +28,25 @@ 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 +EXPORT_FUNC (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 +126 -0
126 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
PATCH v4 17/23 appendedsig: create db and dbx lists
In reply to Sudhakar KuppusamyIf secure boot is enabled with a static key, the trusted certificates will be extracted from the GRUB ELF NOTE and adding in the db list. The db list is used by GRUB for kernel verification.
If secure boot is enabled with a dynamic key, the trusted certificates and certificate/binary hash will be extracted from the PKS and adding in the db list. The distrusted certificates, certificate/binary hash from the PKS and adding in the dbx list. Both dbx and db list are used by GRUB for kernel verification.
Note:-
If the certificate or the certificate hash exists in the dbx list, then reject the certificate/certificate hash before adding it in the db list.
--- grub-core/commands/appendedsig/appendedsig.c | 558 +++++++++++++++++-- include/grub/crypto.h | 1 + include/grub/efi/pks.h | 112 ++++ 3 files changed, 629 insertions(+), 42 deletions(-) create mode 100644 include/grub/efi/pks.h
grub-core/commands/appendedsig/appendedsig.c +516 -42
@@ -34,13 +34,16 @@#include <libtasn1.h> #include <grub/env.h> #include <grub/lockdown.h> - +#include <grub/efi/pks.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> #include "appendedsig.h" GRUB_MOD_LICENSE ("GPLv3+" );/* Public key type. */ -#define GRUB_PKEY_ID_PKCS7 2 +#define GRUB_PKEY_ID_PKCS7 2 +/* GUID size. */ +#define GRUB_GUID_SIZE (sizeof (grub_packed_guid_t))/* Appended signature magic string. */ static const char magic[] ="~Module signature appended~\n" ;
@@ -68,8 +71,30 @@ struct grub_appended_signaturestruct 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 ); static enum {
@@ -78,6 +103,201 @@ static enum CHECK_SIGS_FORCED = 2 } check_sigs =CHECK_SIGS_NO ; +/* +* 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 empty." ); + +if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha256; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha384; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha512; +else +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"unsupported GUID for 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 empty." ); + + 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_GUID_SIZE ) ==0 ) +return true; + +return false; +} + +static bool +is_cert_match (const struct x509_certificate *distrusted_cert, +const struct x509_certificate *db_cert) +{ +if (grub_memcmp (distrusted_cert->subject, db_cert->subject, db_cert->subject_len) ==0 + &&grub_memcmp (distrusted_cert->serial, db_cert->serial, db_cert->serial_len) ==0 + &&grub_memcmp (distrusted_cert->mpis[0 ], db_cert->mpis[0 ],sizeof (db_cert->mpis[0 ])) ==0 + &&grub_memcmp (distrusted_cert->mpis[1 ], db_cert->mpis[1 ],sizeof (db_cert->mpis[1 ])) ==0 ) +return true; + +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_printf ("Warning: a trusted certificate CN='%s' is ignored " +"because it is on the dbx list.\n" , db_cert->subject); +grub_free (grub_pks_keystore.dbx[i].data); +grub_memset (&grub_pks_keystore.dbx[i],0 ,sizeof (grub_pks_sd_t)); +certificate_release (distrusted_cert); +grub_free (distrusted_cert); +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 empty." ); + + 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 const char *grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),const char *val __attribute__ ((unused)))
@@ -241,7 +461,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);
@@ -272,7 +492,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize) datasize, i, hash[0], hash[1], hash[2], hash[3]); err =GRUB_ERR_BAD_SIGNATURE ; -for (pk = db; pk !=NULL ; pk = pk->next) +for (pk = db.certs; pk !=NULL ; pk = pk->next) { rc =grub_crypto_rsa_pad (&hashmpi, hash, si->hash, pk->mpis[0 ]);if (rc !=GPG_ERR_NO_ERROR )
@@ -357,11 +577,11 @@ grub_cmd_dbx (grub_command_t cmd __attribute__ ((unused)), int argc, char **argselse if (cert_num ==0 )return grub_error (GRUB_ERR_OUT_OF_RANGE ,"certificate numbers start at 1." ); -for (cert = prev_cert = db; cert !=NULL ; cert = cert->next) +for (cert = prev_cert = db.certs; cert !=NULL ; cert = cert->next) {if (cert_num ==1 )/* Match with first certificate in the db. */ { - db = cert->next; + db.certs = cert->next;break ; }else if (cert_num == i)
@@ -376,6 +596,7 @@ grub_cmd_dbx (grub_command_t cmd __attribute__ ((unused)), int argc, char **argsif (cert !=NULL ) { + db.cert_entries--;certificate_release (cert);grub_free (cert);return GRUB_ERR_NONE ;
@@ -413,8 +634,9 @@ grub_cmd_db (grub_command_t cmd __attribute__ ((unused)), int argc, char **args)grub_dprintf ("appendedsig" ,"loaded 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 ; }
@@ -427,7 +649,7 @@ grub_cmd_list (grub_command_t cmd __attribute__ ((unused)), int argc __attributeint 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: " );
@@ -518,6 +740,247 @@ static struct grub_fs pseudo_fs = {static grub_command_t cmd_verify, cmd_list, cmd_dbx, cmd_db; +/* Check the certificate hash presence in the 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_printf ("Warning: 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 ]); +grub_free (grub_pks_keystore.dbx[i].data); +grub_memset (&grub_pks_keystore.dbx[i],0 ,sizeof (grub_pks_keystore.dbx[i])); +return true; + } + } + +return false; +} + +/* Check the binary hash presence in the 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_printf ("Warning: 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 ]); +grub_free (grub_pks_keystore.dbx[i].data); +grub_memset (&grub_pks_keystore.dbx[i],0 ,sizeof (grub_pks_keystore.dbx[i])); +return true; + } + } + +return false; +} + +/* Add the binary hash to the db list if it does not exist in the 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 empty." ); + +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_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 ) +return true; + +/* GUID type of the certificate hash. */ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_GUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_GUID_SIZE ) ==0 ) +return true; + +return false; +} + +/* Add the X.509 certificates/binary hash to the db list. */ +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. */ +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 (grub_pks_keystore.dbx[i].data !=NULL || + grub_pks_keystore.dbx[i].data_size >0 ) + { +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_all (&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;
@@ -527,7 +990,6 @@ GRUB_MOD_INIT (appendedsig)if (grub_is_lockdown () ==GRUB_LOCKDOWN_ENABLED ) check_sigs =CHECK_SIGS_FORCED ; - db =NULL ;grub_register_variable_hook ("check_appended_signatures" , grub_env_read_sec, grub_env_write_sec);grub_env_export ("check_appended_signatures" );
@@ -535,37 +997,49 @@ GRUB_MOD_INIT (appendedsig)if (rc)grub_fatal ("error initing ASN.1 data structures: %d: %s\n" , rc,asn1_strerror (rc)); -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); - - pk =grub_zalloc (sizeof (struct x509_certificate )); -if (pk ==NULL ) -grub_fatal ("out of memory loading initial certificates." ); - - 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); +if (!grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) + { +FOR_MODULES (header) + { +/* Not an ELF module, skip. */ +if (header->type !=OBJ_TYPE_X509_PUBKEY ) +continue ; - pk->next = db; - db = pk; - } + 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); + } + } +else if (grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) + { + 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 =grub_register_command ("append_add_db_cert" , grub_cmd_db,N_ ("X509_CERTIFICATE" ),N_ ("Add trusted X509_CERTIFICATE to the db" ));
include/grub/crypto.h +1 -0
@@ -369,6 +369,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
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v4 18/23 appendedsig: verify the kernel using db and dbx lists
In reply to Sudhakar KuppusamyTo verify the kernel's signature: verify the kernel binary against lists of binary hashes that are either in dbx or db list. If it is not list in either db or dbx list then the trusted keys from the db list are used to verify the signature.
--- grub-core/commands/appendedsig/appendedsig.c | 173 ++++++++++++++----- 1 file changed, 126 insertions(+), 47 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +126 -47
@@ -447,6 +447,77 @@ extract_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize,return parse_pkcs7_signedData (appsigdata, pkcs7_size, &sig->pkcs7); } +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_GUID_SIZE ); +else if (binary_hash_size ==48 ) +grub_memcpy (&guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_GUID_SIZE ); +else if (binary_hash_size ==64 ) +grub_memcpy (&guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_GUID_SIZE ); +else + { +grub_dprintf ("appendedsig" ,"unsupported hash type (%" PRIuGRUB_SIZE") and " +"skipped the binary hash\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 binary 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 binary hash (%02x%02x%02x%02x)\n" , + hash[0 ], hash[1 ], hash[2 ], hash[3 ]); +return GRUB_ERR_NONE ; + } + } + +return GRUB_ERR_EOF ; +} + +/* Verify the kernel's integrity using db and dbx list */ static grub_err_t grub_verify_appended_signature (const grub_uint8_t *buf,grub_size_t bufsize) {
@@ -461,7 +532,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)struct pkcs7_signerInfo *si;int i; -if (!db.cert_entries) +if (!db.cert_entries && !db.signature_entries)return grub_error (GRUB_ERR_BAD_SIGNATURE ,"no trusted keys to verify against." ); err =extract_appended_signature (buf, bufsize, &sig);
@@ -469,59 +540,67 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)return err; datasize = bufsize - sig.signature_len; - -for (i =0 ; i < sig.pkcs7.signerInfo_count; i++) +/* Verify binary hash against the db and dbx list. */ + err =verify_binary_hash (buf, datasize); +if (err ==GRUB_ERR_BAD_SIGNATURE ) { -/* -* This could be optimised in a couple of ways: -* - we could only compute hashes once per hash type. -* - we could track signer information and only verify where IDs match. -* For now we do the naive O(trusted keys * pkcs7 signers) approach. -*/ - si = &sig.pkcs7.signerInfos[i]; - context =grub_zalloc (si->hash->contextsize); -if (context ==NULL ) -return grub_errno; - - si->hash->init (context); - si->hash->write (context, buf, datasize); - si->hash->final (context); - hash = si->hash->read (context); - -grub_dprintf ("appendedsig" ,"data size %" PRIxGRUB_SIZE", signer %d hash %02x%02x%02x%02x...\n" , - datasize, i, hash[0 ], hash[1 ], hash[2 ], hash[3 ]); - - err =GRUB_ERR_BAD_SIGNATURE ; -for (pk = db.certs; pk !=NULL ; pk = pk->next) + err =grub_error (err,"failed to verify the signature because this binary is in the dbx list.\n" ); + goto cleanup; + } +else + { +for (i =0 ; i < sig.pkcs7.signerInfo_count; i++) { - rc =grub_crypto_rsa_pad (&hashmpi, hash, si->hash, pk->mpis[0 ]); -if (rc !=GPG_ERR_NO_ERROR ) +/* +* This could be optimised in a couple of ways: +* - we could only compute hashes once per hash type. +* - we could track signer information and only verify where IDs match. +* For now we do the naive O(trusted keys * pkcs7 signers) approach. +*/ + si = &sig.pkcs7.signerInfos[i]; + context =grub_zalloc (si->hash->contextsize); +if (context ==NULL ) +return grub_errno; + + si->hash->init (context); + si->hash->write (context, buf, datasize); + si->hash->final (context); + hash = si->hash->read (context); + +grub_dprintf ("appendedsig" ,"data size %" PRIxGRUB_SIZE", signer %d hash %02x%02x%02x%02x...\n" , + datasize, i, hash[0 ], hash[1 ], hash[2 ], hash[3 ]); + + err =GRUB_ERR_BAD_SIGNATURE ; +for (pk = db.certs; pk !=NULL ; pk = pk->next) { - err =grub_error (GRUB_ERR_BAD_SIGNATURE , -"error padding hash for RSA verification: %d" , (int ) rc); -grub_free (context); - goto cleanup; + rc =grub_crypto_rsa_pad (&hashmpi, hash, si->hash, pk->mpis[0 ]); +if (rc !=GPG_ERR_NO_ERROR ) + { + err =grub_error (GRUB_ERR_BAD_SIGNATURE , +"error padding hash for RSA verification: %d" , (int ) rc); +grub_free (context); + goto cleanup; + } + + rc = grub_crypto_pk_rsa->verify (0 , hashmpi, &si->sig_mpi, pk->mpis,NULL ,NULL ); +gcry_mpi_release (hashmpi); +if (rc ==GPG_ERR_NO_ERROR ) + { +grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' succeeded.\n" , + i, pk->subject); + err =GRUB_ERR_NONE ; +break ; + } + +grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' failed with %d\n" , + i, pk->subject, (int ) rc); } - rc = grub_crypto_pk_rsa->verify (0 , hashmpi, &si->sig_mpi, pk->mpis,NULL ,NULL ); -gcry_mpi_release (hashmpi); -if (rc ==GPG_ERR_NO_ERROR ) - { -grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' succeeded.\n" , - i, pk->subject); - err =GRUB_ERR_NONE ; -break ; - } - -grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' failed with %d\n" , - i, pk->subject, (int ) rc); +grub_free (context); +if (err ==GRUB_ERR_NONE ) +break ; } - -grub_free (context); -if (err ==GRUB_ERR_NONE ) -break ; } -/* If we didn't verify, provide a neat message. */ if (err !=GRUB_ERR_NONE ) err = grub_error (GRUB_ERR_BAD_SIGNATURE,
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v4 19/23 powerpc_ieee1275: Introduce use_static_keys flag
In reply to Sudhakar KuppusamyIntroduce 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
@@ -40,7 +40,8 @@ static grub_size_t pks_max_object_size;grub_uint8_t grub_pks_use_keystore =0 ;/* 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 };/* Convert the esl data into the ESL. */ static grub_esl_t *
@@ -323,6 +324,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 ; +/* +* 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
@@ -108,6 +108,7 @@ struct grub_pksgrub_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
PATCH v4 20/23 appendedsig: Read default DB keys from the ELF Note
In reply to Sudhakar KuppusamyIf Secure Boot is enabled with PKS 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 | 59 ++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +43 -16
@@ -989,7 +989,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;
@@ -1008,6 +1008,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);
@@ -1060,6 +1066,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;
@@ -1078,26 +1103,28 @@ GRUB_MOD_INIT (appendedsig)if (!grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) { -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); }else if (grub_pks_use_keystore && check_sigs ==CHECK_SIGS_FORCED ) { - rc =create_db_list (); + +if (grub_pks_keystore.use_static_keys == true) + { +grub_printf ("Warning: 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
PATCH v4 21/23 appendedsig: Introduce GRUB commands to access db and dbx
In reply to Sudhakar KuppusamyIntroducing the following db and dbx commands
1. append_list_db:
It will show the list of trusted certificates and binary hashes
2. append_list_dbx:
It will show the list of distrusted certificates and binary/certificate hashes
3. append_add_db_cert:
It will add the trusted certificate to the db list
4. append_add_db_sig:
It will add the certificate/binary hash to the db list
5. append_rm_dbx_cert:
It will remove the trusted certificate from the db list
6. append_add_dbx_sig:
It will add the certificate/binary hash to the dbx list
Note:-
The addition/deletion of trusted certificates and binary hashes
are not allowed in GRUB command prompt while secure boot is enabled.
--- grub-core/commands/appendedsig/appendedsig.c | 416 +++++++++++++++---- 1 file changed, 337 insertions(+), 79 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +337 -79
@@ -103,6 +103,38 @@ static enum CHECK_SIGS_FORCED = 2 } check_sigs =CHECK_SIGS_NO ; +enum +{ +OPTION_BINARY_HASH =0 , +OPTION_CERT_HASH =1 +}; + +static const struct grub_arg_option options[] = +{ + {"binary-hash" ,'b' ,0 ,N_ ("hash file of the binary." ),0 ,ARG_TYPE_NONE }, + {"cert-hash" ,'c' ,1 ,N_ ("hash file of the certificate." ),0 ,ARG_TYPE_NONE }, + {0 ,0 ,0 ,0 ,0 ,0 } +}; + +static void +print_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]); +} +/* * GUID can be used to determine the hashing function and * generate the hash using determined hashing function.
@@ -616,132 +648,342 @@ static grub_err_t grub_cmd_verify_signature (grub_command_t cmd __attribute__ ((unused)), int argc, char **args) {grub_file_t signed_file; -grub_err_t err =GRUB_ERR_NONE ; -grub_uint8_t *data; -grub_size_t file_size; +grub_err_t err; +grub_uint8_t *signed_data; +grub_size_t signed_data_size;if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected." ); + { +grub_printf ("a signed file is expected.\n" +"Example:\n\tappend_verify <SIGNED FILE>\n" ); +return GRUB_ERR_BAD_ARGUMENT ; + }grub_dprintf ("appendedsig" ,"verifying %s\n" , args[0 ]); signed_file =grub_file_open (args[0 ],GRUB_FILE_TYPE_VERIFY_SIGNATURE );if (signed_file ==NULL ) -return grub_error (GRUB_ERR_BAD_FILE_TYPE ,"could not open %s file." , args[0 ]); +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"could not open %s file." , args[0 ]); - err =file_read_all (signed_file, &data, &file_size); + err =file_read_all (signed_file, &signed_data, &signed_data_size);if (err ==GRUB_ERR_NONE ) - err =grub_verify_appended_signature (data, file_size); + err =grub_verify_appended_signature (signed_data, signed_data_size);grub_file_close (signed_file); -grub_free (data); +grub_free (signed_data);return err; }static grub_err_t -grub_cmd_dbx (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +grub_cmd_list_db (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 = db.certs; cert; cert = cert->next) + { +grub_printf ("trusted 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 ("\tCN: %s\n\n" , cert->subject); + cert_num++; + } + +for (i =0 ; i < db.signature_entries; i++) + { +grub_printf ("trusted binary hash %" PRIuGRUB_SIZE":\n" , i +1 ); +grub_printf ("\thash: " ); +print_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))) { -unsigned long cert_num, i =0 ; -const char *end; -struct x509_certificate *cert, *prev_cert; +struct x509_certificate *cert; +grub_size_t i, cert_num =1 ; + +for (cert = dbx.certs; cert; cert = cert->next) + { +grub_printf ("distrusted 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 ("\tCN: %s\n\n" , cert->subject); + cert_num++; + } + +for (i =0 ; i < dbx.signature_entries; i++) + { +grub_printf ("distrusted certificate/binary hash %" PRIuGRUB_SIZE":\n" , i +1 ); +grub_printf ("\thash: " ); +print_hex (dbx.signatures[i], dbx.signature_size[i]); + } + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_cmd_db_cert (grub_command_t cmd __attribute__((unused)),int argc,char **args) +{ +grub_err_t err; +grub_file_t cert_file; +grub_uint8_t *cert_data =NULL ; +grub_size_t cert_data_size =0 ;if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected." ); + { +grub_printf ("a trusted X.509 certificate file is expected in DER format.\n" +"Example:\n\tappend_add_db_cert <CERT FILE>\n" ); +return GRUB_ERR_BAD_ARGUMENT ; + } - cert_num =grub_strtoul (args[0 ], &end,10 ); -if (*(args[0 ]) =='\0' || *end !='\0' ) -return grub_error (GRUB_ERR_BAD_ARGUMENT , -"non-numeric or certificate number is invalid %s" , args[0 ]); -else if (cert_num ==0 ) -return grub_error (GRUB_ERR_OUT_OF_RANGE ,"certificate numbers start at 1." ); +if (check_sigs ==CHECK_SIGS_FORCED ) + { +grub_printf ("Warning: since secure boot is enabled, " +"adding of trusted X.509 certificate is not permitted!\n" ); +return GRUB_ERR_ACCESS_DENIED ; + } -for (cert = prev_cert = db.certs; cert !=NULL ; cert = cert->next) +if (grub_strlen (args[0 ]) ==0 ) +return grub_error (GRUB_ERR_BAD_FILENAME ,"missing trusted 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_FILE_NOT_FOUND ,"unable to open the trusted X.509 certificate file." ); + + err =file_read_all (cert_file, &cert_data, &cert_data_size); +if (err !=GRUB_ERR_NONE ) + goto cleanup; + + err =add_certificate (cert_data, cert_data_size, &db, true); +if (err !=GRUB_ERR_NONE ) { -if (cert_num ==1 )/* Match with first certificate in the db. */ - { - db.certs = cert->next; -break ; - } -else if (cert_num == i) - { - prev_cert->next = cert->next; -break ; - } +free_db_list (); +free_dbx_list (); + err =grub_error (err,"adding of trusted certificate failed." ); + } + + cleanup: +grub_file_close (cert_file); +grub_free (cert_data); - prev_cert = cert; - i++; +return err; +} + +static grub_err_t +grub_cmd_db_sig (grub_command_t cmd __attribute__((unused)),int argc,char **args) +{ +grub_err_t rc; +grub_file_t hash_file; +grub_uint8_t *hash_data =NULL ; +grub_size_t hash_data_size =0 ; + +if (argc !=1 ) + { +grub_printf ("a trusted binary hash file is expected.\n" +"Example:\n\tappend_add_db_sig <BINARY HASH FILE>\n" ); +return GRUB_ERR_BAD_ARGUMENT ; } -if (cert !=NULL ) +if (check_sigs ==CHECK_SIGS_FORCED ) { - db.cert_entries--; -certificate_release (cert); -grub_free (cert); -return GRUB_ERR_NONE ; +grub_printf ("Warning: since secure boot is enabled, " +"adding of trusted binary hash is not permitted!\n" ); +return GRUB_ERR_ACCESS_DENIED ; } -return grub_error (GRUB_ERR_BAD_ARGUMENT , -"not found certificate number %lu in the db." , cert_num); +if (grub_strlen (args[0 ]) ==0 ) +return grub_error (GRUB_ERR_BAD_FILENAME ,"missing trusted binary hash file" ); + + hash_file =grub_file_open (args[0 ],GRUB_FILE_TYPE_TO_HASH |GRUB_FILE_TYPE_NO_DECOMPRESS ); +if (hash_file ==NULL ) +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"unable to open the trusted binary hash file" ); + + rc =file_read_all (hash_file, &hash_data, &hash_data_size); +if (rc !=GRUB_ERR_NONE ) + goto cleanup; + +grub_dprintf ("appendedsig" ,"adding a trusted binary hash %s\n with size of %" PRIuGRUB_SIZE"\n" , + hash_data, hash_data_size); + +/* Only accept SHA256, SHA384 and SHA512 binary hash */ +if (hash_data_size !=32 && hash_data_size !=48 && hash_data_size !=64 ) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"unacceptable trusted binary hash type." ); + + 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." ); + } + + cleanup: +grub_file_close (hash_file); +grub_free (hash_data); + +return rc; }static grub_err_t -grub_cmd_db (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +grub_cmd_dbx_cert (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) { -grub_file_t certf; -struct x509_certificate *cert =NULL ; +grub_size_t i;grub_err_t err; +grub_file_t cert_file; +struct x509_certificate *current_cert = db.certs; +struct x509_certificate *previous_cert =NULL ; +struct x509_certificate *cert =NULL ; +bool is_cert_found = false;if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected." ); + { +grub_printf ("a distrusted X.509 certificate file is expected in DER format.\n" +"Example:\n\tappend_rm_dbx_cert <CERT FILE>\n" ); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (check_sigs ==CHECK_SIGS_FORCED ) + { +grub_printf ("Warning: since secure boot is enabled, " +"adding of distrusted X.509 certificate is not permitted!\n" ); +return GRUB_ERR_ACCESS_DENIED ; + } - certf =grub_file_open (args[0 ],GRUB_FILE_TYPE_CERTIFICATE_TRUST |GRUB_FILE_TYPE_NO_DECOMPRESS ); -if (certf ==NULL ) -return grub_error (GRUB_ERR_BAD_FILE_TYPE ,"could not open %s file." , args[0 ]); +if (grub_strlen (args[0 ]) ==0 ) +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_FILE_NOT_FOUND ,"unable to open the distrusted X.509 certificate file." ); cert =grub_zalloc (sizeof (struct x509_certificate ));if (cert ==NULL )return grub_error (GRUB_ERR_OUT_OF_MEMORY ,"could not allocate memory for certificate." ); - err =read_cert_from_file (certf, cert); -grub_file_close (certf); + err =read_cert_from_file (cert_file, cert); +grub_file_close (cert_file);if (err !=GRUB_ERR_NONE ) {grub_free (cert);return err; } -grub_dprintf ("appendedsig" ,"loaded certificate with CN: %s\n" , cert->subject); +for (i =0 ; i <= db.cert_entries; i++) + { +if (is_cert_match (current_cert, cert) == true) + { +if (i ==0 )/* Match with first certificate in the db list. */ + db.certs = current_cert->next; +else + previous_cert->next = current_cert->next; - cert->next = db.certs; - db.certs = cert; - db.cert_entries++; + current_cert->next =NULL ; +certificate_release (current_cert); +grub_free (current_cert); + db.cert_entries--; + is_cert_found = true; +break ; + } +else + { + previous_cert = current_cert; + current_cert = current_cert->next; + } + } -return GRUB_ERR_NONE ; +if (is_cert_found == false) +return grub_error (GRUB_ERR_UNKNOWN_COMMAND , +"not found certificate CN=%s in the db list." , cert->subject); + +return err; }static grub_err_t -grub_cmd_list (grub_command_t cmd __attribute__ ((unused)),int argc __attribute__ ((unused)), -char **args __attribute__ ((unused))) +grub_cmd_dbx_sig (grub_extcmd_context_t ctxt,int argc,char **args) { -struct x509_certificate *cert; -int cert_num =1 ; -grub_size_t i; +grub_err_t rc; +grub_file_t hash_file =NULL ; +grub_uint8_t *hash_data =NULL ; +grub_size_t hash_data_size =0 ; -for (cert = db.certs; cert !=NULL ; cert = cert->next) +if (argc !=2 ) { -grub_printf ("Certificate %d:\n" , cert_num); -grub_printf ("\tSerial: " ); +grub_printf ("a distrusted certificate/binary hash file is expected\n" +"Example:\n\tappend_rm_dbx_sig [option] <FILE>\n" +"option:\n[-b|--binary-hash] FILE [BINARY HASH FILE]\n" +"[-c|--cert-hash] FILE [CERTFICATE HASH FILE]\n" ); +return GRUB_ERR_BAD_ARGUMENT ; + } -for (i =0 ; i < cert->serial_len -1 ; i++) -grub_printf ("%02x:" , cert->serial[i]); +if (check_sigs ==CHECK_SIGS_FORCED ) + { +grub_printf ("Warning: since secure boot is enabled, " +"adding of distrusted certificate/binary hash is not permitted!\n" ); +return GRUB_ERR_ACCESS_DENIED ; + } -grub_printf ("%02x\n" , cert->serial[cert->serial_len -1 ]); -grub_printf ("\tCN: %s\n\n" , cert->subject); - cert_num++; +if (!ctxt->state[OPTION_BINARY_HASH ].set && !ctxt->state[OPTION_CERT_HASH ].set) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"missing options and use --help to konw." ); + +if (grub_strlen (args[1 ]) ==0 ) +return grub_error (GRUB_ERR_BAD_FILENAME ,"missing distrusted certificate/binary hash file." ); + + hash_file =grub_file_open (args[1 ],GRUB_FILE_TYPE_TO_HASH |GRUB_FILE_TYPE_NO_DECOMPRESS ); +if (hash_file ==NULL ) +return grub_error (GRUB_ERR_FILE_NOT_FOUND , +"unable to open the distrusted certificate/binary hash file." ); + + rc =file_read_all (hash_file, &hash_data, &hash_data_size); +if (rc !=GRUB_ERR_NONE ) + goto cleanup; + +grub_dprintf ("appendedsig" ,"adding a distrusted certificate/binary hash %s\n" +" with size of %" PRIuGRUB_SIZE"\n" , hash_data, hash_data_size); + +if (ctxt->state[OPTION_BINARY_HASH ].set) + { +/* Only accept SHA256, SHA384 and SHA512 binary hash */ +if (hash_data_size !=32 && hash_data_size !=48 && hash_data_size !=64 ) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"unacceptable distrusted binary hash type." ); + } +else if (ctxt->state[OPTION_CERT_HASH ].set) + { +/* Only accept SHA256, SHA384 and SHA512 certificate hash */ +if (hash_data_size !=32 && hash_data_size !=48 && hash_data_size !=64 ) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"unacceptable distrusted certificate hash type." ); } -return GRUB_ERR_NONE ; + 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." ); + } + + cleanup: +grub_file_close (hash_file); +grub_free (hash_data); + +return rc; } static grub_err_t
@@ -817,8 +1059,6 @@ static struct grub_fs pseudo_fs = { .fs_read = pseudo_read }; -static grub_command_t cmd_verify, cmd_list, cmd_dbx, cmd_db; -/* Check the certificate hash presence in the dbx list. */ static bool is_dbx_cert_hash (const grub_uint8_t *data,const grub_size_t data_size)
@@ -1085,6 +1325,10 @@ load_static_keys (const struct grub_module_header *header, const bool is_pks)return rc; } +static grub_extcmd_t cmd_dbx_sig; +static grub_command_t cmd_verify, cmd_list_db, cmd_db_cert, cmd_db_sig, + cmd_list_dbx, cmd_dbx_cert; +GRUB_MOD_INIT (appendedsig) {int rc;
@@ -1147,14 +1391,25 @@ GRUB_MOD_INIT (appendedsig)grub_pks_free_keystore (); } - cmd_db =grub_register_command ("append_add_db_cert" , grub_cmd_db,N_ ("X509_CERTIFICATE" ), -N_ ("Add trusted X509_CERTIFICATE to the db" )); - cmd_list =grub_register_command ("append_list_db" , grub_cmd_list,0 , -N_ ("Show the list of trusted X.509 certificates from the db" )); + cmd_db_cert =grub_register_command ("append_add_db_cert" , grub_cmd_db_cert,N_ ("X509_CERTIFICATE" ), +N_ ("Add X509_CERTIFICATE to the db list." )); + cmd_db_sig =grub_register_command ("append_add_db_sig" , grub_cmd_db_sig,N_ ("BINARY HASH FILE" ), +N_ ("Add trusted BINARY HASH to the db list." )); + cmd_dbx_cert =grub_register_command ("append_rm_dbx_cert" , grub_cmd_dbx_cert,N_ ("X509_CERTIFICATE" ), +N_ ("Remove X509_CERTIFICATE from the db list." )); + cmd_dbx_sig =grub_register_extcmd ("append_add_dbx_sig" , grub_cmd_dbx_sig,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 db list." ), options); + cmd_list_db =grub_register_command ("append_list_db" , grub_cmd_list_db,0 , +N_ ("Show the list of trusted x509 certificates and" +" trusted binary hashes from 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_verify =grub_register_command ("append_verify" , grub_cmd_verify_signature,N_ ("FILE" ), -N_ ("Verify FILE against the trusted X.509 certificates in the db" )); - cmd_dbx =grub_register_command ("append_rm_dbx_cert" , grub_cmd_dbx,N_ ("CERT_NUMBER" ), -N_ ("Remove CERT_NUMBER (as listed by append_list_db) from the db" )); +N_ ("Verify FILE against the trusted x509 certificates/" +"trusted binary hashes." ));grub_verifier_register (&grub_appendedsig_verifier);grub_dl_set_persistent (mod);
@@ -1168,7 +1423,10 @@ GRUB_MOD_FINI (appendedsig)*/ grub_verifier_unregister (&grub_appendedsig_verifier);grub_unregister_command (cmd_verify); -grub_unregister_command (cmd_list); -grub_unregister_command (cmd_db); -grub_unregister_command (cmd_dbx); +grub_unregister_command (cmd_list_db); +grub_unregister_command (cmd_list_dbx); +grub_unregister_command (cmd_db_cert); +grub_unregister_command (cmd_db_sig); +grub_unregister_command (cmd_dbx_cert); +grub_unregister_extcmd (cmd_dbx_sig); }
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v4 22/23 appendedsig: Documentation
In reply to Sudhakar KuppusamyThis explains how static and dynamic key appended signatures can be used to form part of a secure boot chain, and documents the commands and variables introduced.
--- docs/grub.texi | 90 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 28 deletions(-)
docs/grub.texi +62 -28
@@ -6420,9 +6420,12 @@ you forget a command, you can run the command @command{help} * [:: Check file types and compare values * acpi:: Load ACPI tables * append_add_db_cert:: Add an X.509 certificate to the db list -* append_list_db:: List trusted certificates from the db list +* append_add_db_sig:: Add an X.509 certificate/binary hash to the db list +* append_add_dbx_sig:: Add an X.509 certificate/binary hash to the dbx list +* append_list_db:: List trusted certificates/binary hashes from the db list +* append_list_dbx:: List certificates and binary/certificate hashes from the dbx list * append_rm_dbx_cert:: Remove a certificate from the db list -* append_verify:: Verify appended digital signature using db list +* append_verify:: Verify appended digital signature using db and dbx list * authenticate:: Check whether user is in user list * background_color:: Set background color for active terminal * background_image:: Load background image for active terminal
@@ -6563,16 +6566,48 @@ certificates themselves.) See @xref{Using appended signatures} for more information. @end deffn +@node append_add_db_sig +@subsection append_add_db_sig + +@deffn Command append_add_db_sig hash_file +Read a binary/certificate hash from the file @var{hash_file} +and add it to GRUB's internal db list. These hash are used to validate linux image +integrity if appended signatures validation failed when the environment variable +@code{check_appended_signatures} is set to @code{enforce}. + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_add_dbx_sig +@subsection append_add_dbx_sig + +@deffn Command append_add_dbx_sig hash_file +Read a binary/certificate hash from the file @var{hash_file} +and add it to GRUB's internal dbx list. These hash are used to restrict validation +of linux image integrity using db list if appended signatures validation failed +when the environment variable @code{check_appended_signatures} is set to @code{enforce}. + +See @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 trusted by GRUB for validating appended signatures. -The output is a numbered list of certificates, showing the certificate's serial -number and Common Name. +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 and Common Name. + +See @xref{Using appended signatures} for more information. +@end deffn + +@node append_list_dbx +@subsection append_list_dbx -The certificate number can be used as an argument to -@command{append_rm_dbx_cert} (@pxref{append_rm_dbx_cert}). +@deffn Command append_list_dbx +List all the distrusted x509 certificates and binary/certificate hashes. +The output is a numbered list of certificates and binary/certificate hashes, +showing the certificate's serial number and Common Name. See @xref{Using appended signatures} for more information. @end deffn
@@ -6580,29 +6615,22 @@ See @xref{Using appended signatures} for more information. @node append_rm_dbx_cert @subsection append_rm_dbx_cert -@deffn Command append_rm_dbx_cert cert_number -Remove the X.509 certificate numbered @var{cert_number} from GRUB's keyring of -db for verifying appended signatures. - -@var{cert_number} is the certificate number as listed by -@command{append_list_db} (@pxref{append_list_db}). +@deffn Command append_rm_dbx_cert X509_certificate +Read a DER-formatted X.509 certificate from the file @var{X509_certificate} +and remove this certificate from db list. -These certificates are used to validate appended signatures when environment -variable @code{check_appended_signatures} is set to @code{enforce} -(@pxref{check_appended_signatures}), and by @command{append_verify} -(@pxref{append_verify}). See @xref{Using appended signatures} for more -information. +See @xref{Using appended signatures} for more information. @end deffn @node append_verify @subsection append_verify -@deffn Command append_verify file -Verifies an appended signature on @var{file} against the trusted X.509 certificates -known to GRUB (See @pxref{append_list_db}, @pxref{append_add_db_cert}, and -@pxref{append_rm_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. +@deffn Command append_verify signed_file +Verifies an appended signature on @var{signed_file} against the trusted X.509 certificates +known to GRUB (See @pxref{append_list_db},@pxref{append_list_dbx}, @pxref{append_add_db_cert}, +@pxref{append_add_db_sig}, @pxref{append_add_dbx_sig}, and @pxref{append_rm_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. See @xref{Using appended signatures}, for more information. @end deffn
@@ -8925,10 +8953,16 @@ To enable appended signature verification, load the appendedsig module and an x509 certificate for verification. Building the appendedsig module into the core grub image is recommended. -Certificates can be managed at boot time using the @pxref{append_add_db_cert}, -@pxref{append_rm_dbx_cert} and @pxref{append_list_db} commands. -Certificates can also be built in to the core image using the @code{--x509} -parameter to @command{grub-install} or @command{grub-mkimage}. +For static key secure boot, certificates will be built in to the core image using +the @code{--x509} parameter to @command{grub-install} or @command{grub-mkimage}. +It allows listing the trusted certificates and binary hashes at boot time using +@pxref{append_list_db} command. + +For dynamic key secure boot, it loads the db and dbx from Platform KeyStore (PKS). +It allows listing the trusted certificates and binary hashes at boot time using +@pxref{append_list_db} and listing distrusted certificates and binary/certificate +hashes at boot time using @pxref{append_list_dbx} commands. + A file can be explicitly verified using the @pxref{append_verify} command. Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported,
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v4 23/23 appendedsig test: Replace the certificate number with an x.509 certificate
In reply to Sudhakar KuppusamyThe GRUB command append_rm_dbx_cert removing the certificate from the db list based on x.509 certificate instead of certificate number. So, replacing the certificate number with an x.509 certificate.
--- grub-core/tests/appended_signature_test.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
grub-core/tests/appended_signature_test.c +9 -11
@@ -104,8 +104,6 @@ appended_signature_test (void)char *trust_args2[] = { (char *)"(proc)/certificate2.der" ,NULL };char *trust_args_printable[] = { (char *)"(proc)/certificate_printable.der" ,NULL };char *trust_args_eku[] = { (char *)"(proc)/certificate_eku.der" ,NULL }; -char *distrust_args[] = { (char *)"1" ,NULL }; -char *distrust2_args[] = { (char *)"2" ,NULL };grub_err_t err;grub_procfs_register ("certificate.der" , &certificate_der_entry);
@@ -189,7 +187,7 @@ appended_signature_test (void)* removed by position in the list. Current the list looks like [#2, #1]. * * First test removing the second certificate in the db list, which is -* certificate #1, giving us just [#2]. +* certificate #1. */ cmd_distrust =grub_command_find ("append_rm_dbx_cert" );if (cmd_distrust ==NULL )
@@ -198,7 +196,7 @@ appended_signature_test (void)return ; } - err = (cmd_distrust->func) (cmd_distrust,1 , distrust2_args); + err = (cmd_distrust->func) (cmd_distrust,1 , trust_args);grub_test_assert (err ==GRUB_ERR_NONE ,"distrusting certificate 1 failed: %d: %s" , grub_errno, grub_errmsg);DO_TEST (hi_signed_2nd,1 );
@@ -211,18 +209,18 @@ appended_signature_test (void) grub_errno, grub_errmsg);DO_TEST (hi_signed,1 ); -/* Remove the first certificate in the db list, giving us just [#2]. */ - err = (cmd_distrust->func) (cmd_distrust,1 , distrust_args); -grub_test_assert (err ==GRUB_ERR_NONE ,"distrusting certificate 1 (first time) failed: %d: %s" , +/* Remove the first certificate in the db list, which is certificate #2. */ + err = (cmd_distrust->func) (cmd_distrust,1 , trust_args2); +grub_test_assert (err ==GRUB_ERR_NONE ,"distrusting certificate 2 (first time) failed: %d: %s" , grub_errno, grub_errmsg); -DO_TEST (hi_signed_2nd,1 ); -DO_TEST (hi_signed,0 ); +DO_TEST (hi_signed_2nd,0 ); +DO_TEST (hi_signed,1 );/* -* Remove the first certificate again, giving an empty db list. +* Remove the certificate#1 again, giving an empty db list. * append_verify should fail if there are no certificates to verify against. */ - err = (cmd_distrust->func) (cmd_distrust,1 , distrust_args); + err = (cmd_distrust->func) (cmd_distrust,1 , trust_args);grub_test_assert (err ==GRUB_ERR_NONE ,"distrusting certificate 1 (second time) failed: %d: %s" , grub_errno, grub_errmsg);DO_TEST (hi_signed_2nd,0 );
Signature · 7 lines
2.39.5 (Apple Git-154) _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel