Appended Signature Secure Boot Support for PowerPC
v3 cover Appended Signature Secure Boot Support for PowerPC
This patch set contains v3 of the consolidated version of the patch sets for secure boot using appended signatures on powerpc, rebased on top of git HEAD.
The v2 series is at
https://mail.gnu.org/archive/html/grub-devel/2025-03/msg00183.html
Changes since v2:
- Daniel Kiper review comments addressed:
- v3 patch 1: correct the unreliable check conditions, coding style and complain loudly if somebody attempts to generate PE image with appended signature.
reduce one step in signing process and add those in grub-install/grub-mkimage.
- v3 patch 3: added the GRUB signing process for single and multi signature.
- v3 patch 4: add fake grub_dl_is_persistent for the emu target.
- v3 patch 5: correct coding style.
- v3 patch 7, 8: Split these into two from v2 patch 7.
- v3 patch 9: correct the coding style and updated GNUTLS ASN.1 description files.
- v3 patch 10, 11, 12: Split these into three from v2 patch 9 and correct coding style.
- v3 patch 13: correct the unreliable check conditions and coding style.
- Gary Lin review comments addressed:
- v3 patch 13: the changes for libtasn1.h merged into the patch 10, 11, 12
- v3 patch 24: fixed the bug in grub_cmd_distrusted_cert function and appendedsig tests.
Also, correct the code style from patch-1 to patch 25
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 - 15).
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:
- patch 4 is a small fix to allow persistent modules to work on the emu target.
- patches 5 and 6 are small refactorings.
- patch 7 and 8 allows x509 certificates to be built in to the grub core in much the same way as PGP keys.
- patch 9 brings in the code from GNUTLS that allows us to parse PKCS#7 and x509 with libtasn1.
- patch 10, 11 and 12 is our PKCS#7 and x509 parser. They're minimal and fairly strict parsers that extract only the bits we need to verify the signatures.
- patch 13 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 14 adds tests, and patch 15 adds documentation.
3.) Enable lockdown if secure boot is enabled (Patch 16)
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 17 - 25)
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 17 is an exposes an interface in ieee1275 for reading secure boot variable db and dbx from Platform Keystore.
- patch 18 is a read secure boot variables such as db and dbx from PKS and extract certificates from ESL.
- patch 19 is creates the trusted and distrusted lists.
- patch 20 is verify the kernel using trusted and distrusted lists
- patch 21 sets the use_static_keys flag if DB not available in PKS, and patch 22 is reads the DB default keys from ELF Note and store it in trusted lists if use_static_keys flag is set.
- patch 23 adds trusted and distrusted commands, and patch 24 adds documentation.
- patch 25 adds trusted_certificate and distrusted_certificate commands in appendedsig tests.
Thanks to Daniel Kiper and Gary Lin for providing review comments on v2.
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
Alastair D'Silva (1):
grub-install: support embedding x509 certificates
Daniel Axtens (13):
docs/grub: Document signing GRUB under UEFI
docs/grub: Document signing GRUB with an appended signature
dl: provide a fake grub_dl_set_persistent and grub_dl_is_persistent
for the emu target
pgp: factor out rsa_pad
crypto: move storage for grub_crypto_pk_* to crypto.c 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 appended signatures: verification tests
appended signatures: documentation
ieee1275: enter lockdown based on /ibm,secure-boot
Rashmica Gupta (1):
powerpc-ieee1275: Add support for signing GRUB with an appended
signature
Sudhakar (1):
PGP: renames the OBJ_TYPE_PUBKEY
Sudhakar Kuppusamy (9):
ieee1275: Platform Keystore (PKS) Support
ieee1275: Read the DB and DBX secure boot variables
appendedsig: The creation of trusted and distrusted lists
appendedsig: While verifying the kernel, use trusted and distrusted
lists
powerpc_ieee1275: set use_static_keys flag
appendedsig: Reads the default DB keys from ELF Note
appendedsig: The grub command's trusted and distrusted support
appendedsig: documentation
appendedsig: correcting the grub commands in appended signature tests
docs/grub.texi | 290 +++- grub-core/Makefile.am | 2 + grub-core/Makefile.core.def | 32 + grub-core/commands/appendedsig/appendedsig.c | 1456 +++++++++++++++++ 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 | 954 +++++++++++ grub-core/commands/pgp.c | 34 +- grub-core/kern/file.c | 34 + grub-core/kern/ieee1275/init.c | 48 + grub-core/kern/powerpc/ieee1275/ieee1275.c | 141 ++ .../kern/powerpc/ieee1275/platform_keystore.c | 346 ++++ grub-core/lib/crypto.c | 4 + grub-core/lib/pkcs1_v15.c | 63 + grub-core/term/tparm.c | 1 - grub-core/tests/appended_signature_test.c | 248 +++ grub-core/tests/appended_signatures.h | 975 +++++++++++ grub-core/tests/lib/functional_test.c | 1 + include/grub/dl.h | 21 +- include/grub/file.h | 3 + include/grub/kernel.h | 3 +- include/grub/lockdown.h | 3 +- include/grub/pkcs1_v15.h | 27 + include/grub/powerpc/ieee1275/ieee1275.h | 18 + .../grub/powerpc/ieee1275/platform_keystore.h | 234 +++ include/grub/types.h | 11 + include/grub/util/install.h | 13 +- include/grub/util/mkimage.h | 4 +- util/grub-install-common.c | 44 +- util/grub-mkimage.c | 34 +- util/grub-mkimagexx.c | 40 +- util/mkimage.c | 46 +- 35 files changed, 6350 insertions(+), 71 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/pkcs1_v15.h create mode 100644 include/grub/powerpc/ieee1275/platform_keystore.h
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 1/25 powerpc-ieee1275: Add support for signing GRUB with an appended signature
From: Rashmica Gupta <[email protected]>
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 | 25 ++++++++++++++++++++--- util/grub-mkimage.c | 20 +++++++++++++++++++ util/grub-mkimagexx.c | 40 ++++++++++++++++++++++++++++++++++++- util/mkimage.c | 9 ++++++--- 6 files changed, 97 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 } +/* max_appended_signature_size = 4k key signature size x number of signature */ +#define GRUB_APPSIG_SIZE_MAX (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 +22 -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,21 @@ 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 : + grub_errno =GRUB_ERR_NONE ; + appsig_size =grub_strtoul (arg, &end,10 ); +if (grub_errno !=GRUB_ERR_NONE ) + { +grub_util_error (_ ("non-numeric or invalid appended signature size `%s'" ), arg); +return 0 ; + } +else if (appsig_size ==0 || appsig_size > (size_t )GRUB_APPSIG_SIZE_MAX ) + { +grub_util_error (_ ("appended signature size `%s' not in the range of 1 byte to %u byte" ), + arg,GRUB_APPSIG_SIZE_MAX ); +return 0 ; + } +return 1 ;default :return 0 ; }
@@ -683,9 +701,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 +717,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 +20 -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,22 @@ argp_parser (int key, char *arg, struct argp_state *state) arguments->note =1 ;break ; +case 'S' : + grub_errno =GRUB_ERR_NONE ; + arguments->appsig_size =grub_strtoul (arg, &end,10 ); +if (grub_errno !=GRUB_ERR_NONE ) + { +grub_util_error (_ ("non-numeric or invalid appended signature size `%s'" ), arg); +return 1 ; + } +else if (arguments->appsig_size ==0 || arguments->appsig_size > (size_t )GRUB_APPSIG_SIZE_MAX ) + { +grub_util_error (_ ("appended signature size `%s' not in the range of 1 byte to %u byte" ), + arg,GRUB_APPSIG_SIZE_MAX ); +return 1 ; + } +break ; +case 'm' :if (arguments->memdisk)free (arguments->memdisk);
@@ -330,6 +349,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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 2/25 docs/grub: Document signing GRUB under UEFI
In reply to SudhakarFrom: 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 3/25 docs/grub: Document signing GRUB with an appended signature
In reply to SudhakarFrom: Daniel Axtens <[email protected]>
Signing GRUB for firmware that verifies an appended signature is a bit fiddly. I don't want people to have to figure it out from scratch so document it here.
--- docs/grub.texi | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
docs/grub.texi +48 -0
@@ -9379,6 +9379,54 @@ 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 +# grub.key is your private key and certificate.der is your public key +# kernel.der is your kernel signing public key +# For single signature: +# Determine the size of the appended signature. It depends on the signing +# certificate and the hash algorithm +sign-file SHA256 grub.key certificate.der /dev/null /dev/null.sig +SIG_SIZE=`stat -c '%s' /dev/null.sig` +# Build a GRUB image with $SIG_SIZE reserved for the signature +grub-install --appended-signature-size $SIG_SIZE --modules="..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 +--appended-signature-size $SIG_SIZE --modules="..." ... +# sign the GRUB image with an appended signature +sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed +# For Multiple signature: +# Determine the size of the appended signature +openssl cms -sign -binary -nocerts -in /dev/null -signer certificate.pem -inkey grub.key +-signer certificate1.pem -inkey grub1.key -out /dev/null.p7s -outform DER -noattr -md sha256 +sign-file -s /dev/null.p7s sha256 /dev/null /dev/null /dev/null.signed +SIG_SIZE=`stat -c '%s' /dev/null.signed` +# Build a GRUB image with $SIG_SIZE reserved for the signature +grub-install --appended-signature-size $SIG_SIZE --modules="..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 --appended-signature-size $SIG_SIZE +--modules="..." ... +# generate raw signature +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 +# sign the 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 03/25] docs/grub: Document signing GRUB with an appended signature
Quoted 29 lines · Sudhakar
From: Daniel Axtens <[email protected]>
Signing GRUB for firmware that verifies an appended signature is a bit fiddly. I don't want people to have to figure it out from scratch so document it here.
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]>--- docs/grub.texi | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)docs/grub.texi +7
-0@@ -9379,6 +9379,54 @@ 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}.
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9398
Quoted 10 lines
docs/grub.texi +10
-0+An image can be signed this way using the @command{sign-file} command from +the Linux kernel: +@example +@group +# grub.key is your private key and certificate.der is your public key +# kernel.der is your kernel signing public key +# For single signature: +# Determine the size of the appended signature. It depends on the signing +# certificate and the hash algorithm +sign-file SHA256 grub.key certificate.der /dev/null /dev/null.sig
Please do not create random files in the /dev directory. And probably s/null.sig/empty.sig/...
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9399
Quoted 1 line
docs/grub.texi +1
-0+SIG_SIZE=`stat -c '%s' /dev/null.sig`
s/SIG_SIZE/EMPTY_SIG_SIZE/
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9410
Quoted 11 lines
docs/grub.texi +11
-0+# Build a GRUB image with $SIG_SIZE reserved for the signature +grub-install --appended-signature-size $SIG_SIZE --modules="..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 +--appended-signature-size $SIG_SIZE --modules="..." ... +# sign the GRUB image with an appended signature +sign-file SHA256 grub.key certificate.der core.elf.unsigned core.elf.signed +# For Multiple signature: +# Determine the size of the appended signature +openssl cms -sign -binary -nocerts -in /dev/null -signer certificate.pem -inkey grub.key +-signer certificate1.pem -inkey grub1.key -out /dev/null.p7s -outform DER -noattr -md sha256
You glue all lines together and it is not possible to get where a command starts and ends. If you want break lines the two lines above should be presented in the docs in the following way:
openssl cms -sign -binary -nocerts -in /dev/null -signer certificate.pem -inkey grub.key \
-signer certificate1.pem -inkey grub1.key -out /dev/null.p7s -outform DER -noattr -md sha256
In general I suggest to add empty line after each command.
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9411
Quoted 1 line
docs/grub.texi +1
-0+sign-file -s /dev/null.p7s sha256 /dev/null /dev/null /dev/null.signed
Again, please do not pollute /dev directory.
docs/grub.texi image works under UEFI secure boot and can maintain the secure-boot chain. It · line 9420
Quoted 9 lines
docs/grub.texi +9
-0+SIG_SIZE=`stat -c '%s' /dev/null.signed` +# Build a GRUB image with $SIG_SIZE reserved for the signature +grub-install --appended-signature-size $SIG_SIZE --modules="..." ... + or +grub-mkimage -O powerpc-ieee1275 -o core.elf.unsigned -x kernel.der -p /grub2 --appended-signature-size $SIG_SIZE +--modules="..." ... +# generate raw signature +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
Same problem. Missing line continuation as above. Please fix similar problems everywhere...
And I suggest to add a command to remove all temporary files.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 4/25 dl: provide a fake grub_dl_set_persistent and grub_dl_is_persistent for the emu target
In reply to SudhakarFrom: Daniel Axtens <[email protected]>
Trying to start grub-emu with a module that calls grub_dl_set_persistent and grub_dl_is_persistent will crash because grub-emu fakes modules and passes NULL to the module init function.
Provide an empty function for the emu case.
--- include/grub/dl.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
include/grub/dl.h +20 -1
@@ -242,6 +242,25 @@ grub_dl_get (const char *name)return 0 ; } +#ifdef GRUB_MACHINE_EMU +/* +* Under grub-emu, modules are faked and NULL is passed to GRUB_MOD_INIT. +* So we fake this out to avoid a NULL deref. +*/ +static inline void +grub_dl_set_persistent (grub_dl_t mod __attribute__((unused))) +{ +} + +/* +* Under grub-emu, modules are faked and NULL is passed to GRUB_MOD_INIT. +* So we fake this out to avoid a NULL deref. +*/ +static inline int +grub_dl_is_persistent (grub_dl_t mod __attribute__((unused))) +{ +} +#else static inline void grub_dl_set_persistent (grub_dl_t mod) {
@@ -253,7 +272,7 @@ grub_dl_is_persistent (grub_dl_t mod) {return mod->persistent; } - +#endif #endif grub_err_t grub_dl_register_symbol (const char *name, void *addr,
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 04/25] dl: provide a fake grub_dl_set_persistent and grub_dl_is_persistent for the emu target
Quoted 6 lines · Sudhakar
From: Daniel Axtens <[email protected]>
Trying to start grub-emu with a module that calls
grub_dl_set_persistentandgrub_dl_is_persistentwill crash because grub-emu fakes modules and passes NULL to the module init function.
Quoted 7 lines
Provide an empty function for the emu case.
Fixes ee7808e2197c (dl: Add support for persistent modules)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]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 5/25 pgp: factor out rsa_pad
In reply to SudhakarFrom: 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 | 63 +++++++++++++++++++++++++++++++++++++ include/grub/pkcs1_v15.h | 27 ++++++++++++++++ 4 files changed, 101 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 +63 -0
63 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 6/25 crypto: move storage for grub_crypto_pk_* to crypto.c
In reply to SudhakarFrom: 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 7/25 PGP: renames the OBJ_TYPE_PUBKEY
In reply to Sudhakarrenames the OBJ_TYPE_PUBKEY to OBJ_TYPE_GPG_PUBKEY
--- 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 07/25] PGP: renames the OBJ_TYPE_PUBKEY
Quoted 1 line · Sudhakar
renames the
OBJ_TYPE_PUBKEYtoOBJ_TYPE_GPG_PUBKEY
Missing an explanation why this patch is needed...
Quoted 5 lines
Signed-off-by Alastair D'Silva <[email protected]>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]>
You should not retain at least RBs here. AFAICT this patch has not been reviewed on the mailing list. SOBs are less questionable here but...
Anyway, you can add my RB if you expand the commit message and remove Stefan's and Avnish's RBs.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 8/25 grub-install: support embedding x509 certificates
In reply to SudhakarFrom: Alastair D'Silva <[email protected]>
To support verification of appended signatures, we need a way to embed the necessary public keys. Existing appended signature schemes in the Linux kernel use X.509 certificates, so allow certificates to be embedded in the GRUB core image in the same way as PGP keys.
--- include/grub/kernel.h | 1 + include/grub/util/install.h | 3 +++ util/grub-install-common.c | 19 ++++++++++++++++++- util/grub-mkimage.c | 12 +++++++++++- util/mkimage.c | 35 ++++++++++++++++++++++++++++++++++- 5 files changed, 67 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++;
@@ -650,6 +656,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);
@@ -690,6 +699,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++ =' ' ;
@@ -716,7 +733,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;
@@ -217,6 +220,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);
@@ -347,7 +356,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 +34 -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,20 @@ grub_install_generate_image (const char *dir, const char *prefix, } } + { +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 +1079,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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 08/25] grub-install: support embedding x509 certificates
Quoted 7 lines · Sudhakar
From: Alastair D'Silva <[email protected]>
To support verification of appended signatures, we need a way to embed the necessary public keys. Existing appended signature schemes in the Linux kernel use X.509 certificates, so allow certificates to be embedded in the GRUB core image in the same way as PGP keys.
Quoted 5 lines
Signed-off-by Alastair D'Silva <[email protected]>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]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 9/25 appended signatures: import GNUTLS's ASN.1 description files
In reply to SudhakarFrom: Daniel Axtens <[email protected]>
In order to parse PKCS#7 messages and X.509 certificates with libtasn1, we need some information about how they are encoded.
We get these from GNUTLS, which has the benefit that they support the features we need and are well tested.
The GNUTLS files are from:
- https://github.com/gnutls/gnutls/blob/master/lib/gnutls.asn
- https://github.com/gnutls/gnutls/blob/master/lib/pkix.asn
The GNUTLS license is LGPLv2.1+, which is GPLv3 compatible, allowing us to import it without issue.
--- .../commands/appendedsig/gnutls_asn1_tab.c | 148 ++++++ .../commands/appendedsig/pkix_asn1_tab.c | 485 ++++++++++++++++++ 2 files changed, 633 insertions(+) create mode 100644 grub-core/commands/appendedsig/gnutls_asn1_tab.c create mode 100644 grub-core/commands/appendedsig/pkix_asn1_tab.c
grub-core/commands/appendedsig/gnutls_asn1_tab.c new file +148 -0
@@ -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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 09/25] appended signatures: import GNUTLS's ASN.1 description files
Quoted 13 lines · Sudhakar
From: Daniel Axtens <[email protected]>
In order to parse PKCS#7 messages and X.509 certificates with libtasn1, we need some information about how they are encoded.
We get these from GNUTLS, which has the benefit that they support the features we need and are well tested.
The GNUTLS files are from:
- https://github.com/gnutls/gnutls/blob/master/lib/gnutls.asn
- https://github.com/gnutls/gnutls/blob/master/lib/pkix.asn
Quoted 7 lines
The GNUTLS license is LGPLv2.1+, which is GPLv3 compatible, allowing us to import it without issue.
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]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 10/25 appended signatures: parse ASN1 node
In reply to SudhakarFrom: Daniel Axtens <[email protected]>
This code allows us to parse ASN1 node and allocating memory to store it. It will work for anything where the size libtasn1 returns is right:
- Integers
- Octet strings
- DER encoding of other structures
It will _not_ work for things where libtasn1 size requires adjustment:
- Strings that require an extra NULL byte at the end
- Bit strings because libtasn1 returns the length in bits, not bytes.
If the function returns a non-NULL value, the caller must free it.
--- grub-core/commands/appendedsig/appendedsig.h | 42 +++++++++ grub-core/commands/appendedsig/asn1util.c | 96 ++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 grub-core/commands/appendedsig/appendedsig.h create mode 100644 grub-core/commands/appendedsig/asn1util.c
grub-core/commands/appendedsig/appendedsig.h new file +42 -0
@@ -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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 10/25] appended signatures: parse ASN1 node
Quoted 12 lines · Sudhakar
From: Daniel Axtens <[email protected]>
This code allows us to parse ASN1 node and allocating memory to store it. It will work for anything where the size libtasn1 returns is right:
- Integers
- Octet strings
- DER encoding of other structures
It will _not_ work for things where libtasn1 size requires adjustment:
- Strings that require an extra NULL byte at the end
- Bit strings because libtasn1 returns the length in bits, not bytes.
If the function returns a non-NULL value, the caller must free it.
Quoted 6 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]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 11/25 appended signatures: parse PKCS#7 signedData
In reply to SudhakarFrom: Daniel Axtens <[email protected]>
This code allows us to parse:
- PKCS#7 signedData messages. Only a single signerInfo is supported, which is all that the Linux sign-file utility supports creating out-of-the-box. Only RSA, SHA-256 and SHA-512 are supported. Any certificate embedded in the PKCS#7 message will be ignored.
--- grub-core/commands/appendedsig/appendedsig.h | 36 ++ grub-core/commands/appendedsig/pkcs7.c | 454 +++++++++++++++++++ 2 files changed, 490 insertions(+) create mode 100644 grub-core/commands/appendedsig/pkcs7.c
grub-core/commands/appendedsig/appendedsig.h +36 -0
@@ -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.Add commentMore actions */ +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.Add commentMore actions +* 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 11/25] appended signatures: parse PKCS#7 signedData
Quoted 47 lines · Sudhakar
From: Daniel Axtens <[email protected]>
This code allows us to parse:
- PKCS#7 signedData messages. Only a single signerInfo is supported, which is all that the Linux sign-file utility supports creating out-of-the-box. Only RSA, SHA-256 and SHA-512 are supported. Any certificate embedded in the PKCS#7 message will be ignored.
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]>--- 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.cgrub-core/commands/appendedsig/appendedsig.h +13
-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.Add commentMore actions */ +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
grub-core/commands/appendedsig/appendedsig.h line 47
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.h +10
-0+* the signer. +*/ +struct pkcs7_signedData +{ +int signerInfo_count; +struct pkcs7_signerInfo *signerInfos; +}; + +/* +* Parse a PKCS#7 message, which must be a signedData message.Add commentMore actions
Probably "Add commentMore actions" has to be dropped...
Quoted 54 lines
grub-core/commands/appendedsig/appendedsig.h +13
-0+* 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 +32
-0@@ -0,0 +1,454 @@ +/* +* 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 "appendedsig.h" +#include <grub/misc.h> +#include <grub/crypto.h> +#include <grub/gcrypt/gcrypt.h> +#include <sys/types.h> + +static char asn1_error[ASN1_MAX_ERROR_DESCRIPTION_SIZE ]; + +/* RFC 5652 s 5.1 */ +static const char *signedData_oid ="1.2.840.113549.1.7.2" ; + +/* RFC 4055 s 2.1 */ +static const char *sha256_oid ="2.16.840.1.101.3.4.2.1" ;
grub-core/commands/appendedsig/pkcs7.c line 42
Quoted 10 lines
grub-core/commands/appendedsig/pkcs7.c new file +10
-0+static const char *sha512_oid ="2.16.840.1.101.3.4.2.3" ; + +static grub_err_t +process_content (grub_uint8_t *content,int size,struct pkcs7_signedData *msg) +{ +int res; +asn1_node signed_part; +grub_err_t err =GRUB_ERR_NONE ; +char algo_oid[MAX_OID_LEN ]; +int algo_oid_size =sizeof (algo_oid);
It seems to me that you can drop this initialization...
[...]
Quoted 5 lines
grub-core/commands/appendedsig/pkcs7.c new file +5
-0+grub_err_t +parse_pkcs7_signedData (const void *sigbuf,grub_size_t data_size,struct pkcs7_signedData *msg) +{ +int res; +asn1_node content_info;
grub-core/commands/appendedsig/pkcs7.c line 382
Quoted 10 lines
grub-core/commands/appendedsig/pkcs7.c new file +10
-0+grub_err_t err =GRUB_ERR_NONE ; +char content_oid[MAX_OID_LEN ]; +grub_uint8_t *content; +int content_size; +int content_oid_size =sizeof (content_oid); +int size; + +if (data_size >GRUB_INT_MAX ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +"cannot parse a PKCS#7 message where data size > INT_MAX" );
s/INT_MAX/GRUB_INT_MAX/
And probably I would consider dropping some more N_() from various cryptic error messages.
Anyway, if you fix these minor issues you can add my RB to this patch.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 12/25 appended signatures: parse X.509 certificates
In reply to SudhakarFrom: Daniel Axtens <[email protected]>
This code allows us to parse:
- X.509 certificates: at least enough to verify the signatures on the PKCS#7 messages. We expect that the certificates embedded in grub will be leaf certificates, not CA certificates. The parser enforces this.
- X.509 certificates support the Extended Key Usage extension and handle it by verifying that the certificate has a Code Signing usage.
--- grub-core/commands/appendedsig/appendedsig.h | 30 + grub-core/commands/appendedsig/x509.c | 954 +++++++++++++++++++ 2 files changed, 984 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.Add commentMore actions +* 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.Add commentMore actions */ 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.Add commentMore actions +*/ +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.Add commentMore actions * The message must be in 'sigbuf' and of size 'data_size'. The result is
grub-core/commands/appendedsig/x509.c new file +954 -0
954 lines not on this page · raw message
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 12/25] appended signatures: parse X.509 certificates
Quoted 6 lines · Sudhakar
From: Daniel Axtens <[email protected]>
This code allows us to parse:
- X.509 certificates: at least enough to verify the signatures on the PKCS#7 messages. We expect that the certificates embedded in grub will
s/grub/GRUB/ The project name is GRUB... Please fix this everywhere...
Quoted 16 lines
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.
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]>--- grub-core/commands/appendedsig/appendedsig.h | 30 + grub-core/commands/appendedsig/x509.c | 954 +++++++++++++++++++ 2 files changed, 984 insertions(+) create mode 100644 grub-core/commands/appendedsig/x509.c
[...]
Quoted 38 lines
grub-core/commands/appendedsig/x509.c new file +38
-0+/* Decode a string as defined in Appendix A */ +static grub_err_t +decode_string (char *der,int der_size,char **string,grub_size_t *string_size) +{ +asn1_node strasn; +int result; +char *choice; +int choice_size =0 ; +int tmp_size =0 ; +grub_err_t err =GRUB_ERR_NONE ; + + result =asn1_create_element (grub_gnutls_pkix_asn,"PKIX1.DirectoryString" , &strasn); +if (result !=ASN1_SUCCESS ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY , +N_ ("could not create ASN.1 structure for certificate: %s" ), +asn1_strerror (result)); + + result =asn1_der_decoding2 (&strasn, der, &der_size,ASN1_DECODE_FLAG_STRICT_DER , asn1_error); +if (result !=ASN1_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_FILE_TYPE , +N_ ("could not parse DER for DirectoryString: %s" ), asn1_error); + goto cleanup; + } + + choice =grub_asn1_allocate_and_read (strasn,"" ,"DirectoryString choice" , &choice_size); +if (choice ==NULL ) + { + err = grub_errno; + goto cleanup; + } + +if (grub_strncmp ("utf8String" , choice, choice_size) ==0 ) + { + result =asn1_read_value (strasn,"utf8String" ,NULL , &tmp_size); +if (result !=ASN1_MEM_ERROR ) + { + err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("error reading size of UTF-8 string: %s"),
grub-core/commands/appendedsig/x509.c line 270
Quoted 10 lines
grub-core/commands/appendedsig/x509.c new file +10
-0+ asn1_strerror (result)); + goto cleanup_choice; + } + } +else if (grub_strncmp ("printableString" , choice, choice_size) ==0 ) + { + result =asn1_read_value (strasn,"printableString" ,NULL , &tmp_size); +if (result !=ASN1_MEM_ERROR ) + { + err = grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("error reading size of UTF-8 string: %s"),
This error message seems like copypasta mistake...
grub-core/commands/appendedsig/x509.c line 283
Quoted 13 lines
grub-core/commands/appendedsig/x509.c new file +13
-0+ asn1_strerror (result)); + goto cleanup_choice; + } + } +else + { + err =grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET , +N_ ("only UTF-8 and printable DirectoryStrings are supported, got %s" ), + choice); + goto cleanup_choice; + } + +/* read size does not include trailing null */
s/read size does not include trailing null/Read size does not include trailing NUL./
Quoted 30 lines
grub-core/commands/appendedsig/x509.c new file +30
-0+ tmp_size++; + + *string =grub_malloc (tmp_size); +if (*string ==NULL ) + { + err =grub_error (GRUB_ERR_OUT_OF_MEMORY , +"cannot allocate memory for DirectoryString contents" ); + goto cleanup_choice; + } + + result =asn1_read_value (strasn, choice, *string, &tmp_size); +if (result !=ASN1_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_FILE_TYPE ,N_ ("error reading out %s in DirectoryString: %s" ), + choice,asn1_strerror (result)); +grub_free (*string); + *string =NULL ; + goto cleanup_choice; + } + + *string_size = tmp_size +1 ; + (*string)[tmp_size] ='\0' ; + + cleanup_choice: +grub_free (choice); + cleanup: +asn1_delete_structure (&strasn); + +return err; +}
[...]
Quoted 23 lines
grub-core/commands/appendedsig/x509.c new file +23
-0+static grub_err_t +verify_extended_key_usage (grub_uint8_t *value,int value_size) +{ +asn1_node extendedasn; +int result, count, i =0 ; +grub_err_t err =GRUB_ERR_NONE ; +char usage[MAX_OID_LEN ], name[3 ]; +int usage_size =sizeof (usage); + + result =asn1_create_element (grub_gnutls_pkix_asn,"PKIX1.ExtKeyUsageSyntax" , &extendedasn); +if (result !=ASN1_SUCCESS ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY , +"could not create ASN.1 structure for Extended Key Usage" ); + + result =asn1_der_decoding2 (&extendedasn, value, &value_size, +ASN1_DECODE_FLAG_STRICT_DER , asn1_error); +if (result !=ASN1_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_FILE_TYPE , +N_ ("error parsing DER for Extended Key Usage: %s" ), asn1_error); + goto cleanup; + } +
grub-core/commands/appendedsig/x509.c line 613
Quoted 10 lines
grub-core/commands/appendedsig/x509.c new file +10
-0+/* If EKUs are present, it checks the presents of Code Signing usage */ + result =asn1_number_of_elements (extendedasn,"" , &count); +if (result !=ASN1_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_FILE_TYPE ,N_ ("error counting number of Extended Key Usages: %s" ), +asn1_strerror (result)); + goto cleanup; + } + +
Redundant empty line...
Quoted 13 lines
grub-core/commands/appendedsig/x509.c new file +13
-0+for (i =1 ; i < count +1 ; i++) + { +grub_memset (name,0 ,sizeof (name)); +grub_snprintf (name,sizeof (name),"?%d" , i); + result =asn1_read_value (extendedasn, name, usage, &usage_size); +if (result !=ASN1_SUCCESS ) + { + err =grub_error (GRUB_ERR_BAD_FILE_TYPE ,N_ ("error reading Extended Key Usage: %s" ), +asn1_strerror (result)); + goto cleanup; + } + +if (grub_strncmp (codeSigningUsage_oid, usage, usage_size) ==0 )
grub-core/commands/appendedsig/x509.c line 636
Quoted 10 lines
grub-core/commands/appendedsig/x509.c new file +10
-0+ goto cleanup; + } + + err =grub_error (GRUB_ERR_BAD_FILE_TYPE ,"extended key usage missing Code Signing usage" ); + + cleanup: +asn1_delete_structure (&extendedasn); + +return err; +}
And probably I would consider dropping some more N_() from various cryptic error messages. Same for other files in the patch set...
Anyway, if you fix these minor issues you can add my RB to this patch.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 13/25 appended signatures: support verifying appended signatures
In reply to SudhakarFrom: Daniel Axtens <[email protected]>
Building 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 | 624 +++++++++++++++++++ include/grub/file.h | 2 + 3 files changed, 640 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; + + cflags = '$(CFLAGS_POSIX)'; + cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/libtasn1-grub'; + depends = asn1, crypto, gcry_rsa, mpi, pkcs1_v15; +}; + module = { name = hdparm; common = commands/hdparm.c;
grub-core/commands/appendedsig/appendedsig.c new file +624 -0
624 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 13/25] appended signatures: support verifying appended signatures
Quoted 139 lines · Sudhakar
From: Daniel Axtens <[email protected]>
Building 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.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]>--- grub-core/Makefile.core.def | 14 + grub-core/commands/appendedsig/appendedsig.c | 624 +++++++++++++++++++ include/grub/file.h | 2 + 3 files changed, 640 insertions(+) create mode 100644 grub-core/commands/appendedsig/appendedsig.cgrub-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; + + cflags = '$(CFLAGS_POSIX)'; + cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/libtasn1-grub'; + depends = asn1, crypto, gcry_rsa, mpi, pkcs1_v15; +}; + module = { name = hdparm; common = commands/hdparm.c;grub-core/commands/appendedsig/appendedsig.c new file +79
-0@@ -0,0 +1,624 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2020, 2021, 2022 Free Software Foundation, Inc. +* Copyright (C) 2020, 2021, 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 <grub/types.h> +#include <grub/misc.h> +#include <grub/mm.h> +#include <grub/err.h> +#include <grub/dl.h> +#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/kernel.h> +#include <grub/extcmd.h> +#include <grub/verify.h> +#include <libtasn1.h> +#include <grub/env.h> +#include <grub/lockdown.h> + +#include "appendedsig.h" + +GRUB_MOD_LICENSE ("GPLv3+" ); + +static const char magic[] ="~Module signature appended~\n" ; + +/* +* This structure is extracted from scripts/sign-file.c in the linux kernel +* source. It was licensed as LGPLv2.1+, which is GPLv3+ compatible. +*/ +struct module_signature +{ +grub_uint8_t algo;/* Public-key crypto algorithm [0] */ +grub_uint8_t hash;/* Digest algorithm [0] */ +grub_uint8_t id_type;/* Key identifier type [PKEY_ID_PKCS7] */ +grub_uint8_t signer_len;/* Length of signer's name [0] */ +grub_uint8_t key_id_len;/* Length of key identifier [0] */ +grub_uint8_t __pad[3 ]; +grub_uint32_t sig_len;/* Length of signature data */ +}GRUB_PACKED ; + +/* This represents an entire, parsed, appended signature */ +struct grub_appended_signature +{ +grub_size_t signature_len;/* Length of PKCS#7 data + metadata + magic */ +struct module_signature sig_metadata;/* Module signature metadata */ +struct pkcs7_signedData pkcs7;/* Parsed PKCS#7 data */ +}; + +/* Trusted certificates for verifying appended signatures */ +struct x509_certificate *grub_trusted_key; + +/* +* Force gcry_rsa to be a module dependency. +* +* If we use grub_crypto_pk_rsa, then then the gcry_rsa module won't be built +* in if you add 'appendedsig' to grub-install --modules. You would need to +* add 'gcry_rsa' too. That's confusing and seems suboptimal, especially when +* we only support RSA. +* +* Dynamic loading also causes some concerns. We can't load gcry_rsa from the +* the filesystem after we install the verifier - we won't be able to verify
grub-core/commands/appendedsig/appendedsig.c line 89
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10
-0+* it without having it already present. We also shouldn't load it before we +* install the verifier, because that would mean it wouldn't be verified - an +* attacker could insert any code they wanted into the module. +* +* So instead, reference the internal symbol from gcry_rsa. That creates a +* direct dependency on gcry_rsa, so it will be built in when this module +* is built in. Being built in (assuming the core image is itself signed!) +* also resolves our concerns about loading from the filesystem. +*/ +extern gcry_pk_spec_t _gcry_pubkey_spec_rsa;
This is not nice. Please take a look at the commit 6744840b1 (build:
Track explicit module dependencies in Makefile.core.def) how it should be done properly.
grub-core/commands/appendedsig/appendedsig.c line 96
Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c new file +6
-0+static enum +{ +CHECK_SIGS_NO =0 , +CHECK_SIGS_ENFORCE =1 , +CHECK_SIGS_FORCED =2 +}CHECK_SIGS =CHECK_SIGS_NO ;
s/CHECK_SIGS = CHECK_SIGS_NO/check_sigs = CHECK_SIGS_NO/
Quoted 110 lines
grub-core/commands/appendedsig/appendedsig.c new file +110
-0+static const char * +grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)), +const char *val __attribute__ ((unused))) +{ +if (CHECK_SIGS ==CHECK_SIGS_FORCED ) +return "forced" ; +else if (CHECK_SIGS ==CHECK_SIGS_ENFORCE ) +return "enforce" ; + +return "no" ; +} + +static char * +grub_env_write_sec (struct grub_env_var *var __attribute__ ((unused)),const char *val) +{ +char *ret; + +/* Do not allow the value to be changed if set to forced */ +if (CHECK_SIGS ==CHECK_SIGS_FORCED ) + { + ret =grub_strdup ("forced" ); +if (ret ==NULL ) +grub_error (GRUB_ERR_OUT_OF_MEMORY ,"could not duplicate a string forced" ); + +return ret; + } + +if ((*val =='2' ) || (*val =='f' )) +CHECK_SIGS =CHECK_SIGS_FORCED ; +else if ((*val =='1' ) || (*val =='e' )) +CHECK_SIGS =CHECK_SIGS_ENFORCE ; +else if ((*val =='0' ) || (*val =='n' )) +CHECK_SIGS =CHECK_SIGS_NO ; + + ret =grub_strdup (grub_env_read_sec (NULL ,NULL )); +if (ret ==NULL ) +grub_error (GRUB_ERR_OUT_OF_MEMORY ,N_ ("could not duplicate a string %s" ), +grub_env_read_sec (NULL ,NULL )); + +return ret; +} + +static grub_err_t +file_read_all (grub_file_t file,grub_uint8_t **buf,grub_size_t *len) +{ +grub_off_t full_file_size; +grub_size_t file_size, total_read_size =0 ; +grub_ssize_t read_size; + + full_file_size =grub_file_size (file); +if (full_file_size ==GRUB_FILE_SIZE_UNKNOWN ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +"cannot read a file of unknown size into a buffer" ); + +if (full_file_size >GRUB_SIZE_MAX ) +return grub_error (GRUB_ERR_OUT_OF_RANGE , +"file is too large to read: %" PRIuGRUB_UINT64_T" bytes" , + full_file_size); + + file_size = (grub_size_t ) full_file_size; + *buf =grub_malloc (file_size); +if (*buf ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY , +"could not allocate file data buffer size %" PRIuGRUB_SIZE, + file_size); + +while (total_read_size < file_size) + { + read_size =grub_file_read (file, *buf + total_read_size, file_size - total_read_size); +if (read_size <0 ) + { +grub_free (*buf); +return grub_errno; + } +else if (read_size ==0 ) + { +grub_free (*buf); +return grub_error (GRUB_ERR_IO , +"could not read full file size " +"(%" PRIuGRUB_SIZE"), only %" PRIuGRUB_SIZE" bytes read" , + file_size, total_read_size); + } + + total_read_size += read_size; + } + + *len = file_size; + +return GRUB_ERR_NONE ; +} + +static grub_err_t +read_cert_from_file (grub_file_t f,struct x509_certificate *certificate) +{ +grub_err_t err; +grub_uint8_t *buf; +grub_size_t file_size; + + err =file_read_all (f, &buf, &file_size); +if (err !=GRUB_ERR_NONE ) +return err; + + err =parse_x509_certificate (buf, file_size, certificate); +grub_free (buf); + +return err; +} + +static grub_err_t +extract_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize,
grub-core/commands/appendedsig/appendedsig.c line 217
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10
-0+ struct grub_appended_signature *sig) +{ +grub_size_t pkcs7_size; +grub_size_t remaining_len; +const grub_uint8_t *appsigdata = buf + bufsize -grub_strlen (magic); + +if (bufsize <grub_strlen (magic)) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"file too short for signature magic" ); + +if (grub_memcmp (appsigdata, (grub_uint8_t *) magic,grub_strlen (magic)))
if (grub_strncmp (appsigdata, magic, sizeof (magic) - 1))
You compare/expect strings, not binary data...
grub-core/commands/appendedsig/appendedsig.c line 230
Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c new file +13
-0+return grub_error (GRUB_ERR_BAD_SIGNATURE ,"missing or invalid signature magic" ); + + remaining_len = bufsize -grub_strlen (magic); + +if (remaining_len <sizeof (struct module_signature )) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"file too short for signature metadata" ); + + appsigdata -=sizeof (struct module_signature ); +/* extract the metadata */ +grub_memcpy (&(sig->sig_metadata), appsigdata,sizeof (struct module_signature )); + remaining_len -=sizeof (struct module_signature ); + +if (sig->sig_metadata.id_type !=2 )
What id_type 2 means? Please define constant or add a comment before "if". I think (meaningful) constant would be better...
Quoted 15 lines
grub-core/commands/appendedsig/appendedsig.c new file +15
-0+return grub_error (GRUB_ERR_BAD_SIGNATURE ,"wrong signature type" ); + + pkcs7_size =grub_be_to_cpu32 (sig->sig_metadata.sig_len); + +if (pkcs7_size > remaining_len) +return grub_error (GRUB_ERR_BAD_SIGNATURE ,"file too short for PKCS#7 message" ); + +grub_dprintf ("appendedsig" ,"sig len %" PRIuGRUB_SIZE"\n" , pkcs7_size); + + sig->signature_len =grub_strlen (magic) +sizeof (struct module_signature ) + pkcs7_size; +/* rewind pointer and parse pkcs7 data */ + appsigdata -= pkcs7_size; + +return parse_pkcs7_signedData (appsigdata, pkcs7_size, &sig->pkcs7); +}
[...]
grub-core/commands/appendedsig/appendedsig.c line 378
Quoted 12 lines
grub-core/commands/appendedsig/appendedsig.c new file +12
-0+static grub_err_t +grub_cmd_distrust (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +{ +unsigned long cert_num, i; +struct x509_certificate *cert, *prev; + +if (argc !=1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected" ); + + grub_errno =GRUB_ERR_NONE ; + cert_num =grub_strtoul (args[0 ],NULL ,10 ); +if (grub_errno !=GRUB_ERR_NONE )
Again, this is not reliable. Please fix this.
grub-core/commands/appendedsig/appendedsig.c line 381
Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3
-0+return grub_error (GRUB_ERR_BAD_ARGUMENT , +"non-numeric or certificate number is invalid" ); + else if (cert_num <= 0)
How can it be less than zero if it is "unsigned long"?
grub-core/commands/appendedsig/appendedsig.c line 394
Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c new file +13
-0+return grub_error (GRUB_ERR_OUT_OF_RANGE , +"certificate number too small - numbers start at 1" ); + +if (cert_num ==1 ) + { + cert = grub_trusted_key; + grub_trusted_key = cert->next; + +certificate_release (cert); +grub_free (cert); +return GRUB_ERR_NONE ; + } + i =2 ;
Where 2 comes from? It begs for a comment.
grub-core/commands/appendedsig/appendedsig.c line 397
Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c new file +3
-0+ prev = grub_trusted_key; + cert = grub_trusted_key->next; +while (cert)
Taking into account above probably "for" instead of while() would be better here.
Quoted 57 lines
grub-core/commands/appendedsig/appendedsig.c new file +57
-0+ { +if (i == cert_num) + { + prev->next = cert->next; +certificate_release (cert); +grub_free (cert); +return GRUB_ERR_NONE ; + } + i++; + prev = cert; + cert = cert->next; + } + +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("no certificate number %lu found - only %lu certificates in the store" ), + cert_num, i -1 ); +} + +static grub_err_t +grub_cmd_trust (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +{ +grub_file_t certf; +struct x509_certificate *cert =NULL ; +grub_err_t err; + +if (argc !=1 ) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected" ); + + 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 ,N_ ("could not open %s file" ), args[0 ]); + + 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); +if (err !=GRUB_ERR_NONE ) + { +grub_free (cert); +return err; + } + +grub_dprintf ("appendedsig" ,"loaded certificate with CN: %s\n" , cert->subject); + + cert->next = grub_trusted_key; + grub_trusted_key = cert; + +return GRUB_ERR_NONE ; +} + +static grub_err_t +grub_cmd_list (grub_command_t cmd __attribute__ ((unused)),int argc __attribute__ ((unused)), +char **args __attribute__ ((unused))) +{ +struct x509_certificate *cert;
grub-core/commands/appendedsig/appendedsig.c line 464
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10
-0+int cert_num =1 ; +grub_size_t i; + +for (cert = grub_trusted_key; cert; cert = cert->next) + { +grub_printf ("Certificate %d:\n" , cert_num); +grub_printf ("\tSerial: " ); + +for (i =0 ; i < cert->serial_len -1 ; i++) +grub_printf ("%02x:" , cert->serial[i]);
Something is off with indention here...
Quoted 34 lines
grub-core/commands/appendedsig/appendedsig.c new file +34
-0+grub_printf ("%02x\n" , cert->serial[cert->serial_len -1 ]); +grub_printf ("\tCN: %s\n\n" , cert->subject); + cert_num++; + } + +return GRUB_ERR_NONE ; +} + +static grub_err_t +appendedsig_init (grub_file_t io __attribute__ ((unused)),enum grub_file_type type, +void **context __attribute__ ((unused)),enum grub_verify_flags *flags) +{ +if (CHECK_SIGS ==CHECK_SIGS_NO ) + { + *flags =GRUB_VERIFY_FLAGS_SKIP_VERIFICATION ; +return GRUB_ERR_NONE ; + } + +switch (type &GRUB_FILE_TYPE_MASK ) + { +case GRUB_FILE_TYPE_CERTIFICATE_TRUST : +/* +* This is a certificate to add to trusted keychain. +* +* This needs to be verified or blocked. Ideally we'd write an x509 +* verifier, but we lack the hubris required to take this on. Instead, +* require that it have an appended signature. +*/ +case GRUB_FILE_TYPE_LINUX_KERNEL : +case GRUB_FILE_TYPE_GRUB_MODULE : +/* +* Appended signatures are only defined for ELF binaries. +* Out of an abundance of caution, we only verify Linux kernels and +* GRUB modules at this point.
grub-core/commands/appendedsig/appendedsig.c line 509
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c new file +10
-0+*/ + *flags =GRUB_VERIFY_FLAGS_SINGLE_CHUNK ; +return GRUB_ERR_NONE ; + +case GRUB_FILE_TYPE_ACPI_TABLE : +case GRUB_FILE_TYPE_DEVICE_TREE_IMAGE : +/* +* It is possible to use appended signature verification without +* lockdown - like the PGP verifier. When combined with an embedded +* config file in a signed grub binary, this could still be a meaningful
s/grub/GRUB/
grub-core/commands/appendedsig/appendedsig.c line 520
Quoted 11 lines
grub-core/commands/appendedsig/appendedsig.c new file +11
-0+* secure-boot chain - so long as it isn't subverted by something like a +* rouge ACPI table or DT image. Defer them explicitly. +*/ + *flags =GRUB_VERIFY_FLAGS_DEFER_AUTH ; +return GRUB_ERR_NONE ; + +default : + *flags =GRUB_VERIFY_FLAGS_SKIP_VERIFICATION ; +return GRUB_ERR_NONE ; + } +}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 14/25 appended signatures: verification tests
In reply to SudhakarFrom: 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 | 6 + grub-core/tests/appended_signature_test.c | 250 ++++++ grub-core/tests/appended_signatures.h | 975 ++++++++++++++++++++++ grub-core/tests/lib/functional_test.c | 1 + 4 files changed, 1232 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 +6 -0
@@ -2194,6 +2194,12 @@ module = { common = tests/setjmp_test.c; }; +module = { + name = appended_signature_test; + common = tests/appended_signature_test.c; + common = tests/appended_signatures.h; +}; + 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 +1 -0
@@ -73,6 +73,7 @@ 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" ); +grub_dl_load ("appended_signature_test" );grub_dl_load ("sleep_test" );grub_dl_load ("bswap_test" );grub_dl_load ("ctz_test" );
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 14/25] appended signatures: verification tests
Quoted 9 lines · Sudhakar
From: Daniel Axtens <[email protected]>
These tests are run through
all_functional_testand test a range of commands and behaviours.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]>
Though one nit below...
Quoted 63 lines
--- grub-core/Makefile.core.def | 6 + grub-core/tests/appended_signature_test.c | 250 ++++++ grub-core/tests/appended_signatures.h | 975 ++++++++++++++++++++++ grub-core/tests/lib/functional_test.c | 1 + 4 files changed, 1232 insertions(+) create mode 100644 grub-core/tests/appended_signature_test.c create mode 100644 grub-core/tests/appended_signatures.hgrub-core/Makefile.core.def +6
-0@@ -2194,6 +2194,12 @@ module = { common = tests/setjmp_test.c; }; +module = { + name = appended_signature_test; + common = tests/appended_signature_test.c; + common = tests/appended_signatures.h; +}; + module = { name = signature_test; common = tests/signature_test.c;grub-core/tests/appended_signature_test.c new file +31
-0@@ -0,0 +1,250 @@ +/* +* 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 <grub/time.h> +#include <grub/misc.h> +#include <grub/dl.h> +#include <grub/command.h> +#include <grub/env.h> +#include <grub/test.h> +#include <grub/mm.h> +#include <grub/procfs.h> +#include <grub/file.h> + +#include "appended_signatures.h" +
grub-core/tests/appended_signature_test.c line 41
Quoted 10 lines
grub-core/tests/appended_signature_test.c new file +10
-0+GRUB_MOD_LICENSE ("GPLv3+" ); + +#define PROC_FILE (identifier, file_name) \ + static char *get_##identifier (grub_size_t *sz) \ + { \ + char *ret; \ + \ + *sz = identifier##_len; \ + ret = grub_malloc (*sz); \ + if (ret) \
if (ret != NULL)
grub-core/tests/appended_signature_test.c line 47
Quoted 6 lines
grub-core/tests/appended_signature_test.c new file +6
-0+grub_memcpy (ret, identifier, *sz); \ +return ret; \ + } \ + \ + static struct grub_procfs_entry identifier##_entry = { .name = file_name, \ + .get_contents = get_##identifier };
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 15/25 appended signatures: documentation
In reply to SudhakarFrom: 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 | 185 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 167 insertions(+), 18 deletions(-)
docs/grub.texi +167 -18
@@ -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 +certain loaded files. @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
@@ -6435,6 +6440,7 @@ you forget a command, you can run the command @command{help} * date:: Display or set current date and time * devicetree:: Load a device tree blob * distrust:: Remove a pubkey from trusted keys +* distrust_certificate:: Remove a certificate from the list of trusted certificates * drivemap:: Map a drive to another * echo:: Display a line of text * efitextmode:: Set/Get text output mode resolution
@@ -6453,6 +6459,7 @@ you forget a command, you can run the command @command{help} * hexdump:: Show raw contents of a file or memory * insmod:: Insert a module * keystatus:: Check key modifier status +* list_certificates:: List trusted certificates * list_env:: List variables in environment block * list_trusted:: List trusted public keys * load_env:: Load variables from environment block
@@ -6494,8 +6501,10 @@ you forget a command, you can run the command @command{help} * tpm2_dump_pcr:: Dump TPM2 PCRs * true:: Do nothing, successfully * trust:: Add public key to list of trusted keys +* trust_certificate:: Add an x509 certificate to the list of trusted certificates * unset:: Unset an environment variable @comment * vbeinfo:: List available video modes +* verify_appended:: Verify appended digital signature * verify_detached:: Verify detached digital signature * videoinfo:: List available video modes * wrmsr:: Write values to model-specific registers
@@ -6854,7 +6863,24 @@ 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 distrust_certificate +@subsection distrust_certificate + +@deffn Command distrust_certificate cert_number +Remove the x509 certificate numbered @var{cert_number} from GRUB's keyring of +trusted x509 certificates for verifying appended signatures. + +@var{cert_number} is the certificate number as listed by +@command{list_certificates} (@pxref{list_certificates}). + +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{verify_appended} +(@pxref{verify_appended}). See @xref{Using appended signatures} for more +information. @end deffn @node drivemap
@@ -7250,6 +7276,19 @@ without any options, the @command{keystatus} command returns true if and only if checking key modifier status is supported. @end deffn +@node list_certificates +@subsection list_certificates + +@deffn Command list_certificates +List all x509 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{distrust_certificate} (@pxref{distrust_certificate}). + +See @xref{Using appended signatures} for more information. +@end deffn @node list_env @subsection list_env
@@ -7270,7 +7309,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 +7344,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 +7719,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 +8209,30 @@ 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 trust_certificate +@subsection trust_certificate + +@deffn Command trust_certificate x509_certificate +Read a DER-formatted x509 certificate from the file @var{x509_certificate} +and add it to GRUB's internal list of trusted x509 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{trust_certificate} 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 +x509 rules: GRUB does not include support for validating signatures within x509 +certificates themselves.) + +See @xref{Using appended signatures} for more information. +@end deffn + @node unset @subsection unset
@@ -8190,6 +8251,18 @@ only on PC BIOS platforms. @end deffn @end ignore +@node verify_appended +@subsection verify_appended + +@deffn Command verify_appended file +Verifies an appended signature on @var{file} against the trusted certificates +known to GRUB (See @pxref{list_certificates}, @pxref{trust_certificate}, and +@pxref{distrust_certificate}). +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 verify_detached @subsection verify_detached
@@ -8208,7 +8281,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 +8741,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 +8825,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 +8909,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{trust_certificate}, +@pxref{distrust_certificate} and @pxref{list_certificates} 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{verify_appended} 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 15/25] appended signatures: documentation
Quoted 27 lines · Sudhakar
From: 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.
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]>--- docs/grub.texi | 185 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 167 insertions(+), 18 deletions(-)docs/grub.texi +1
-0@@ -3281,6 +3281,7 @@ These variables have special meaning to GRUB. @menu * biosnum:: +* check_appended_signatures:: * check_signatures:: * chosen:: * cmdpath::
docs/grub.texi this. · line 3350
Quoted 7 lines
docs/grub.texi +4
-0@@ -3343,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 +certain loaded files. @xref{Using appended signatures}.
What does a "certain" mean? Please expand that.
Quoted 118 lines
docs/grub.texi +43
-6@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@@ -6435,6 +6440,7 @@ you forget a command, you can run the command @command{help} * date:: Display or set current date and time * devicetree:: Load a device tree blob * distrust:: Remove a pubkey from trusted keys +* distrust_certificate:: Remove a certificate from the list of trusted certificates * drivemap:: Map a drive to another * echo:: Display a line of text * efitextmode:: Set/Get text output mode resolution@@ -6453,6 +6459,7 @@ you forget a command, you can run the command @command{help} * hexdump:: Show raw contents of a file or memory * insmod:: Insert a module * keystatus:: Check key modifier status +* list_certificates:: List trusted certificates * list_env:: List variables in environment block * list_trusted:: List trusted public keys * load_env:: Load variables from environment block@@ -6494,8 +6501,10 @@ you forget a command, you can run the command @command{help} * tpm2_dump_pcr:: Dump TPM2 PCRs * true:: Do nothing, successfully * trust:: Add public key to list of trusted keys +* trust_certificate:: Add an x509 certificate to the list of trusted certificates * unset:: Unset an environment variable @comment * vbeinfo:: List available video modes +* verify_appended:: Verify appended digital signature * verify_detached:: Verify detached digital signature * videoinfo:: List available video modes * wrmsr:: Write values to model-specific registers@@ -6854,7 +6863,24 @@ 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 distrust_certificate +@subsection distrust_certificate + +@deffn Command distrust_certificate cert_number +Remove the x509 certificate numbered @var{cert_number} from GRUB's keyring of +trusted x509 certificates for verifying appended signatures. + +@var{cert_number} is the certificate number as listed by +@command{list_certificates} (@pxref{list_certificates}). + +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{verify_appended} +(@pxref{verify_appended}). See @xref{Using appended signatures} for more +information. @end deffn @node drivemap@@ -7250,6 +7276,19 @@ without any options, the @command{keystatus} command returns true if and only if checking key modifier status is supported. @end deffn +@node list_certificates +@subsection list_certificates + +@deffn Command list_certificates +List all x509 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{distrust_certificate} (@pxref{distrust_certificate}). + +See @xref{Using appended signatures} for more information. +@end deffn @node list_env @subsection list_env@@ -7270,7 +7309,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 +7344,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 +7719,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 +8209,30 @@ 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
docs/grub.texi signatures when environment variable @code{check_signatures} is set to · line 8220
Quoted 10 lines
docs/grub.texi +5
-1-and manual booting. @xref{Using digital signatures}, for more +and manual booting. @xref{Using GPG-style digital signatures}, for more information. @end deffn +@node trust_certificate +@subsection trust_certificate + +@deffn Command trust_certificate x509_certificate
The name of this command is confusing. It is very generic but applies only to appended signatures. So, maybe "appended_trust_certificate"?
Quoted 4 lines
docs/grub.texi +4
-0+Read a DER-formatted x509 certificate from the file @var{x509_certificate} +and add it to GRUB's internal list of trusted x509 certificates. These +certificates are used to validate appended signatures when the environment +variable @code{check_appended_signatures} is set to @code{enforce}.
docs/grub.texi signatures when environment variable @code{check_signatures} is set to · line 8234
Quoted 10 lines
docs/grub.texi +10
-0+ +Note that if @code{check_appended_signatures} is set to @code{enforce} +when @command{trust_certificate} 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 +x509 rules: GRUB does not include support for validating signatures within x509 +certificates themselves.) + +See @xref{Using appended signatures} for more information. +@end deffn
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 16/25 ieee1275: enter lockdown based on /ibm,secure-boot
In reply to SudhakarFrom: 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 | 36 ++++++++++++++++++++++++++++++++++ include/grub/lockdown.h | 3 ++- 4 files changed, 40 insertions(+), 2 deletions(-)
docs/grub.texi +1 -1
@@ -9062,7 +9062,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 +36 -0
@@ -49,6 +49,7 @@#if defined(__powerpc__) || defined(__i386__)#include <grub/ieee1275/alloc.h> #endif +#include <grub/lockdown.h> /* The maximum heap size we're going to claim at boot. Not used by sparc. */ #ifdef __i386__
@@ -995,6 +996,39 @@ grub_parse_cmdline (void) } } +static void +grub_get_ieee1275_secure_boot (void ) +{ +grub_ieee1275_phandle_t root; +int rc; +grub_uint32_t is_sb; + + rc =grub_ieee1275_finddevice ("/" , &root); +if (rc !=0 ) + { +grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"couldn't find / node" ); +return ; + } + + rc =grub_ieee1275_get_integer_property (root,"ibm,secure-boot" , &is_sb,sizeof (is_sb),0 ); +if (rc !=0 ) + { +grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"couldn't examine /ibm,secure-boot property" ); +return ; + } +/* +* ibm,secure-boot: +* 0 - disabled +* 1 - audit +* 2 - enforce +* 3 - enforce + OS-specific behaviour +* +* We only support enforce. +*/ +if (is_sb >=2 ) +grub_lockdown (); +} +grub_addr_t grub_modbase;void
@@ -1020,6 +1054,8 @@ grub_machine_init (void)#else grub_install_get_time_ms (grub_rtc_get_time_ms);#endif + +grub_get_ieee1275_secure_boot (); }void
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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 16/25] ieee1275: enter lockdown based on /ibm,secure-boot
Quoted 9 lines · Sudhakar
From: Daniel Axtens <[email protected]>
If the 'ibm,secure-boot' property of the root node is 2 or greater, enter lockdown.
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]>
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 17/25 ieee1275: Platform Keystore (PKS) Support
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB VERSION, DB, and DBX secure boot variables from PKS.
--- grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 1 + grub-core/kern/powerpc/ieee1275/ieee1275.c | 141 +++++++++++++++++++++ include/grub/powerpc/ieee1275/ieee1275.h | 18 +++ 4 files changed, 161 insertions(+) create mode 100644 grub-core/kern/powerpc/ieee1275/ieee1275.c
grub-core/Makefile.am +1 -0
@@ -241,6 +241,7 @@ 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
grub-core/Makefile.core.def +1 -0
@@ -328,6 +328,7 @@ 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;
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +141 -0
141 lines not on this page · raw message
include/grub/powerpc/ieee1275/ieee1275.h +18 -0
@@ -28,4 +28,22 @@ typedef grub_uint32_t grub_ieee1275_cell_t;#define PRIxGRUB_IEEE1275_CELL_T PRIxGRUB_UINT32_T#define PRIuGRUB_IEEE1275_CELL_T PRIuGRUB_UINT32_T +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 /* ! GRUB_IEEE1275_MACHINE_HEADER */
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 17/25] ieee1275: Platform Keystore (PKS) Support
Quoted 47 lines · Sudhakar
From: Sudhakar Kuppusamy <[email protected]>
enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB VERSION, DB, and DBX secure boot variables from PKS.
Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 1 + grub-core/kern/powerpc/ieee1275/ieee1275.c | 141 +++++++++++++++++++++ include/grub/powerpc/ieee1275/ieee1275.h | 18 +++ 4 files changed, 161 insertions(+) create mode 100644 grub-core/kern/powerpc/ieee1275/ieee1275.cgrub-core/Makefile.am +1
-0@@ -241,6 +241,7 @@ 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.hgrub-core/Makefile.core.def +1
-0@@ -328,6 +328,7 @@ 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;grub-core/kern/powerpc/ieee1275/ieee1275.c new file +0
-0
grub-core/kern/powerpc/ieee1275/ieee1275.c line 1
Quoted 1 line
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +1
-0@@ -0,0 +1,141 @@ +/* of.c - Access the Open Firmware client interface. */
s/of.c// or s/of.c/ieee1275.c/?
Quoted 13 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +13
-0+/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc. +* Copyright (C) 2020, 2021, 2022, 2023, 2024, 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
grub-core/kern/powerpc/ieee1275/ieee1275.c line 24
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +10
-0+* 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/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/ieee1275.h> +#include <grub/misc.h> + +#define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) - 1)
I can see this defined and used in the grub-core/kern/ieee1275/ieee1275.c. Why cannot you define GRUB_IEEE1275_CELL_INVALID in include/grub/ieee1275/ieee1275.h somewhere around GRUB_IEEE1275_CELL_FALSE/GRUB_IEEE1275_CELL_TRUE and use here and in grub-core/kern/ieee1275/ieee1275.c?
grub-core/kern/powerpc/ieee1275/ieee1275.c line 27
Quoted 2 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +2
-0+int +grub_ieee1275_test (const char *name,grub_ieee1275_cell_t *missing)
Where are these functions used? Without that knowledge it is difficult to say they are correct or not including return values types. I think this patch should be merged with patch which call this functions.
Quoted 14 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +14
-0+{ +struct test_args + { +struct grub_ieee1275_common_hdr common; +grub_ieee1275_cell_t name; +grub_ieee1275_cell_t missing; + } args; + +INIT_IEEE1275_COMMON (&args.common,"test" ,1 ,1 ); + args.name = (grub_ieee1275_cell_t ) name; + +if (IEEE1275_CALL_ENTRY_FN (&args) ==-1 ) +return -1 ; +
grub-core/kern/powerpc/ieee1275/ieee1275.c line 51
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +10
-0+if (args.missing ==IEEE1275_CELL_INVALID ) +return -1 ; + + *missing = args.missing; + +return 0 ; +} + +int +grub_ieee1275_pks_max_object_size (grub_size_t *result)
Ditto and below...
Quoted 9 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +9
-0+{ +struct mos_args + { +struct grub_ieee1275_common_hdr common; +grub_ieee1275_cell_t size; + } args; + +INIT_IEEE1275_COMMON (&args.common,"pks-max-object-size" ,0 ,1 ); +
grub-core/kern/powerpc/ieee1275/ieee1275.c line 70
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +10
-0+if (IEEE1275_CALL_ENTRY_FN (&args) ==-1 ) +return -1 ; + +if (args.size ==IEEE1275_CELL_INVALID ) +return -1 ; + + *result = args.size; + +return 0 ; +}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 17/25] ieee1275: Platform Keystore (PKS) Support
Hi Daniel,
Thanks a lot for your review.
Quoted 40 lines · Daniel Kiper
Quoted 37 lines · Sudhakar
From: Sudhakar Kuppusamy <[email protected]>
enhancing the infrastructure to enable the Platform Keystore (PKS) feature, which provides access to the SB VERSION, DB, and DBX secure boot variables from PKS.
Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>---
grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 1 + grub-core/kern/powerpc/ieee1275/ieee1275.c | 141 +++++++++++++++++++++ include/grub/powerpc/ieee1275/ieee1275.h | 18 +++ 4 files changed, 161 insertions(+)create mode 100644
grub-core/kern/powerpc/ieee1275/ieee1275.cgrub-core/Makefile.am +0
-0@@ -241,6 +241,7 @@ KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.hendif
if
COND_powerpc_ieee1275
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/powerpc/ieee1275/ieee1275.hKERNEL_HEADER_FILES+= $(top_srcdir)/include/grub/ieee1275/ieee1275.hKERNEL_HEADER_FILES+= $(top_srcdir)/include/grub/ieee1275/alloc.hKERNEL_HEADER_FILES+= $(top_srcdir)/include/grub/terminfo.hgrub-core/Makefile.core.def +0
-0@@ -328,6 +328,7 @@ kernel = { extra_dist = video/sis315_init.c; mips_loongson = commands/keylayouts.c;
Quoted 13 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c line 1
Quoted 11 lines
grub-core/Makefile.core.def +1
-0+ 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;grub-core/kern/powerpc/ieee1275/ieee1275.c new file +1
-0@@ -0,0 +1,141 @@ +/* of.c - Access the Open Firmware client interface. */ s/of.c// or s/of.c/ieee1275.c/?
Sure. Will change it
Quoted 21 lines
Quoted 21 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +20
-0+/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc. +* Copyright (C) 2020, 2021, 2022, 2023, 2024, 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 <grub/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/ieee1275.h>
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c line 24
Quoted 3 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +3
-0+#include <grub/misc.h> + +#define IEEE1275_CELL_INVALID ((grub_ieee1275_cell_t) - 1)I can see this defined and used in the
grub-core/kern/ieee1275/ieee1275.c. Why cannot you defineGRUB_IEEE1275_CELL_INVALIDininclude/grub/ieee1275/ieee1275.hsomewhere around GRUB_IEEE1275_CELL_FALSE/GRUB_IEEE1275_CELL_TRUE and use here and ingrub-core/kern/ieee1275/ieee1275.c?
Sure. Will do it.
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c line 27
Quoted 3 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +2
-0+int +grub_ieee1275_test (const char *name,grub_ieee1275_cell_t *missing)Where are these functions used? Without that knowledge it is difficult to say they are correct or not including return values types. I think this patch should be merged with patch which call this functions.
These functions used by Patch -18 ([PATCH v3 18/25] ieee1275: Read the DB and DBX secure boot variables) Still do you think it should be merged with Patch -18.
Quoted 22 lines
Quoted 22 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +21
-0+{ +struct test_args + { +struct grub_ieee1275_common_hdr common; +grub_ieee1275_cell_t name; +grub_ieee1275_cell_t missing; + } args; + +INIT_IEEE1275_COMMON (&args.common,"test" ,1 ,1 ); + args.name = (grub_ieee1275_cell_t ) name; + +if (IEEE1275_CALL_ENTRY_FN (&args) ==-1 ) +return -1 ; + +if (args.missing ==IEEE1275_CELL_INVALID ) +return -1 ; + + *missing = args.missing; + +return 0 ; +}
Quoted 5 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c line 49
Quoted 2 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +2
-0+int +grub_ieee1275_pks_max_object_size (grub_size_t *result)Ditto and below…
Thanks
Sudhakar
Quoted 13 lines
Quoted 13 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +13
-0+{ +struct mos_args + { +struct grub_ieee1275_common_hdr common; +grub_ieee1275_cell_t size; + } args; + +INIT_IEEE1275_COMMON (&args.common,"pks-max-object-size" ,0 ,1 ); + +if (IEEE1275_CALL_ENTRY_FN (&args) ==-1 ) +return -1 ; + +if (args.size ==IEEE1275_CELL_INVALID )
Quoted 8 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c line 70
Quoted 6 lines
grub-core/kern/powerpc/ieee1275/ieee1275.c new file +6
-0+return -1 ; + + *result = args.size; + +return 0 ; +}Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 17/25] ieee1275: Platform Keystore (PKS) Support
First of all, please turn off HTML formatting...
On Tue, Jul 01, 2025 at 10:11:48PM +0530, Sudhakar Kuppusamy wrote:
[...]
Quoted 8 lines
Where are these functions used? Without that knowledge it is difficult
to say they are correct or not including return values types. I think this patch should be merged with patch which call this functions.These functions used by Patch -18 ([PATCH v3 18/25] ieee1275: Read the DB and DBX secure boot variables)
Still do you think it should be merged with Patch -18.
Yes, please...
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 18/25 ieee1275: Read the DB and DBX secure boot variables
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
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 | 1 + grub-core/Makefile.core.def | 1 + grub-core/kern/ieee1275/init.c | 16 +- .../kern/powerpc/ieee1275/platform_keystore.c | 332 ++++++++++++++++++ .../grub/powerpc/ieee1275/platform_keystore.h | 233 ++++++++++++ include/grub/types.h | 9 + 6 files changed, 590 insertions(+), 2 deletions(-) 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 +1 -0
@@ -247,6 +247,7 @@ 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 +1 -0
@@ -333,6 +333,7 @@ kernel = { 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/init.c +14 -2
@@ -50,6 +50,8 @@#include <grub/ieee1275/alloc.h> #endif #include <grub/lockdown.h> +#include <grub/powerpc/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> /* The maximum heap size we're going to claim at boot. Not used by sparc. */ #ifdef __i386__
@@ -1001,7 +1003,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 =0 ; rc =grub_ieee1275_finddevice ("/" , &root);if (rc !=0 )
@@ -1026,7 +1029,16 @@ grub_get_ieee1275_secure_boot (void)* We only support enforce. */ if (is_sb >=2 ) -grub_lockdown (); + { +grub_dprintf ("ieee1275" ,"Secure Boot Enabled\n" ); + err =grub_pks_keystore_init (); +if (err !=GRUB_ERR_NONE ) +grub_error (err,"Initialization of the Platform Keystore failed!\n" ); + +grub_lockdown (); + } +else +grub_dprintf ("ieee1275" ,"Secure Boot Disabled\n" ); }grub_addr_t grub_modbase;
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +332 -0
332 lines not on this page · raw message
include/grub/powerpc/ieee1275/platform_keystore.h new file +233 -0
233 lines not on this page · raw message
include/grub/types.h +9 -0
@@ -388,4 +388,13 @@ struct grub_packed_guid } GRUB_PACKED;typedef struct grub_packed_guid grub_packed_guid_t ; + +#define GRUB_UUID_SIZE 16 +typedef struct grub_uuid grub_uuid_t ; +/* The structure of a UUID.*/ +struct grub_uuid +{ +grub_uint8_t b[GRUB_UUID_SIZE ]; +}; +#endif /* ! GRUB_TYPES_HEADER */
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 18/25] ieee1275: Read the DB and DBX secure boot variables
Quoted 71 lines · Sudhakar
From: Sudhakar Kuppusamy <[email protected]>
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 enabledNote:-
SB Version - Secure Boot mode
1 - PKS
0 - static key (embeded key)Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- grub-core/Makefile.am | 1 + grub-core/Makefile.core.def | 1 + grub-core/kern/ieee1275/init.c | 16 +- .../kern/powerpc/ieee1275/platform_keystore.c | 332 ++++++++++++++++++ .../grub/powerpc/ieee1275/platform_keystore.h | 233 ++++++++++++ include/grub/types.h | 9 + 6 files changed, 590 insertions(+), 2 deletions(-) create mode 100644 grub-core/kern/powerpc/ieee1275/platform_keystore.c create mode 100644 include/grub/powerpc/ieee1275/platform_keystore.hgrub-core/Makefile.am +1
-0@@ -247,6 +247,7 @@ 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_ieee1275grub-core/Makefile.core.def +1
-0@@ -333,6 +333,7 @@ kernel = { 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/init.c +2
-0@@ -50,6 +50,8 @@#include <grub/ieee1275/alloc.h> #endif #include <grub/lockdown.h> +#include <grub/powerpc/ieee1275/ieee1275.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> /* The maximum heap size we're going to claim at boot. Not used by sparc. */ #ifdef __i386__
grub-core/kern/ieee1275/init.c grub_get_ieee1275_secure_boot (void) · line 1007
Quoted 6 lines
grub-core/kern/ieee1275/init.c +2
-1@@ -1001,7 +1003,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 =0 ;
Please define and use constants for this.
Quoted 43 lines
grub-core/kern/ieee1275/init.c +10
-1rc =grub_ieee1275_finddevice ("/" , &root);if (rc !=0 )@@ -1026,7 +1029,16 @@ grub_get_ieee1275_secure_boot (void)* We only support enforce. */ if (is_sb >=2 ) -grub_lockdown (); + { +grub_dprintf ("ieee1275" ,"Secure Boot Enabled\n" ); + err =grub_pks_keystore_init (); +if (err !=GRUB_ERR_NONE ) +grub_error (err,"Initialization of the Platform Keystore failed!\n" ); + +grub_lockdown (); + } +else +grub_dprintf ("ieee1275" ,"Secure Boot Disabled\n" ); }grub_addr_t grub_modbase;grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +17
-0@@ -0,0 +1,332 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2024 Free Software Foundation, Inc. +* Copyright (C) 2022, 2023, 2024, 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/>.
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 27
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+*/ + +#include <grub/mm.h> +#include <grub/powerpc/ieee1275/ieee1275.h> +#include <grub/types.h> +#include <grub/misc.h> +#include <grub/lockdown.h> +#include <grub/powerpc/ieee1275/platform_keystore.h> + +#define PKS_CONSUMER_FW ((grub_uint8_t) 1)
Why do you glue all different constants into one block? Please separate them logically with empty lines.
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 32
Quoted 5 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +5
-0+#define SB_VERSION_KEY_NAME ((grub_uint8_t *) "SB_VERSION") +#define SB_VERSION_KEY_LEN ((grub_size_t) 10) +#define DB ((grub_uint8_t) 1) +#define DBX ((grub_uint8_t) 2) +#define PKS_OBJECT_NOT_FOUND ((grub_err_t) -7)
Why do you need PKS_OBJECT_NOT_FOUND constant? Why cannot you use grub_err_t enum type directly? If it is really needed please do not use plain number but an enum constant. And probably a comment explaining why the PKS_OBJECT_NOT_FOUND is needed would be nice...
... and align all values in a column with tab(s).
[...]
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 197
Quoted 5 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +5
-0+/* Test the availability of PKS support. */ +static grub_err_t +is_support_pks (void ) +{ +grub_err_t rc =GRUB_ERR_NONE ;
You do not need this assignment.
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 200
Quoted 3 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +3
-0+grub_ieee1275_cell_t missing =0 ; + + rc =grub_ieee1275_test ("pks-max-object-size" , &missing);
This function returns an int and you cast it to grub_err_t. I do not mention that returned values do not map to grub_err_t constants at all. Please fix this. I think grub_ieee1275_test() should return a grub_err_t.
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 201
Quoted 1 line
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +1
-0+if (rc !=GRUB_ERR_NONE || (int ) missing ==-1 )
s/-1/GRUB_IEEE1275_CELL_INVALID/?
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 205
Quoted 4 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +4
-0+ rc =grub_error (GRUB_ERR_BAD_FIRMWARE ,"firmware doesn't have PKS support\n" ); + else + { + rc =grub_ieee1275_pks_max_object_size (&pks_max_object_size);
Same problems with types mismatch as above. Please fix this problem everywhere.
Quoted 13 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +13
-0+if (rc !=GRUB_ERR_NONE ) + rc =grub_error (GRUB_ERR_BAD_NUMBER ,"PKS support is there but it has zero objects\n" ); + } + +return rc; +} + +/* +* Retrieve the secure boot variable from PKS, unpacks it, read the esd +* from ESL, and store the information in the pks sd buffer. +*/ +static grub_err_t +read_secure_boot_variables (const grub_uint8_t sbvarflags, const grub_uint8_t sbvartype,
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 228
Quoted 10 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +10
-0+ grub_pks_sd_t **pks_sd, grub_size_t *pks_sd_entries) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_uint8_t *esl_data =NULL ; +grub_size_t esl_data_size =0 ; + + rc =read_sbvar_from_pks (sbvarflags, sbvartype, &esl_data, &esl_data_size); +/* +* at this point we have SB_VERSION, so any error is worth +* at least some user-visible info
May I ask you to use proper English in all comments, i.e. start with upper case, variable/function names are exceptions, and end with full stop? Additionally, please differentiate functions by adding "()" at the end of their names. These issues have to be fixed in many places not only in this patch.
grub-core/kern/powerpc/ieee1275/platform_keystore.c line 241
Quoted 13 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +13
-0+*/ +if (rc !=GRUB_ERR_NONE ) + rc =grub_error (rc,"secure boot variable %s reading (%d)" , + (sbvartype ==DB ?"db" :"dbx" ), (int ) rc); +else if (esl_data_size !=0 ) + rc =pks_sd_from_esl ((const grub_uint8_t *) esl_data, esl_data_size, + pks_sd, pks_sd_entries); +grub_free (esl_data); + +return rc; +} + +/* reads secure boot version (SB_VERSION) and it supports following
Wrong coding style for comments...
Quoted 116 lines
grub-core/kern/powerpc/ieee1275/platform_keystore.c new file +91
-0+* SB_VERSION +* 1 - PKS +* 0 - static key (embeded key) +*/ +static grub_err_t +get_secure_boot_version (void ) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_uint8_t *data =NULL ; +grub_size_t len =0 , policy =0 ; + + rc =read_sbversion_from_pks (&data, &len, &policy); +if (rc !=GRUB_ERR_NONE ) + rc =grub_error (GRUB_ERR_READ_ERROR ,"SB version read failed (%d)\n" , (int ) rc); +else if (len !=1 || (*data >=2 )) + rc =grub_error (GRUB_ERR_BAD_NUMBER ,"found unexpected SB version (%d)\n" , *data); + +if (rc !=GRUB_ERR_NONE ) + { +grub_printf ("switch to static key!\n" ); +if (grub_is_lockdown () ==GRUB_LOCKDOWN_ENABLED ) +grub_fatal ("secure boot locked down" ); + } +else + grub_pks_use_keystore = *data; + +grub_free (data); + +return rc; +} + +/* Free allocated memory */ +void +grub_pks_free_keystore (void ) +{ +grub_size_t i =0 ; + +for (i =0 ; i < grub_pks_keystore.db_entries; i++) +grub_free (grub_pks_keystore.db[i].data); + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) +grub_free (grub_pks_keystore.dbx[i].data); + +grub_free (grub_pks_keystore.db); +grub_free (grub_pks_keystore.dbx); +grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t)); +} + +/* Initialization of the Platform Keystore */ +grub_err_t +grub_pks_keystore_init (void ) +{ +grub_err_t rc =GRUB_ERR_NONE ; + +grub_dprintf ("ieee1275" ,"trying to load Platform Keystore\n" ); + + rc =is_support_pks (); +if (rc !=GRUB_ERR_NONE ) + { +grub_printf ("switch to static key\n" ); +return rc; + } + +/* SB_VERSION */ + rc =get_secure_boot_version (); +if (rc !=GRUB_ERR_NONE ) +return rc; + +if (grub_pks_use_keystore) + { +grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t)); +/* DB */ + rc =read_secure_boot_variables (0 ,DB , &grub_pks_keystore.db, &grub_pks_keystore.db_entries); +if (rc ==GRUB_ERR_NONE ) + { +/* DBX */ + rc =read_secure_boot_variables (0 ,DBX , &grub_pks_keystore.dbx, &grub_pks_keystore.dbx_entries); +if (rc ==PKS_OBJECT_NOT_FOUND ) + { +grub_dprintf ("ieee1275" ,"dbx is not found in PKS\n" ); + rc =GRUB_ERR_NONE ; + } + } + + } + +if (rc !=GRUB_ERR_NONE ) +grub_pks_free_keystore (); + +return rc; +}include/grub/powerpc/ieee1275/platform_keystore.h new file +19
-0@@ -0,0 +1,233 @@ +/* +* Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved. This +* program and the accompanying materials are licensed and made available +* under the terms and conditions of the 2-Clause BSD License which +* accompanies this distribution. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
include/grub/powerpc/ieee1275/platform_keystore.h line 29
Quoted 10 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +10
-0+* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +*
Please drop this extra empty line...
Quoted 13 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +13
-0+* https://github.com/tianocore/edk2-staging (edk2-staging repo of tianocore), +* the ImageAuthentication.h file under it, and here's the copyright and license. +* +* MdePkg/Include/Guid/ImageAuthentication.h +* +* Copyright 2022, 2023, 2024, 2025 IBM Corp. +*/ + +#ifndef __PLATFORM_KEYSTORE_H__ +#define __PLATFORM_KEYSTORE_H__ + +#include <grub/symbol.h> +#include <grub/mm.h>
include/grub/powerpc/ieee1275/platform_keystore.h line 52
Quoted 10 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +10
-0+#include <grub/types.h> + +#if __GNUC__ >=9 +#pragma GCC diagnostic ignored "-Waddress-of-packed-member" +#endif + +#define GRUB_MAX_HASH_SIZE 64 + +typedef struct grub_esd grub_esd_t ; +typedef struct grub_esl grub_esl_t ;
These two lines should go behind immediately after relevant struct definitions... Good examples are all over the GRUB code...
include/grub/powerpc/ieee1275/platform_keystore.h line 65
Quoted 12 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +12
-0+/* +* It is derived from EFI_SIGNATURE_DATA +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +* +* The structure of an EFI signature database (ESD).*/ +struct grub_esd +{ +/* +* An identifier which identifies the agent which added +* the signature to the list. +*/ +grub_uuid_t signatureowner;
s/signatureowner/signature_owner/
include/grub/powerpc/ieee1275/platform_keystore.h line 67
Quoted 2 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +2
-0+/* The format of the signature is defined by the SignatureType.*/ +grub_uint8_t signaturedata[];
s/signaturedata/signature_data/
include/grub/powerpc/ieee1275/platform_keystore.h line 78
Quoted 11 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +11
-0+}GRUB_PACKED ; + +/* +* It is derived from EFI_SIGNATURE_LIST +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +* +* The structure of an EFI signature list (ESL).*/ +struct grub_esl +{ +/* Type of the signature. GUID signature types are defined in below.*/ +grub_uuid_t signaturetype;
s/signaturetype/signature_type/
include/grub/powerpc/ieee1275/platform_keystore.h line 80
Quoted 2 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +2
-0+/* Total size of the signature list, including this header.*/ +grub_uint32_t signaturelistsize;
s/signaturelistsize/signature_list_size/
include/grub/powerpc/ieee1275/platform_keystore.h line 85
Quoted 5 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +5
-0+/* +* Size of the signature header which precedes +* the array of signatures. +*/ +grub_uint32_t signatureheadersize;
s/signatureheadersize/signature_header_size/
include/grub/powerpc/ieee1275/platform_keystore.h line 87
Quoted 2 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +2
-0+/* Size of each signature.*/ +grub_uint32_t signaturesize;
s/signaturesize/signature_size/
Quoted 89 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +89
-0+}GRUB_PACKED ; + +/* +* It is derived from EFI_CERT_X509_GUID +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +*/ +#define GRUB_PKS_CERT_X509_GUID \ + (grub_uuid_t) \ + { \ + { \ + 0xa1, 0x59, 0xc0, 0xa5, 0xe4, 0x94, \ + 0xa7, 0x4a, 0x87, 0xb5, 0xab, 0x15, \ + 0x5c, 0x2b, 0xf0, 0x72 \ + } \ + } + +/* +* It is derived from EFI_CERT_SHA256_GUID +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +*/ +#define GRUB_PKS_CERT_SHA256_GUID \ + (grub_uuid_t) \ + { \ + { \ + 0x26, 0x16, 0xc4, 0xc1, 0x4c, 0x50, \ + 0x92, 0x40, 0xac, 0xa9, 0x41, 0xf9, \ + 0x36, 0x93, 0x43, 0x28 \ + } \ + } + +/* +* It is derived from EFI_CERT_SHA384_GUID +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +*/ +#define GRUB_PKS_CERT_SHA384_GUID \ + (grub_uuid_t) \ + { \ + { \ + 0x07, 0x53, 0x3e, 0xff, 0xd0, 0x9f, \ + 0xc9, 0x48, 0x85, 0xf1, 0x8a, 0xd5, \ + 0x6c, 0x70, 0x1e, 0x1 \ + } \ + } + +/* +* It is derived from EFI_CERT_SHA512_GUID +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +*/ +#define GRUB_PKS_CERT_SHA512_GUID \ + (grub_uuid_t) \ + { \ + { \ + 0xae, 0x0f, 0x3e, 0x09, 0xc4, 0xa6, \ + 0x50, 0x4f, 0x9f, 0x1b, 0xd4, 0x1e, \ + 0x2b, 0x89, 0xc1, 0x9a \ + } \ + } + +/* +* It is derived from EFI_CERT_X509_SHA256_GUID +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +*/ +#define GRUB_PKS_CERT_X509_SHA256_GUID \ + (grub_uuid_t) \ + { \ + { \ + 0x92, 0xa4, 0xd2, 0x3b, 0xc0, 0x96, \ + 0x79, 0x40, 0xb4, 0x20, 0xfc, 0xf9, \ + 0x8e, 0xf1, 0x03, 0xed \ + } \ + } + +/* +* It is derived from EFI_CERT_X509_SHA384_GUID +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h +*/ +#define GRUB_PKS_CERT_X509_SHA384_GUID \ + (grub_uuid_t) \ + { \ + { \ + 0x6e, 0x87, 0x76, 0x70, 0xc2, 0x80, \ + 0xe6, 0x4e, 0xaa, 0xd2, 0x28, 0xb3, \ + 0x49, 0xa6, 0x86, 0x5b \ + } \ + } + +/* +* It is derived from EFI_CERT_X509_SHA512_GUID +* https://github.com/tianocore/edk2-staging/blob/master/MdePkg/Include/Guid/ImageAuthentication.h
include/grub/powerpc/ieee1275/platform_keystore.h line 186
Quoted 10 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +10
-0+*/ +#define GRUB_PKS_CERT_X509_SHA512_GUID \ + (grub_uuid_t) \ + { \ + { \ + 0x63, 0xbf, 0x6d, 0x44, 0x02, 0x25, \ + 0xda, 0x4c, 0xbc, 0xfa, 0x24, 0x65, \ + 0xd2, 0xb0, 0xfe, 0x9d \ + } \ + }
All GRUB_PKS_* constants should go to the new include/grub/efi/pks.h file... The include/grub/efi/api.h shows how they should be defined...
include/grub/powerpc/ieee1275/platform_keystore.h line 189
Quoted 2 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +2
-0+typedef struct grub_pks_sd grub_pks_sd_t ; +typedef struct grub_pks grub_pks_t ;
Same comment for these typedefs as above...
Quoted 23 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +23
-0+/* The structure of a PKS signature data.*/ +struct grub_pks_sd +{ +grub_uuid_t guid;/* signature type */ +grub_uint8_t *data;/* signature data */ +grub_size_t data_size;/* size of signature data */ +}GRUB_PACKED ; + +/* The structure of a PKS.*/ +struct grub_pks +{ +grub_pks_sd_t *db;/* signature database */ +grub_pks_sd_t *dbx;/* forbidden signature database */ +grub_size_t db_entries;/* size of signature database */ +grub_size_t dbx_entries;/* size of forbidden signature database */ +}GRUB_PACKED ; + +#ifdef __powerpc__ + +/* Initialization of the Platform Keystore */ +extern grub_err_t +grub_pks_keystore_init (void ); +/* Free allocated memory */
include/grub/powerpc/ieee1275/platform_keystore.h line 223
Quoted 10 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +10
-0+extern void +EXPORT_FUNC (grub_pks_free_keystore ) (void ); + +extern grub_uint8_t EXPORT_VAR (grub_pks_use_keystore ); +extern grub_pks_t EXPORT_VAR (grub_pks_keystore ); + +#else + +/* Free allocated memory */ +inline void
static inline void
include/grub/powerpc/ieee1275/platform_keystore.h line 229
Quoted 6 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +6
-0+grub_pks_free_keystore (void) +{ +} + +grub_uint8_t grub_pks_use_keystore =0 ; +grub_pks_t grub_pks_keystore = {NULL ,NULL ,0 ,0 };
These variables cannot be defined in header file.
Quoted 9 lines
include/grub/powerpc/ieee1275/platform_keystore.h new file +3
-0+#endif + +#endif include/grub/types.h +0
-0@@ -388,4 +388,13 @@ struct grub_packed_guid }GRUB_PACKED ;
include/grub/types.h struct grub_packed_guid · line 398
Quoted 10 lines
include/grub/types.h +8
-0typedef struct grub_packed_guid grub_packed_guid_t ; + +#define GRUB_UUID_SIZE 16 +typedef struct grub_uuid grub_uuid_t ; +/* The structure of a UUID.*/ +struct grub_uuid +{ +grub_uint8_t b[GRUB_UUID_SIZE ]; +};
Why cannot you use existing grub_guid_t/grub_packed_guid_t types?
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 19/25 appendedsig: The creation of trusted and distrusted lists
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
The trusted certificates and binary hashes, distrusted certificates and binary/certificate hashes will be extracted from the platform keystore buffer if Secure Boot is enabled with PKS.
In order to verify the integrity of the kernel, the extracted data needs to be stored stored in the buffer db and dbx.
The trusted certificates will be extracted from the grub ELFNOTE if Secure Boot is enabled with static key. In order to verify the integerity of the kernel, the extracted data needs to be stored in the buffer db.
Note:-
If neither the trusted certificate nor binary hash exists in the distrusted list (dbx), rejects it while extracting certificate/binary hash from the platform keystore buffer.
--- grub-core/commands/appendedsig/appendedsig.c | 584 +++++++++++++++++-- grub-core/kern/file.c | 34 ++ include/grub/file.h | 1 + 3 files changed, 577 insertions(+), 42 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +542 -42
@@ -34,7 +34,7 @@#include <libtasn1.h> #include <grub/env.h> #include <grub/lockdown.h> - +#include <grub/powerpc/ieee1275/platform_keystore.h> #include "appendedsig.h" GRUB_MOD_LICENSE ("GPLv3+" );
@@ -64,8 +64,23 @@ struct grub_appended_signaturestruct pkcs7_signedData pkcs7;/* Parsed PKCS#7 data */ }; -/* Trusted certificates for verifying appended signatures */ -struct x509_certificate *grub_trusted_key; +/* This represents a trusted/distrusted list*/ +struct grub_database +{ +struct x509_certificate *keys;/* Certificates */ +grub_size_t key_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 */ +}; + +/* Trusted list */ +struct grub_database db = {.keys =NULL , .key_entries =0 , .signatures =NULL , + .signature_size =NULL , .signature_entries =0 }; + +/* Distrusted list */ +struct grub_database dbx = {.signatures =NULL , .signature_size =NULL , + .signature_entries =0 };/* * Force gcry_rsa to be a module dependency.
@@ -87,6 +102,13 @@ struct x509_certificate *grub_trusted_key;* also resolves our concerns about loading from the filesystem. */ extern gcry_pk_spec_t _gcry_pubkey_spec_rsa; +extern gcry_md_spec_t _gcry_digest_spec_sha224; +extern gcry_md_spec_t _gcry_digest_spec_sha384; + +/* Free trusted list memory */ +static void free_trusted_list (void ); +/* Free distrusted list memory */ +static void free_distrusted_list (void ); static enum {
@@ -95,6 +117,204 @@ 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_uuid_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_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_UUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha256; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_UUID_SIZE ) ==0 ) + hash_func = &_gcry_digest_spec_sha384; +else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_UUID_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 trusted/distrusted 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_trusted_list/free_distrusted_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 int +is_x509 (const grub_uuid_t *guid) +{ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_GUID ,GRUB_UUID_SIZE ) ==0 ) +return GRUB_ERR_NONE ; + +return GRUB_ERR_UNKNOWN_COMMAND ; +} + +static int +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 GRUB_ERR_NONE ; + +return GRUB_ERR_UNKNOWN_COMMAND ; +} + +/* +* Verify the certificate against the certificate from platform keystore buffer's +* distrusted list. +*/ +static grub_err_t +is_distrusted_cert (const struct x509_certificate *db_cert) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =0 ; +struct x509_certificate *distrusted_cert =NULL ; + +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) ==GRUB_ERR_NONE ) + { + 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) ==GRUB_ERR_NONE ) + { +grub_printf ("Warning: a trusted certificate CN='%s' is ignored " +"because it is on the distrusted list (dbx).\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 trusted/distrusted list */ +static grub_err_t +add_certificate (const grub_uint8_t *data,const grub_size_t data_size, +struct grub_database *database,const grub_size_t is_db) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t key_entries = database->key_entries; +struct x509_certificate *cert =NULL ; + +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" ,"skipping %s certificate (%d)\n" , + (is_db ?"trusted" :"distrusted" ), (int ) rc); +grub_free (cert); +return rc; + } + +if (is_db) + { + rc =is_distrusted_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 ?"trusted" :"distrusted" ), cert->subject); + + key_entries++; + cert->next = database->keys; + database->keys = cert; + database->key_entries = key_entries; + +return rc; +} +static const char *grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),const char *val __attribute__ ((unused)))
@@ -258,7 +478,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)struct pkcs7_signerInfo *si;int i; -if (grub_trusted_key ==NULL ) +if (!db.key_entries)return grub_error (GRUB_ERR_BAD_SIGNATURE ,"no trusted keys to verify against" ); err =extract_appended_signature (buf, bufsize, &sig);
@@ -289,7 +509,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 = grub_trusted_key; pk; pk = pk->next) +for (pk = db.keys; pk; pk = pk->next) { rc =grub_crypto_rsa_pad (&hashmpi, hash, si->hash, pk->mpis[0 ]);if (rc !=GPG_ERR_NO_ERROR )
@@ -384,16 +604,16 @@ grub_cmd_distrust (grub_command_t cmd __attribute__ ((unused)), int argc, char *if (cert_num ==1 ) { - cert = grub_trusted_key; - grub_trusted_key = cert->next; + cert = db.keys; + db.keys = cert->next;certificate_release (cert);grub_free (cert);return GRUB_ERR_NONE ; } i =2 ; - prev = grub_trusted_key; - cert = grub_trusted_key->next; + prev = db.keys; + cert = db.keys->next;while (cert) {if (i == cert_num)
@@ -441,8 +661,8 @@ grub_cmd_trust (grub_command_t cmd __attribute__ ((unused)), int argc, char **argrub_dprintf ("appendedsig" ,"loaded certificate with CN: %s\n" , cert->subject); - cert->next = grub_trusted_key; - grub_trusted_key = cert; + cert->next = db.keys; + db.keys = cert;return GRUB_ERR_NONE ; }
@@ -455,7 +675,7 @@ grub_cmd_list (grub_command_t cmd __attribute__ ((unused)), int argc __attributeint cert_num =1 ;grub_size_t i; -for (cert = grub_trusted_key; cert; cert = cert->next) +for (cert = db.keys; cert; cert = cert->next) {grub_printf ("Certificate %d:\n" , cert_num);grub_printf ("\tSerial: " );
@@ -546,6 +766,274 @@ static struct grub_fs pseudo_fs = {static grub_command_t cmd_verify, cmd_list, cmd_distrust, cmd_trust; +/* +* Verify the trusted certificate against the certificate hashes from platform keystore buffer's +* distrusted list. +*/ +static grub_err_t +is_distrusted_cert_hash (const grub_uint8_t *data,const grub_size_t data_size) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =0 , cert_hash_size =0 ; +grub_uint8_t cert_hash[GRUB_MAX_HASH_SIZE ] = {0 }; + +if (data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"trusted certificate data or size is empty" ); + +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 distrusted list (dbx).\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 GRUB_ERR_BAD_SIGNATURE ; + } + } + +return GRUB_ERR_NONE ; +} + +/* +* Verify the trusted binary hash against the platform keystore buffer's +* distrusted list. +*/ +static grub_err_t +is_distrusted_binary_hash (const grub_uint8_t *binary_hash, +const grub_size_t binary_hash_size) +{ +grub_size_t i =0 ; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data ==NULL || + grub_pks_keystore.dbx[i].data_size ==0 ) +continue ; + +if (binary_hash_size == grub_pks_keystore.dbx[i].data_size && +grub_memcmp (grub_pks_keystore.dbx[i].data, binary_hash, binary_hash_size) ==0 ) + { +grub_printf ("Warning: a trusted binary hash (%02x%02x%02x%02x) is ignored" +" because it is on the distrusted list (dbx).\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 GRUB_ERR_BAD_SIGNATURE ; + } + } + +return GRUB_ERR_NONE ; +} + +/* +* Extract the binary hashes from the platform keystore buffer, +* and add it to the trusted list if it does not exist in the distrusted list. +*/ +static grub_err_t +add_trusted_binary_hash (const grub_uint8_t **data,const grub_size_t data_size) +{ +grub_err_t rc =GRUB_ERR_NONE ; + +if (*data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"trusted binary hash data or size is empty" ); + + rc =is_distrusted_binary_hash (*data, data_size); +if (rc !=GRUB_ERR_NONE ) +return rc; + + rc =add_hash (data, data_size, &db.signatures, &db.signature_size, + &db.signature_entries); +return rc; +} + +static int +is_hash (const grub_uuid_t *guid) +{ +/* GUID type of the binary hash */ +if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_UUID_SIZE ) ==0 ) +return GRUB_ERR_NONE ; + +/* GUID type of the certificate hash */ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_UUID_SIZE ) ==0 ) +return GRUB_ERR_NONE ; + +return GRUB_ERR_UNKNOWN_COMMAND ; +} + +/* +* Extract the x509 certificates/binary hashes from the platform keystore buffer, +* parse it, and add it to the trusted list. +*/ +static grub_err_t +create_trusted_list (void ) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =0 ; + +for (i =0 ; i < grub_pks_keystore.db_entries; i++) + { +if (is_hash (&grub_pks_keystore.db[i].guid) ==GRUB_ERR_NONE ) + { + rc =add_trusted_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) ==GRUB_ERR_NONE ) + { + rc =is_distrusted_cert_hash (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size); +if (rc !=GRUB_ERR_NONE ) +continue ; + + rc =add_certificate (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size, &db,1 ); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; +else if (rc !=GRUB_ERR_NONE ) +continue ; + } +else +grub_dprintf ("appendedsig" ,"unsupported signature data type and " +"skipping trusted data (%" PRIuGRUB_SIZE")\n" , i +1 ); + } + +return GRUB_ERR_NONE ; +} + +/* +* Extract the certificates, certificate/binary hashes out of the platform keystore buffer, +* and add it to the distrusted list. +*/ +static grub_err_t +create_distrusted_list (void ) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =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 ) + { +if (is_x509 (&grub_pks_keystore.dbx[i].guid) ==GRUB_ERR_NONE ) + { + rc =add_certificate (grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, &dbx,0 ); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; + } +else if (is_hash (&grub_pks_keystore.dbx[i].guid) ==GRUB_ERR_NONE ) + { + 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 " +"skipping distrusted data (%" PRIuGRUB_SIZE")\n" , i +1 ); + } + } + +return rc; +} + +/* +* Extract the x509 certificates from the ELF note header, +* parse it, and add it to the trusted list. +*/ +static grub_err_t +build_static_trusted_list (const struct grub_module_header *header) +{ +grub_err_t err =GRUB_ERR_NONE ; +struct grub_file pseudo_file; +grub_uint8_t *cert_data =NULL ; +grub_ssize_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 =grub_read_file (&pseudo_file, &cert_data, &cert_data_size); +if (err !=GRUB_ERR_NONE ) +return err; + + err =add_certificate (cert_data, cert_data_size, &db,1 ); +grub_free (cert_data); + +return err; +} + +/* releasing memory */ +static void +free_trusted_list (void ) +{ +struct x509_certificate *cert; +grub_size_t i =0 ; + +while (db.keys !=NULL ) + { + cert = db.keys; + db.keys = db.keys->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)); +} + +/* releasing memory */ +static void +free_distrusted_list (void ) +{ +struct x509_certificate *cert; +grub_size_t i =0 ; + +while (dbx.keys !=NULL ) + { + cert = dbx.keys; + dbx.keys = dbx.keys->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;
@@ -555,7 +1043,6 @@ GRUB_MOD_INIT (appendedsig)if (grub_is_lockdown () ==GRUB_LOCKDOWN_ENABLED )CHECK_SIGS =CHECK_SIGS_FORCED ; - grub_trusted_key =NULL ;grub_register_variable_hook ("check_appended_signatures" , grub_env_read_sec, grub_env_write_sec);grub_env_export ("check_appended_signatures" );
@@ -563,37 +1050,50 @@ 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); +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 ; -grub_dprintf ("appendedsig" ,"loaded certificate CN='%s'\n" , pk->subject); + rc =build_static_trusted_list (header); +if (rc !=GRUB_ERR_NONE ) + { +free_trusted_list (); +grub_error (rc,"static trusted list creation failed" ); + } +else +grub_dprintf ("appendedsig" ,"the trusted list now has %" PRIuGRUB_SIZE" static keys\n" , + db.key_entries); + } + } +else if (grub_pks_use_keystore &&CHECK_SIGS ==CHECK_SIGS_FORCED ) + { + rc =create_trusted_list (); +if (rc !=GRUB_ERR_NONE ) + { +free_trusted_list (); +grub_error (rc,"trusted list creation failed" ); + } +else + { + rc =create_distrusted_list (); +if (rc !=GRUB_ERR_NONE ) + { +free_trusted_list (); +free_distrusted_list (); +grub_error (rc,"distrusted list creation failed" ); + } +else +grub_dprintf ("appendedsig" ,"the trusted list now has %" PRIuGRUB_SIZE" keys.\n" +"the distrusted list now has %" PRIuGRUB_SIZE" keys.\n" , + db.signature_entries + db.key_entries, dbx.signature_entries); + } - pk->next = grub_trusted_key; - grub_trusted_key = pk; - } +grub_pks_free_keystore (); + } cmd_trust =grub_register_command ("trust_certificate" , grub_cmd_trust,N_ ("X509_CERTIFICATE" ),N_ ("Add X509_CERTIFICATE to trusted certificates" ));
grub-core/kern/file.c +34 -0
@@ -231,3 +231,37 @@ grub_file_seek (grub_file_t file, grub_off_t offset)return old; } + +grub_err_t +grub_read_file (const grub_file_t file,grub_uint8_t **data,grub_ssize_t *data_size) +{ +grub_uint8_t *buffer =NULL ; +grub_ssize_t read_size =0 ; +grub_off_t total_read_size =0 ; +grub_off_t file_size =grub_file_size (file); + +if (file_size ==GRUB_FILE_SIZE_UNKNOWN ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("could not determine the size of the file" )); + + buffer =grub_zalloc (file_size); +if (buffer ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,N_ ("out of memory" )); + +while (total_read_size < file_size) + { + read_size =grub_file_read (file, &buffer[total_read_size], file_size - total_read_size); +if (read_size <0 ) + { +grub_free (buffer); +return grub_error (GRUB_ERR_READ_ERROR ,N_ ("unable to read the file" )); + } + + total_read_size += read_size; + } + + *data = buffer; + *data_size = total_read_size; + +return GRUB_ERR_NONE ; +}
include/grub/file.h +1 -0
@@ -217,6 +217,7 @@ grub_ssize_t EXPORT_FUNC(grub_file_read) (grub_file_t file, void *buf, grub_size_t len);grub_off_t EXPORT_FUNC (grub_file_seek ) (grub_file_t file,grub_off_t offset);grub_err_t EXPORT_FUNC (grub_file_close ) (grub_file_t file); +grub_err_t EXPORT_FUNC (grub_read_file ) (const grub_file_t file,grub_uint8_t **data,grub_ssize_t *data_size);/* Return value of grub_file_size() in case file size is unknown. */ #define GRUB_FILE_SIZE_UNKNOWN 0xffffffffffffffffULL
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v3 19/25] appendedsig: The creation of trusted and distrusted lists
Quoted 8 lines · Sudhakar
From: Sudhakar Kuppusamy <[email protected]>
The trusted certificates and binary hashes, distrusted certificates and binary/certificate hashes will be extracted from the platform keystore buffer if Secure Boot is enabled with PKS.
In order to verify the integrity of the kernel, the extracted data needs to be stored stored in the buffer db and dbx.
s/stored stored/stored/
s/buffer db and dbx/db and dbx buffer/?
Quoted 3 lines
The trusted certificates will be extracted from the grub ELFNOTE if Secure Boot is enabled with static key. In order to verify the integerity of the kernel, the extracted data needs to be stored in the buffer db.
What is "buffer db"? "db buffer"? I think it should be better explained here and above.
Quoted 4 lines
Note:-
If neither the trusted certificate nor binary hash exists in the distrusted list (dbx), rejects it while extracting certificate/binary hash from the platform keystore buffer.
I am not sure I understand. Could you elaborate?
Quoted 23 lines
Signed-off-by Sudhakar Kuppusamy <[email protected]>Reviewed-by Stefan Berger <[email protected]>Reviewed-by Avnish Chouhan <[email protected]>--- grub-core/commands/appendedsig/appendedsig.c | 584 +++++++++++++++++-- grub-core/kern/file.c | 34 ++ include/grub/file.h | 1 + 3 files changed, 577 insertions(+), 42 deletions(-)grub-core/commands/appendedsig/appendedsig.c +1
-1@@ -34,7 +34,7 @@#include <libtasn1.h> #include <grub/env.h> #include <grub/lockdown.h> - +#include <grub/powerpc/ieee1275/platform_keystore.h> #include "appendedsig.h" GRUB_MOD_LICENSE ("GPLv3+" );
grub-core/commands/appendedsig/appendedsig.c struct grub_appended_signature · line 70
Quoted 9 lines
grub-core/commands/appendedsig/appendedsig.c +4
-2@@ -64,8 +64,23 @@ struct grub_appended_signaturestruct pkcs7_signedData pkcs7;/* Parsed PKCS#7 data */ }; -/* Trusted certificates for verifying appended signatures */ -struct x509_certificate *grub_trusted_key; +/* This represents a trusted/distrusted list*/ +struct grub_database +{ +struct x509_certificate *keys;/* Certificates */
The comment says "Certificates" but member name is keys. Something is off here. Taking into account type name it seems to me it should be named "certs".
grub-core/commands/appendedsig/appendedsig.c struct grub_appended_signature · line 71
Quoted 1 line
grub-core/commands/appendedsig/appendedsig.c +1
-0+grub_size_t key_entries;/* Number of certificates */
Ditto but s/key_entries/cert_entries/ of course...
Quoted 38 lines
grub-core/commands/appendedsig/appendedsig.c +24
-0+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 */ +}; + +/* Trusted list */ +struct grub_database db = {.keys =NULL , .key_entries =0 , .signatures =NULL , + .signature_size =NULL , .signature_entries =0 }; + +/* Distrusted list */ +struct grub_database dbx = {.signatures =NULL , .signature_size =NULL , + .signature_entries =0 };/* * Force gcry_rsa to be a module dependency. @@ -87,6 +102,13 @@ struct x509_certificate *grub_trusted_key;* also resolves our concerns about loading from the filesystem. */ extern gcry_pk_spec_t _gcry_pubkey_spec_rsa; +extern gcry_md_spec_t _gcry_digest_spec_sha224; +extern gcry_md_spec_t _gcry_digest_spec_sha384; + +/* Free trusted list memory */ +static void free_trusted_list (void ); +/* Free distrusted list memory */ +static void free_distrusted_list (void ); static enum {@@ -95,6 +117,204 @@ 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
grub-core/commands/appendedsig/appendedsig.c static enum · line 134
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+get_hash (const grub_uuid_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_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_UUID_SIZE ) ==0 )
Something is off with indention here...
Quoted 55 lines
grub-core/commands/appendedsig/appendedsig.c +55
-0+ hash_func = &_gcry_digest_spec_sha256; + else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID, GRUB_UUID_SIZE) == 0 || + grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID, GRUB_UUID_SIZE) == 0) + hash_func = &_gcry_digest_spec_sha384; + else if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID, GRUB_UUID_SIZE) == 0 || + grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID, GRUB_UUID_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 trusted/distrusted 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_trusted_list/free_distrusted_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++;
grub-core/commands/appendedsig/appendedsig.c static enum · line 199
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+ *data =NULL ; + + *signature_list = signatures; + *signature_size_list = signature_size; + *signature_list_entries = signature_entries; + +return GRUB_ERR_NONE ; +} + +static int
static bool...
grub-core/commands/appendedsig/appendedsig.c static enum · line 205
Quoted 6 lines
grub-core/commands/appendedsig/appendedsig.c +6
-0+is_x509 (const grub_uuid_t *guid) +{ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_GUID ,GRUB_UUID_SIZE ) ==0 ) +return GRUB_ERR_NONE ; + +return GRUB_ERR_UNKNOWN_COMMAND ;
... then return values has to be updated too...
grub-core/commands/appendedsig/appendedsig.c static enum · line 199
Quoted 3 lines
grub-core/commands/appendedsig/appendedsig.c +3
-0+} + +static int
Ditto...
grub-core/commands/appendedsig/appendedsig.c static enum · line 219
Quoted 11 lines
grub-core/commands/appendedsig/appendedsig.c +11
-0+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 GRUB_ERR_NONE; + +return GRUB_ERR_UNKNOWN_COMMAND ;
Ditto and below please...
Quoted 45 lines
grub-core/commands/appendedsig/appendedsig.c +45
-0+} + +/* +* Verify the certificate against the certificate from platform keystore buffer's +* distrusted list. +*/ +static grub_err_t +is_distrusted_cert (const struct x509_certificate *db_cert) +{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =0 ; +struct x509_certificate *distrusted_cert =NULL ; + +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) ==GRUB_ERR_NONE ) + { + 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) ==GRUB_ERR_NONE ) + { +grub_printf ("Warning: a trusted certificate CN='%s' is ignored " +"because it is on the distrusted list (dbx).\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);
grub-core/commands/appendedsig/appendedsig.c static enum · line 274
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+ } + } + +return GRUB_ERR_NONE ; +} + +/* Add the certificate into the trusted/distrusted list */ +static grub_err_t +add_certificate (const grub_uint8_t *data,const grub_size_t data_size, +struct grub_database *database,const grub_size_t is_db)
Why is_db is defined as grub_size_t? Should not it be bool?
Quoted 12 lines
grub-core/commands/appendedsig/appendedsig.c +12
-0+{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t key_entries = database->key_entries; +struct x509_certificate *cert =NULL ; + +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" ); +
grub-core/commands/appendedsig/appendedsig.c static enum · line 296
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+ rc =parse_x509_certificate (data, data_size, cert); +if (rc !=GRUB_ERR_NONE ) + { +grub_dprintf ("appendedsig" ,"skipping %s certificate (%d)\n" , + (is_db ?"trusted" :"distrusted" ), (int ) rc); +grub_free (cert); +return rc; + } + +if (is_db)
This...
grub-core/commands/appendedsig/appendedsig.c static enum · line 308
Quoted 12 lines
grub-core/commands/appendedsig/appendedsig.c +12
-0+ { + rc =is_distrusted_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 ?"trusted" :"distrusted" ), cert->subject);
... and this suggest is_db should be bool...
Quoted 71 lines
grub-core/commands/appendedsig/appendedsig.c +17
-9+ key_entries++; + cert->next = database->keys; + database->keys = cert; + database->key_entries = key_entries; + +return rc; +} +static const char *grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),const char *val __attribute__ ((unused)))@@ -258,7 +478,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)struct pkcs7_signerInfo *si;int i; -if (grub_trusted_key ==NULL ) +if (!db.key_entries)return grub_error (GRUB_ERR_BAD_SIGNATURE ,"no trusted keys to verify against" ); err =extract_appended_signature (buf, bufsize, &sig);@@ -289,7 +509,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 = grub_trusted_key; pk; pk = pk->next) +for (pk = db.keys; pk; pk = pk->next) { rc =grub_crypto_rsa_pad (&hashmpi, hash, si->hash, pk->mpis[0 ]);if (rc !=GPG_ERR_NO_ERROR )@@ -384,16 +604,16 @@ grub_cmd_distrust (grub_command_t cmd __attribute__ ((unused)), int argc, char *if (cert_num ==1 ) { - cert = grub_trusted_key; - grub_trusted_key = cert->next; + cert = db.keys; + db.keys = cert->next;certificate_release (cert);grub_free (cert);return GRUB_ERR_NONE ; } i =2 ; - prev = grub_trusted_key; - cert = grub_trusted_key->next; + prev = db.keys; + cert = db.keys->next;while (cert) {if (i == cert_num)@@ -441,8 +661,8 @@ grub_cmd_trust (grub_command_t cmd __attribute__ ((unused)), int argc, char **argrub_dprintf ("appendedsig" ,"loaded certificate with CN: %s\n" , cert->subject); - cert->next = grub_trusted_key; - grub_trusted_key = cert; + cert->next = db.keys; + db.keys = cert;return GRUB_ERR_NONE ; }@@ -455,7 +675,7 @@ grub_cmd_list (grub_command_t cmd __attribute__ ((unused)), int argc __attributeint cert_num =1 ;grub_size_t i; -for (cert = grub_trusted_key; cert; cert = cert->next) +for (cert = db.keys; cert; cert = cert->next) {grub_printf ("Certificate %d:\n" , cert_num);grub_printf ("\tSerial: " );
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 774
Quoted 9 lines
grub-core/commands/appendedsig/appendedsig.c +6
-0@@ -546,6 +766,274 @@ static struct grub_fs pseudo_fs = {static grub_command_t cmd_verify, cmd_list, cmd_distrust, cmd_trust; +/* +* Verify the trusted certificate against the certificate hashes from platform keystore buffer's +* distrusted list. +*/ +static grub_err_t +is_distrusted_cert_hash (const grub_uint8_t *data,const grub_size_t data_size)
Name suggests the function should return bool instead of grub_err_t...
Quoted 27 lines
grub-core/commands/appendedsig/appendedsig.c +27
-0+{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =0 , cert_hash_size =0 ; +grub_uint8_t cert_hash[GRUB_MAX_HASH_SIZE ] = {0 }; + +if (data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"trusted certificate data or size is empty" ); + +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 distrusted list (dbx).\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]));
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 811
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+return GRUB_ERR_BAD_SIGNATURE ; + } + } + +return GRUB_ERR_NONE ; +} + +/* +* Verify the trusted binary hash against the platform keystore buffer's +* distrusted list.
Hmmm... What do you mean here? Should not it be "Verify the binary hash against the platform keystore buffer's distrusted list."? Or even better "Check the binary hash presence in dbx list."? In general I would use db/dbx instead of distrusted/trusted list in the comments and in the code. Especially if you name the solution as a Secure Boot. It would be nice if your and UEFI implementation share common language where possible...
Quoted 63 lines
grub-core/commands/appendedsig/appendedsig.c +63
-0+*/ +static grub_err_t +is_distrusted_binary_hash (const grub_uint8_t *binary_hash, +const grub_size_t binary_hash_size) +{ +grub_size_t i =0 ; + +for (i =0 ; i < grub_pks_keystore.dbx_entries; i++) + { +if (grub_pks_keystore.dbx[i].data ==NULL || + grub_pks_keystore.dbx[i].data_size ==0 ) +continue ; + +if (binary_hash_size == grub_pks_keystore.dbx[i].data_size && +grub_memcmp (grub_pks_keystore.dbx[i].data, binary_hash, binary_hash_size) ==0 ) + { +grub_printf ("Warning: a trusted binary hash (%02x%02x%02x%02x) is ignored" +" because it is on the distrusted list (dbx).\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 GRUB_ERR_BAD_SIGNATURE ; + } + } + +return GRUB_ERR_NONE ; +} + +/* +* Extract the binary hashes from the platform keystore buffer, +* and add it to the trusted list if it does not exist in the distrusted list. +*/ +static grub_err_t +add_trusted_binary_hash (const grub_uint8_t **data,const grub_size_t data_size) +{ +grub_err_t rc =GRUB_ERR_NONE ; + +if (*data ==NULL || data_size ==0 ) +return grub_error (GRUB_ERR_OUT_OF_RANGE ,"trusted binary hash data or size is empty" ); + + rc =is_distrusted_binary_hash (*data, data_size); +if (rc !=GRUB_ERR_NONE ) +return rc; + + rc =add_hash (data, data_size, &db.signatures, &db.signature_size, + &db.signature_entries); +return rc; +} + +static int +is_hash (const grub_uuid_t *guid) +{ +/* GUID type of the binary hash */ +if (grub_memcmp (guid, &GRUB_PKS_CERT_SHA256_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_UUID_SIZE ) ==0 ) +return GRUB_ERR_NONE ; + +/* GUID type of the certificate hash */ +if (grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA256_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA384_GUID ,GRUB_UUID_SIZE ) ==0 || +grub_memcmp (guid, &GRUB_PKS_CERT_X509_SHA512_GUID ,GRUB_UUID_SIZE ) ==0 ) +return GRUB_ERR_NONE ;
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 884
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+ +return GRUB_ERR_UNKNOWN_COMMAND ; +} + +/* +* Extract the x509 certificates/binary hashes from the platform keystore buffer, +* parse it, and add it to the trusted list. +*/ +static grub_err_t +create_trusted_list (void )
s/create_trusted_list/create_db_list/?
Quoted 32 lines
grub-core/commands/appendedsig/appendedsig.c +32
-0+{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =0 ; + +for (i =0 ; i < grub_pks_keystore.db_entries; i++) + { +if (is_hash (&grub_pks_keystore.db[i].guid) ==GRUB_ERR_NONE ) + { + rc =add_trusted_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) ==GRUB_ERR_NONE ) + { + rc =is_distrusted_cert_hash (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size); +if (rc !=GRUB_ERR_NONE ) +continue ; + + rc =add_certificate (grub_pks_keystore.db[i].data, + grub_pks_keystore.db[i].data_size, &db,1 ); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; +else if (rc !=GRUB_ERR_NONE ) +continue ; + } +else +grub_dprintf ("appendedsig" ,"unsupported signature data type and " +"skipping trusted data (%" PRIuGRUB_SIZE")\n" , i +1 ); + }
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 926
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+ +return GRUB_ERR_NONE ; +} + +/* +* Extract the certificates, certificate/binary hashes out of the platform keystore buffer, +* and add it to the distrusted list. +*/ +static grub_err_t +create_distrusted_list (void )
s/create_distrusted_list/create_dbx_list/?
Quoted 30 lines
grub-core/commands/appendedsig/appendedsig.c +30
-0+{ +grub_err_t rc =GRUB_ERR_NONE ; +grub_size_t i =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 ) + { +if (is_x509 (&grub_pks_keystore.dbx[i].guid) ==GRUB_ERR_NONE ) + { + rc =add_certificate (grub_pks_keystore.dbx[i].data, + grub_pks_keystore.dbx[i].data_size, &dbx,0 ); +if (rc ==GRUB_ERR_OUT_OF_MEMORY ) +return rc; + } +else if (is_hash (&grub_pks_keystore.dbx[i].guid) ==GRUB_ERR_NONE ) + { + 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 " +"skipping distrusted data (%" PRIuGRUB_SIZE")\n" , i +1 ); + } + }
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 966
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+ +return rc; +} + +/* +* Extract the x509 certificates from the ELF note header, +* parse it, and add it to the trusted list. +*/ +static grub_err_t +build_static_trusted_list (const struct grub_module_header *header)
s/build_static_trusted_list/build_static_db_list/?
Quoted 5 lines
grub-core/commands/appendedsig/appendedsig.c +5
-0+{ +grub_err_t err =GRUB_ERR_NONE ; +struct grub_file pseudo_file; +grub_uint8_t *cert_data =NULL ; +grub_ssize_t cert_data_size =0 ;
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 981
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-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 =grub_read_file (&pseudo_file, &cert_data, &cert_data_size);
Could you explain why cannot you use grub_file_read() here? Why do you create grub_read_file()? Even if I could imagine it can be useful I cannot accept its naming which is very confusing.
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 993
Quoted 12 lines
grub-core/commands/appendedsig/appendedsig.c +12
-0+if (err !=GRUB_ERR_NONE ) +return err; + + err =add_certificate (cert_data, cert_data_size, &db,1 ); +grub_free (cert_data); + +return err; +} + +/* releasing memory */ +static void +free_trusted_list (void )
s/free_trusted_list/free_db_list/
Quoted 13 lines
grub-core/commands/appendedsig/appendedsig.c +13
-0+{ +struct x509_certificate *cert; +grub_size_t i =0 ; + +while (db.keys !=NULL ) + { + cert = db.keys; + db.keys = db.keys->next; +certificate_release (cert); +grub_free (cert); + } + +for (i =0 ; i < db.signature_entries; i++)
grub-core/commands/appendedsig/appendedsig.c static struct grub_fs pseudo_fs = { · line 1016
Quoted 10 lines
grub-core/commands/appendedsig/appendedsig.c +10
-0+grub_free (db.signatures[i]); + +grub_free (db.signatures); +grub_free (db.signature_size); +grub_memset (&db,0 ,sizeof (db)); +} + +/* releasing memory */ +static void +free_distrusted_list (void )
s/free_distrusted_list/free_dbx_list/, etc.
Quoted 116 lines
grub-core/commands/appendedsig/appendedsig.c +62
-30+{ +struct x509_certificate *cert; +grub_size_t i =0 ; + +while (dbx.keys !=NULL ) + { + cert = dbx.keys; + dbx.keys = dbx.keys->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;@@ -555,7 +1043,6 @@ GRUB_MOD_INIT (appendedsig)if (grub_is_lockdown () ==GRUB_LOCKDOWN_ENABLED )CHECK_SIGS =CHECK_SIGS_FORCED ; - grub_trusted_key =NULL ;grub_register_variable_hook ("check_appended_signatures" , grub_env_read_sec, grub_env_write_sec);grub_env_export ("check_appended_signatures" );@@ -563,37 +1050,50 @@ 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); +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 ; -grub_dprintf ("appendedsig" ,"loaded certificate CN='%s'\n" , pk->subject); + rc =build_static_trusted_list (header); +if (rc !=GRUB_ERR_NONE ) + { +free_trusted_list (); +grub_error (rc,"static trusted list creation failed" ); + } +else +grub_dprintf ("appendedsig" ,"the trusted list now has %" PRIuGRUB_SIZE" static keys\n" , + db.key_entries); + } + } +else if (grub_pks_use_keystore &&CHECK_SIGS ==CHECK_SIGS_FORCED ) + { + rc =create_trusted_list (); +if (rc !=GRUB_ERR_NONE ) + { +free_trusted_list (); +grub_error (rc,"trusted list creation failed" ); + } +else + { + rc =create_distrusted_list (); +if (rc !=GRUB_ERR_NONE ) + { +free_trusted_list (); +free_distrusted_list (); +grub_error (rc,"distrusted list creation failed" ); + } +else +grub_dprintf ("appendedsig" ,"the trusted list now has %" PRIuGRUB_SIZE" keys.\n" +"the distrusted list now has %" PRIuGRUB_SIZE" keys.\n" , + db.signature_entries + db.key_entries, dbx.signature_entries); + } - pk->next = grub_trusted_key; - grub_trusted_key = pk; - } +grub_pks_free_keystore (); + } cmd_trust =grub_register_command ("trust_certificate" , grub_cmd_trust,N_ ("X509_CERTIFICATE" ),N_ ("Add X509_CERTIFICATE to trusted certificates" ));grub-core/kern/file.c +0
-0
grub-core/kern/file.c grub_file_seek (grub_file_t file, grub_off_t offset) · line 236
Quoted 6 lines
grub-core/kern/file.c +3
-0@@ -231,3 +231,37 @@ grub_file_seek (grub_file_t file, grub_off_t offset)return old; } + +grub_err_t +grub_read_file (const grub_file_t file,grub_uint8_t **data,grub_ssize_t *data_size)
As I said earlier, at least I do not like its name...
Quoted 21 lines
grub-core/kern/file.c +21
-0+{ +grub_uint8_t *buffer =NULL ; +grub_ssize_t read_size =0 ; +grub_off_t total_read_size =0 ; +grub_off_t file_size =grub_file_size (file); + +if (file_size ==GRUB_FILE_SIZE_UNKNOWN ) +return grub_error (GRUB_ERR_BAD_ARGUMENT , +N_ ("could not determine the size of the file" )); + + buffer =grub_zalloc (file_size); +if (buffer ==NULL ) +return grub_error (GRUB_ERR_OUT_OF_MEMORY ,N_ ("out of memory" )); + +while (total_read_size < file_size) + { + read_size =grub_file_read (file, &buffer[total_read_size], file_size - total_read_size); +if (read_size <0 ) + { +grub_free (buffer); +return grub_error (GRUB_ERR_READ_ERROR ,N_ ("unable to read the file" ));
grub-core/kern/file.c grub_file_seek (grub_file_t file, grub_off_t offset) · line 267
Quoted 10 lines
grub-core/kern/file.c +10
-0+ } + + total_read_size += read_size; + } + + *data = buffer; + *data_size = total_read_size; + +return GRUB_ERR_NONE ; +}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 20/25 appendedsig: While verifying the kernel, use trusted and distrusted lists
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
To verify the kernel's signature: verify the kernel binary against lists of binary hashes that are either distrusted or trusted. If it is not list in either trusted or distrusted hashes list then the trusted keys from the trusted key list are used to verify the signature.
--- grub-core/commands/appendedsig/appendedsig.c | 194 ++++++++++++++----- 1 file changed, 141 insertions(+), 53 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +141 -53
@@ -464,6 +464,83 @@ 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_uuid_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_UUID_SIZE ); +else if (binary_hash_size ==48 ) +grub_memcpy (&guid, &GRUB_PKS_CERT_SHA384_GUID ,GRUB_UUID_SIZE ); +else if (binary_hash_size ==64 ) +grub_memcpy (&guid, &GRUB_PKS_CERT_SHA512_GUID ,GRUB_UUID_SIZE ); +else + { +grub_dprintf ("appendedsig" ,"unsupported hash type (%" PRIuGRUB_SIZE") and skipping 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 list of binary hashes that are distrusted +* and trusted. +* The following errors can occur: +* - GRUB_ERR_BAD_SIGNATURE: indicates that the hash is distrusted. +* - GRUB_ERR_NONE: the hash is trusted, since it was found in the trusted hashes list +* - GRUB_ERR_EOF: the hash could not be found in the hashes 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) was listed as distrusted\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, the trusted key will be used from +* the trusted key list. If it fails, verify it against the list of binary hashes +* that are distrusted and trusted. +*/ static grub_err_t grub_verify_appended_signature (const grub_uint8_t *buf,grub_size_t bufsize) {
@@ -473,7 +550,7 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)unsigned char *hash;gcry_mpi_t hashmpi;gcry_err_code_t rc; -struct x509_certificate *pk; +struct x509_certificate *cert;struct grub_appended_signature sig;struct pkcs7_signerInfo *si;int i;
@@ -486,67 +563,78 @@ 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++) + err =verify_binary_hash (buf, datasize); +if (err !=GRUB_ERR_EOF && err !=GRUB_ERR_NONE ) { -/* -* 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.keys; pk; pk = pk->next) + err =grub_error (err,"failed to verify binary-hash with any trusted binary-hash\n" ); +pkcs7_signedData_release (&sig.pkcs7); +return err; + } +else if (err ==GRUB_ERR_EOF ) + { +/* Binary hash was not found in trusted and distrusted list: check signature now */ +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(db.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 (cert = db.keys; cert; cert = cert->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, cert->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); +pkcs7_signedData_release (&sig.pkcs7); +return err; + } + + rc = _gcry_pubkey_spec_rsa.verify (0 , hashmpi, &si->sig_mpi, cert->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, cert->subject); + err =GRUB_ERR_NONE ; +break ; + } + +grub_dprintf ("appendedsig" ,"verify signer %d with key '%s' failed with %d\n" , + i, cert->subject, (int ) rc); } - rc = _gcry_pubkey_spec_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 , -"failed to verify signature against a trusted key" ); - - cleanup:pkcs7_signedData_release (&sig.pkcs7); +if (err !=GRUB_ERR_NONE ) + err =grub_error (err,"failed to verify signature with any trusted key\n" ); +else +grub_dprintf ("appendedsig" ,"successfully verified the signature with a trusted key\n" ); +return err; }
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 21/25 powerpc_ieee1275: set use_static_keys flag
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
Introduce the use_static_keys flag to indicate that static keys are to be used rather than keys from the PKS storage's DB variable. This variable 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 variable to extract the default DB keys from the ELF note and store the keys found there in the trustedlist.
--- .../kern/powerpc/ieee1275/platform_keystore.c | 16 +++++++++++++++- grub-core/term/tparm.c | 1 - .../grub/powerpc/ieee1275/platform_keystore.h | 11 ++++++----- include/grub/types.h | 2 ++ 4 files changed, 23 insertions(+), 7 deletions(-)
grub-core/kern/powerpc/ieee1275/platform_keystore.c +15 -1
@@ -34,7 +34,11 @@/* Platform Keystore */ static grub_size_t pks_max_object_size;grub_uint8_t grub_pks_use_keystore =0 ; -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 *
@@ -312,6 +316,16 @@ grub_pks_keystore_init (void)grub_memset (&grub_pks_keystore,0 ,sizeof (grub_pks_t));/* DB */ rc =read_secure_boot_variables (0 ,DB , &grub_pks_keystore.db, &grub_pks_keystore.db_entries); +if (rc ==PKS_OBJECT_NOT_FOUND ) + { + rc =GRUB_ERR_NONE ; +/* +* DB variable won't be available by default in PKS. +* So, it will load the Default Keys from ELF Note +*/ + grub_pks_keystore.use_static_keys = true; + } +if (rc ==GRUB_ERR_NONE ) {/* DBX */
grub-core/term/tparm.c +0 -1
@@ -46,7 +46,6 @@/* * Common/troublesome character definitions */ -typedef char grub_bool_t ;#ifndef FALSE # define FALSE (0)#endif
include/grub/powerpc/ieee1275/platform_keystore.h +6 -5
@@ -199,10 +199,11 @@ struct grub_pks_sd/* The structure of a PKS.*/ struct grub_pks { -grub_pks_sd_t *db;/* signature database */ -grub_pks_sd_t *dbx;/* forbidden signature database */ -grub_size_t db_entries;/* size of signature database */ -grub_size_t dbx_entries;/* size of forbidden signature database */ +grub_pks_sd_t *db;/* signature database */ +grub_pks_sd_t *dbx;/* forbidden signature database */ +grub_size_t db_entries;/* size of signature database */ +grub_size_t dbx_entries;/* size of forbidden signature database */ +grub_bool_t use_static_keys;/* flag to indicate use of static keys */ }GRUB_PACKED ;#ifdef __powerpc__
@@ -226,7 +227,7 @@ grub_pks_free_keystore (void) }grub_uint8_t grub_pks_use_keystore =0 ; -grub_pks_t grub_pks_keystore = {NULL ,NULL ,0 ,0 }; +grub_pks_t grub_pks_keystore = {NULL ,NULL ,0 ,0 , false};#endif
include/grub/types.h +2 -0
@@ -397,4 +397,6 @@ struct grub_uuid grub_uint8_t b[GRUB_UUID_SIZE]; }; +typedef char grub_bool_t ; +#endif /* ! GRUB_TYPES_HEADER */
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 22/25 appendedsig: Reads the default DB keys from ELF Note
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
If Secure Boot is enabled with PKS and the use_static_keys flag is set, then read the DB default keys from the ELF note and store them in the trusted list buffer.
--- grub-core/commands/appendedsig/appendedsig.c | 59 ++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +43 -16
@@ -1051,7 +1051,7 @@ create_distrusted_list (void)* parse it, and add it to the trusted list. */ static grub_err_t -build_static_trusted_list (const struct grub_module_header *header) +build_static_trusted_list (const struct grub_module_header *header,const grub_bool_t is_pks) {grub_err_t err =GRUB_ERR_NONE ;struct grub_file pseudo_file;
@@ -1070,6 +1070,13 @@ build_static_trusted_list (const struct grub_module_header *header)if (err !=GRUB_ERR_NONE )return err; +if (is_pks == true) + { + err =is_distrusted_cert_hash (cert_data, cert_data_size); +if (err !=GRUB_ERR_NONE ) +return err; + } + err =add_certificate (cert_data, cert_data_size, &db,1 );grub_free (cert_data);
@@ -1122,6 +1129,25 @@ free_distrusted_list (void)grub_memset (&dbx,0 ,sizeof (dbx)); } +static grub_err_t +load_static_keys (const struct grub_module_header *header,const grub_bool_t 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_trusted_list (header, is_pks); +if (rc !=GRUB_ERR_NONE ) +return rc; + } + +return rc; +} +GRUB_MOD_INIT (appendedsig) {int rc;
@@ -1140,26 +1166,27 @@ 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_trusted_list (header); -if (rc !=GRUB_ERR_NONE ) - { -free_trusted_list (); -grub_error (rc,"static trusted list creation failed" ); - } -else -grub_dprintf ("appendedsig" ,"the trusted list now has %" PRIuGRUB_SIZE" static keys\n" , - db.key_entries); +free_trusted_list (); +grub_error (rc,"static trusted list creation failed" ); } +else +grub_dprintf ("appendedsig" ,"the trusted list now has %" PRIuGRUB_SIZE" static keys\n" , + db.key_entries); }else if (grub_pks_use_keystore &&CHECK_SIGS ==CHECK_SIGS_FORCED ) { - rc =create_trusted_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 trusted list\n" ); + rc =load_static_keys (header, grub_pks_keystore.use_static_keys); + } +else + rc =create_trusted_list (); +if (rc !=GRUB_ERR_NONE ) {free_trusted_list ();
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 23/25 appendedsig: The grub command's trusted and distrusted support
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
To support the following trusted and distrusted commands
1. trusted_list:
It will show the list of trusted certificates and binary hashes
2. distrusted_list:
It will show the list of distrusted certificates and binary/certificate hashes
3. trusted_certificate:
It will add the trusted certificate to the trusted list
4. trusted_signature:
It will add the certificate/binary hash to the trusted list
5. distrusted_certificate:
It will remove the trusted certificate from trsuted list
6. distrusted_signature:
It will add the certificate/binary hash to the distrsuted 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 | 545 +++++++++++++------ 1 file changed, 381 insertions(+), 164 deletions(-)
grub-core/commands/appendedsig/appendedsig.c +381 -164
@@ -117,6 +117,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.
@@ -357,72 +389,6 @@ grub_env_write_sec (struct grub_env_var *var __attribute__ ((unused)), const chareturn ret; } -static grub_err_t -file_read_all (grub_file_t file,grub_uint8_t **buf,grub_size_t *len) -{ -grub_off_t full_file_size; -grub_size_t file_size, total_read_size =0 ; -grub_ssize_t read_size; - - full_file_size =grub_file_size (file); -if (full_file_size ==GRUB_FILE_SIZE_UNKNOWN ) -return grub_error (GRUB_ERR_BAD_ARGUMENT , -"cannot read a file of unknown size into a buffer" ); - -if (full_file_size >GRUB_SIZE_MAX ) -return grub_error (GRUB_ERR_OUT_OF_RANGE , -"file is too large to read: %" PRIuGRUB_UINT64_T" bytes" , - full_file_size); - - file_size = (grub_size_t ) full_file_size; - *buf =grub_malloc (file_size); -if (*buf ==NULL ) -return grub_error (GRUB_ERR_OUT_OF_MEMORY , -"could not allocate file data buffer size %" PRIuGRUB_SIZE, - file_size); - -while (total_read_size < file_size) - { - read_size =grub_file_read (file, *buf + total_read_size, file_size - total_read_size); -if (read_size <0 ) - { -grub_free (*buf); -return grub_errno; - } -else if (read_size ==0 ) - { -grub_free (*buf); -return grub_error (GRUB_ERR_IO , -"could not read full file size " -"(%" PRIuGRUB_SIZE"), only %" PRIuGRUB_SIZE" bytes read" , - file_size, total_read_size); - } - - total_read_size += read_size; - } - - *len = file_size; - -return GRUB_ERR_NONE ; -} - -static grub_err_t -read_cert_from_file (grub_file_t f,struct x509_certificate *certificate) -{ -grub_err_t err; -grub_uint8_t *buf; -grub_size_t file_size; - - err =file_read_all (f, &buf, &file_size); -if (err !=GRUB_ERR_NONE ) -return err; - - err =parse_x509_certificate (buf, file_size, certificate); -grub_free (buf); - -return err; -} -static grub_err_t extract_appended_signature (const grub_uint8_t *buf,grub_size_t bufsize,struct grub_appended_signature *sig)
@@ -641,142 +607,374 @@ grub_verify_appended_signature (const grub_uint8_t *buf, grub_size_t bufsize)static grub_err_t grub_cmd_verify_signature (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) { -grub_file_t f;grub_err_t err =GRUB_ERR_NONE ; -grub_uint8_t *data; -grub_size_t file_size; +grub_file_t signed_file =NULL ; +grub_uint8_t *signed_data =NULL ; +grub_ssize_t signed_data_size =0 ; -if (argc <1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected" ); +if (argc !=1 ) + { +grub_printf (N_ ("a signed file is expected\n" +"Example:\n\tverify_appended <SIGNED FILE>\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + }grub_dprintf ("appendedsig" ,"verifying %s\n" , args[0 ]); - f =grub_file_open (args[0 ],GRUB_FILE_TYPE_VERIFY_SIGNATURE ); -if (f ==NULL ) + signed_file =grub_file_open (args[0 ],GRUB_FILE_TYPE_VERIFY_SIGNATURE ); +if (signed_file ==NULL ) +return grub_error (GRUB_ERR_FILE_NOT_FOUND ,"unable to open a signed file" ); + + err =grub_read_file (signed_file, &signed_data, &signed_data_size); +if (err !=GRUB_ERR_NONE ) { - err = grub_errno; - goto cleanup; +grub_file_close (signed_file); +return err; } - err =file_read_all (f, &data, &file_size); +grub_file_close (signed_file); + err =grub_verify_appended_signature (signed_data, signed_data_size); +grub_free (signed_data); + +return err; +} + +static grub_err_t +grub_cmd_trusted_list (grub_command_t cmd __attribute__((unused)), +int argc __attribute__((unused)),char **args __attribute__((unused))) +{ +struct x509_certificate *cert =NULL ; +grub_size_t i =0 , cert_num =1 ; + +for (cert = db.keys; 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_distrusted_list (grub_command_t cmd __attribute__((unused)), +int argc __attribute__((unused)), +char **args __attribute__((unused))) +{ +struct x509_certificate *cert =NULL ; +grub_size_t i =0 , cert_num =1 ; + +for (cert = dbx.keys; 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_trusted_cert (grub_command_t cmd __attribute__((unused)), +int argc,char **args) +{ +grub_err_t err =GRUB_ERR_NONE ; +grub_file_t cert_file =NULL ; +grub_uint8_t *cert_data =NULL ; +grub_ssize_t cert_data_size =0 ; + +if (argc !=1 ) + { +grub_printf (N_ ("a trusted X.509 certificate file is expected in DER format\n" +"Example:\n\ttrusted_certificate <CERT FILE>\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +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_errno; + } + +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 =grub_read_file (cert_file, &cert_data, &cert_data_size);if (err !=GRUB_ERR_NONE ) - goto cleanup; + { +grub_file_close (cert_file); +return err; + } - err =grub_verify_appended_signature (data, file_size); -grub_free (data); +grub_file_close (cert_file); + err =add_certificate (cert_data, cert_data_size, &db,1 ); +if (err !=GRUB_ERR_NONE ) + { +free_trusted_list (); +free_distrusted_list (); + err =grub_error (err,"adding of trusted certificate failed" ); + } - cleanup: -if (f !=NULL ) -grub_file_close (f); +grub_free (cert_data);return err; }static grub_err_t -grub_cmd_distrust (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +grub_cmd_trusted_hash (grub_command_t cmd __attribute__((unused)),int argc,char **args) { -unsigned long cert_num, i; -struct x509_certificate *cert, *prev; +grub_err_t rc =GRUB_ERR_NONE ; +grub_file_t hash_file =NULL ; +grub_uint8_t *hash_data =NULL ; +grub_ssize_t hash_data_size =0 ;if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected" ); - - grub_errno =GRUB_ERR_NONE ; - cert_num =grub_strtoul (args[0 ],NULL ,10 ); -if (grub_errno !=GRUB_ERR_NONE ) -return grub_error (GRUB_ERR_BAD_ARGUMENT , -"non-numeric or certificate number is invalid" ); -else if (cert_num <=0 ) -return grub_error (GRUB_ERR_OUT_OF_RANGE , -"certificate number too small - numbers start at 1" ); - -if (cert_num ==1 ) { - cert = db.keys; - db.keys = cert->next; +grub_printf (N_ ("a trusted binary hash file is expected\n" +"Example:\n\ttrusted_signature <BINARY HASH FILE>\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } -certificate_release (cert); -grub_free (cert); -return GRUB_ERR_NONE ; +if (CHECK_SIGS ==CHECK_SIGS_FORCED ) + { +grub_printf ("Warning: since secure boot is enabled, " +"adding of trusted binary hash is not permitted!\n" ); +return grub_errno; } - i =2 ; - prev = db.keys; - cert = db.keys->next; -while (cert) + +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 =grub_read_file (hash_file, &hash_data, &hash_data_size); +if (rc !=GRUB_ERR_NONE ) { -if (i == cert_num) - { - prev->next = cert->next; -certificate_release (cert); -grub_free (cert); -return GRUB_ERR_NONE ; - } - i++; - prev = cert; - cert = cert->next; +grub_file_close (hash_file); +return rc; } -return grub_error (GRUB_ERR_BAD_ARGUMENT , -N_ ("no certificate number %lu found - only %lu certificates in the store" ), - cert_num, i -1 ); +grub_file_close (hash_file); + +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_trusted_list (); +free_distrusted_list (); + rc =grub_error (rc,"adding of trusted binary hash failed" ); + } + +grub_free (hash_data); + +return rc; }static grub_err_t -grub_cmd_trust (grub_command_t cmd __attribute__ ((unused)),int argc,char **args) +grub_cmd_distrusted_cert (grub_command_t cmd __attribute__((unused)),int argc,char **args) { -grub_file_t certf; +struct x509_certificate *current_cert = db.keys; +struct x509_certificate *previous_cert =NULL ;struct x509_certificate *cert =NULL ; -grub_err_t err; +grub_file_t cert_file =NULL ; +grub_uint8_t *cert_data =NULL ; +grub_ssize_t cert_data_size =0 ; +grub_size_t i; +grub_err_t rc;if (argc !=1 ) -return grub_error (GRUB_ERR_BAD_ARGUMENT ,"one argument expected" ); + { +grub_printf (N_ ("distrusted X.509 certificate file is expected in DER format\n" +"Example:\n\tdistrusted_certificate <CERT FILE>\n" )); +return GRUB_ERR_BAD_ARGUMENT ; + } + +if (CHECK_SIGS ==CHECK_SIGS_FORCED ) + { +grub_printf ("Warning: since secure boot is enabled, " +"removing of distrusted certificate is not permitted!\n" ); +return grub_errno; + } + +if (grub_strlen (args[0 ]) ==0 ) +return grub_error (GRUB_ERR_BAD_FILENAME ,"missing distrusted X.509 certificate file" ); - 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 ,N_ ("could not open %s file" ), args[0 ]); + 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" ); + rc =grub_read_file (cert_file, &cert_data, &cert_data_size); +if (rc !=GRUB_ERR_NONE ) + { +grub_file_close (cert_file); +return rc; + } + +grub_file_close (cert_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" ); + { + rc =grub_error (GRUB_ERR_OUT_OF_MEMORY ,"out of memory" ); + goto clean; + } - err =read_cert_from_file (certf, cert); -grub_file_close (certf); -if (err !=GRUB_ERR_NONE ) + rc =parse_x509_certificate (cert_data, cert_data_size, cert); +if (rc !=GRUB_ERR_NONE ) { -grub_free (cert); -return err; + rc =grub_error (GRUB_ERR_OUT_OF_MEMORY ,"not a valid X.509 certificate" ); + goto cleanup; } -grub_dprintf ("appendedsig" ,"loaded certificate with CN: %s\n" , cert->subject); +for (i =0 ; i <= db.key_entries; i++) + { + rc =is_cert_match (current_cert, cert); +if (rc ==GRUB_ERR_NONE ) + { +if (i ==0 ) + db.keys = current_cert->next; +else + previous_cert->next = current_cert->next; + + current_cert->next =NULL ; +certificate_release (current_cert); +grub_free (current_cert); + db.key_entries--; +break ; + } +else + { + previous_cert = current_cert; + current_cert = current_cert->next; + } + } - cert->next = db.keys; - db.keys = cert; +if (rc !=GRUB_ERR_NONE ) + rc =grub_error (rc,"this X.509 certificate is not avaliable in trustedlist" ); -return GRUB_ERR_NONE ; + cleanup: +certificate_release (cert); +grub_free (cert); + + clean: +grub_free (cert_data); + +return rc; }static grub_err_t -grub_cmd_list (grub_command_t cmd __attribute__ ((unused)),int argc __attribute__ ((unused)), -char **args __attribute__ ((unused))) +grub_cmd_distrusted_hash (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_ERR_NONE ; +grub_file_t hash_file =NULL ; +grub_uint8_t *hash_data =NULL ; +grub_ssize_t hash_data_size =0 ; -for (cert = db.keys; cert; cert = cert->next) +if (argc !=2 ) { -grub_printf ("Certificate %d:\n" , cert_num); -grub_printf ("\tSerial: " ); +grub_printf (N_ ("a distrusted certificate/binary hash file is expected\n" +"Example:\n\tdistrusted_signature [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_errno; + } -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 =grub_read_file (hash_file, &hash_data, &hash_data_size); +if (rc !=GRUB_ERR_NONE ) + { +grub_file_close (hash_file); +return rc; } -return GRUB_ERR_NONE ; +grub_file_close (hash_file); + +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" ); + } + + 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_trusted_list (); +free_distrusted_list (); + rc =grub_error (rc,"adding of distrusted binary/certificate hash failed" ); + } + +grub_free (hash_data); + +return rc; } static grub_err_t
@@ -852,8 +1050,6 @@ static struct grub_fs pseudo_fs = { .fs_read = pseudo_read }; -static grub_command_t cmd_verify, cmd_list, cmd_distrust, cmd_trust; -/* * Verify the trusted certificate against the certificate hashes from platform keystore buffer's * distrusted list.
@@ -1148,6 +1344,10 @@ load_static_keys (const struct grub_module_header *header, const grub_bool_t is_return rc; } +static grub_extcmd_t cmd_distrusted_hash; +static grub_command_t cmd_verify, cmd_trusted_list, cmd_trusted_cert, cmd_trusted_hash, + cmd_distrusted_list, cmd_distrusted_cert; +GRUB_MOD_INIT (appendedsig) {int rc;
@@ -1210,16 +1410,30 @@ GRUB_MOD_INIT (appendedsig)grub_pks_free_keystore (); } - cmd_trust =grub_register_command ("trust_certificate" , grub_cmd_trust,N_ ("X509_CERTIFICATE" ), -N_ ("Add X509_CERTIFICATE to trusted certificates" )); - cmd_list =grub_register_command ("list_certificates" , grub_cmd_list,0 , -N_ ("Show the list of trusted x509 certificates" )); + cmd_trusted_cert =grub_register_command ("trusted_certificate" , grub_cmd_trusted_cert, +N_ ("X509_CERTIFICATE" ), +N_ ("Add X509_CERTIFICATE to trusted list." )); + cmd_trusted_hash =grub_register_command ("trusted_signature" , grub_cmd_trusted_hash, +N_ ("BINARY HASH FILE" ), +N_ ("Add trusted BINARY HASH to trusted list." )); + cmd_distrusted_cert =grub_register_command ("distrusted_certificate" , grub_cmd_distrusted_cert, +N_ ("X509_CERTIFICATE" ), +N_ ("Remove X509_CERTIFICATE from trusted list." )); + cmd_distrusted_hash =grub_register_extcmd ("distrusted_signature" , grub_cmd_distrusted_hash,0 , +N_ ("[-b|--binary-hash] FILE [BINARY HASH FILE]\n" +"[-c|--cert-hash] FILE [CERTFICATE HASH FILE]" ), +N_ ("Add distrusted CERTFICATE/BINARY HASH " +"to distrusted list." ), + options); + cmd_trusted_list =grub_register_command ("trusted_list" , grub_cmd_trusted_list,0 , +N_ ("Show the list of trusted x509 certificates and" +" trusted binary hashes." )); + cmd_distrusted_list =grub_register_command ("distrusted_list" , grub_cmd_distrusted_list,0 , +N_ ("Show the list of distrusted certificates and" +" certificate/binary hashes" )); cmd_verify =grub_register_command ("verify_appended" , grub_cmd_verify_signature,N_ ("FILE" ), -N_ ("Verify FILE against the trusted x509 certificates" )); - cmd_distrust =grub_register_command ("distrust_certificate" , grub_cmd_distrust, -N_ ("CERT_NUMBER" ), -N_ ("Remove CERT_NUMBER (as listed by list_certificates)" -" from trusted certificates" )); +N_ ("Verify FILE against the trusted x509 certificates/" +"trusted binary hashes." ));grub_verifier_register (&grub_appendedsig_verifier);grub_dl_set_persistent (mod);
@@ -1233,7 +1447,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_trust); -grub_unregister_command (cmd_distrust); +grub_unregister_command (cmd_trusted_list); +grub_unregister_command (cmd_distrusted_list); +grub_unregister_command (cmd_trusted_cert); +grub_unregister_command (cmd_distrusted_cert); +grub_unregister_command (cmd_trusted_hash); +grub_unregister_extcmd (cmd_distrusted_hash); }
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 24/25 appendedsig: documentation
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
This 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 | 113 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 38 deletions(-)
docs/grub.texi +75 -38
@@ -6440,7 +6440,9 @@ you forget a command, you can run the command @command{help} * date:: Display or set current date and time * devicetree:: Load a device tree blob * distrust:: Remove a pubkey from trusted keys -* distrust_certificate:: Remove a certificate from the list of trusted certificates +* distrusted_certificate:: Remove a certificate from the trusted list +* distrusted_list:: List distrusted certificates and binary/certificate hashes +* distrusted_signature:: Add a binary hash to the distrusted list * drivemap:: Map a drive to another * echo:: Display a line of text * efitextmode:: Set/Get text output mode resolution
@@ -6459,7 +6461,6 @@ you forget a command, you can run the command @command{help} * hexdump:: Show raw contents of a file or memory * insmod:: Insert a module * keystatus:: Check key modifier status -* list_certificates:: List trusted certificates * list_env:: List variables in environment block * list_trusted:: List trusted public keys * load_env:: Load variables from environment block
@@ -6501,7 +6502,9 @@ you forget a command, you can run the command @command{help} * tpm2_dump_pcr:: Dump TPM2 PCRs * true:: Do nothing, successfully * trust:: Add public key to list of trusted keys -* trust_certificate:: Add an x509 certificate to the list of trusted certificates +* trusted_certificate:: Add an x509 certificate to the trusted list +* trusted_list:: List trusted certificates and binary hashes +* trusted_signature:: Add a binary hash to the trusted list. * unset:: Unset an environment variable @comment * vbeinfo:: List available video modes * verify_appended:: Verify appended digital signature
@@ -6866,21 +6869,37 @@ These keys are used to validate signatures when environment variable GPG-style digital signatures}, for more information. @end deffn -@node distrust_certificate -@subsection distrust_certificate +@node distrusted_certificate +@subsection distrusted_certificate -@deffn Command distrust_certificate cert_number -Remove the x509 certificate numbered @var{cert_number} from GRUB's keyring of +@deffn Command distrusted_certificate x509_certificate +Read a DER-formatted x509 certificate from the file @var{x509_certificate} +and remove this x509 certificate from GRUB's keyring of trusted x509 certificates for verifying appended signatures. -@var{cert_number} is the certificate number as listed by -@command{list_certificates} (@pxref{list_certificates}). +See @xref{Using appended signatures} for more information. +@end deffn -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{verify_appended} -(@pxref{verify_appended}). See @xref{Using appended signatures} for more -information. +@node distrusted_list +@subsection distrusted_list + +@deffn Command distrusted_list +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. +@end deffn + +@node distrusted_signature +@subsection distrusted_signature + +@deffn Command distrusted_signature +Read a binary hash from the file @var{binary hash file} +and add it to GRUB's internal distrusted list. These hash are used to +restrict validation of linux image integrity using trusted 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 drivemap
@@ -7276,20 +7295,6 @@ without any options, the @command{keystatus} command returns true if and only if checking key modifier status is supported. @end deffn -@node list_certificates -@subsection list_certificates - -@deffn Command list_certificates -List all x509 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{distrust_certificate} (@pxref{distrust_certificate}). - -See @xref{Using appended signatures} for more information. -@end deffn - @node list_env @subsection list_env
@@ -8214,17 +8219,17 @@ information. @end deffn -@node trust_certificate -@subsection trust_certificate +@node trusted_certificate +@subsection trusted_certificate -@deffn Command trust_certificate x509_certificate +@deffn Command trusted_certificate x509_certificate Read a DER-formatted x509 certificate from the file @var{x509_certificate} and add it to GRUB's internal list of trusted x509 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{trust_certificate} is executed, then @var{x509_certificate} +when @command{trusted_certificate} 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 x509 rules: GRUB does not include support for validating signatures within x509
@@ -8233,6 +8238,30 @@ certificates themselves.) See @xref{Using appended signatures} for more information. @end deffn +@node trusted_list +@subsection trusted_list + +@deffn Command trusted_list +List all x509 certificates and binary hases 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 trusted_signature +@subsection trusted_signature + +@deffn Command trust_signature +Read a binary hash from the file @var{binary hash file} +and add it to GRUB's internal trusted list. These binary 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 unset @subsection unset
@@ -8256,8 +8285,8 @@ only on PC BIOS platforms. @deffn Command verify_appended file Verifies an appended signature on @var{file} against the trusted certificates -known to GRUB (See @pxref{list_certificates}, @pxref{trust_certificate}, and -@pxref{distrust_certificate}). +known to GRUB (See @pxref{trusted_list}, @pxref{trusted_certificate}, and +@pxref{distrusted_certificate}). Exit code @code{$?} is set to 0 if the signature validates successfully. If validation fails, it is set to a non-zero value.
@@ -8927,10 +8956,18 @@ 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{trust_certificate}, -@pxref{distrust_certificate} and @pxref{list_certificates} 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, Certificates will be built in to the core image using +the @code{--x509} parameter to @command{grub-install} or @command{grub-mkimage}. +it can allow to list the trusted certificates and binary hashes at boot time using +@pxref{trusted_list} and list distrusted certificates and binary/certificate hashes +at boot time using @pxref{distrusted_list} commands. + +For dynamic key, loads the signature database (DB) and forbidden +signature database (DBX) from platform keystore (PKS) and it can allow to list +the trusted certificates and binary hashes at boot time using @pxref{trusted_list} +and list distrusted certificates and binary/certificate hashes at boot time using +@pxref{distrusted_list} commands. + A file can be explicitly verified using the @pxref{verify_appended} command. Only signatures made with the SHA-256 or SHA-512 hash algorithm are supported,
Signature · 7 lines
2.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH v3 25/25 appendedsig: correcting the grub commands in appended signature tests
In reply to SudhakarFrom: Sudhakar Kuppusamy <[email protected]>
changed the following grub commands:
- "trust_certificate" to "trusted_certificate"
- "distrust_certificate" to "distrusted_certificate"
--- grub-core/tests/appended_signature_test.c | 30 +++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-)
grub-core/tests/appended_signature_test.c +14 -16
@@ -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);
@@ -113,10 +111,10 @@ appended_signature_test (void)grub_procfs_register ("certificate_printable.der" , &certificate_printable_der_entry);grub_procfs_register ("certificate_eku.der" , &certificate_eku_der_entry); - cmd_trust =grub_command_find ("trust_certificate" ); + cmd_trust =grub_command_find ("trusted_certificate" );if (cmd_trust ==NULL ) { -grub_test_assert (0 ,"can't find command `%s'" ,"trust_certificate" ); +grub_test_assert (0 ,"can't find command `%s'" ,"trusted_certificate" );return ; }
@@ -136,7 +134,7 @@ appended_signature_test (void)* releases some internal storage. This means it's not safe to call a second * time and we need to reload it. */ - cmd_trust =grub_command_find ("trust_certificate" ); + cmd_trust =grub_command_find ("trusted_certificate" );/* hi, signed with key 1, SHA-512 */ DO_TEST (hi_signed,1 );
@@ -189,16 +187,16 @@ 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 list, which is -* certificate #1, giving us just [#2]. +* certificate #1 */ - cmd_distrust =grub_command_find ("distrust_certificate" ); + cmd_distrust =grub_command_find ("distrusted_certificate" );if (cmd_distrust ==NULL ) { -grub_test_assert (0 ,"can't find command `%s'" ,"distrust_certificate" ); +grub_test_assert (0 ,"can't find command `%s'" ,"distrusted_certificate" );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 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 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 list. +* Remove the certificate#1 again, giving an empty list. * verify_appended 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.49.0 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Quoted 3 lines · Sudhakar
This patch set contains v3 of the consolidated version of the patch sets for secure boot using appended signatures on powerpc, rebased on top of git HEAD.
Your patch set breaks at least this target/platform pair build:
./configure --target=i386 --with-platform=ieee1275 ... make
kern/ieee1275/init.c: In function ‘grub_get_ieee1275_secure_boot’:
kern/ieee1275/init.c:1034:13: error: implicit declaration of function ‘grub_pks_keystore_init’ [-Werror=implicit-function-declaration]
err = grub_pks_keystore_init ();
^~~~~~~~~~~~~~~~~~~~~~
kern/ieee1275/init.c:1034:7: error: nested extern declaration of ‘grub_pks_keystore_init’ [-Werror=nested-externs]
err = grub_pks_keystore_init ();
^~~cc1: all warnings being treated as errors
Please fix this issue.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Hi Daniel,
Thank you. I fixed it on v4 and also addressed all your review comments in v4.
Thanks,
Sudhakar
Quoted 19 lines · Daniel Kiper
Quoted 3 lines · Sudhakar
This patch set contains v3 of the consolidated version of the patch sets for secure boot using appended signatures on powerpc, rebased on top of git HEAD.
Your patch set breaks at least this target/platform pair build:
./configure --target=i386 --with-platform=ieee1275 ... make
kern/ieee1275/init.c: In function ‘grub_get_ieee1275_secure_boot’:
kern/ieee1275/init.c:1034:13: error: implicit declaration of function ‘grub_pks_keystore_init’ [-Werror=implicit-function-declaration]err = grub_pks_keystore_init (); ^~~~~~~~~~~~~~~~~~~~~~ kern/ieee1275/init.c:1034:7: error: nested extern declaration of ‘grub_pks_keystore_init’ [-Werror=nested-externs] err = grub_pks_keystore_init (); ^~~
Quoted 5 lines
cc1: all warnings being treated as errors
Please fix this issue.
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel