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 1/2 appendedsig: The grub command's trusted and distrusted support
From: 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