b141171
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b141171
From: Daniel Axtens <dja@axtens.net>
b141171
Date: Thu, 30 Jul 2020 01:35:43 +1000
b141171
Subject: [PATCH] appended signatures: support verifying appended signatures
b141171
b141171
Building on the parsers and the ability to embed x509 certificates, as
b141171
well as the existing gcrypt functionality, add a module for verifying
b141171
appended signatures.
b141171
46968b6
This includes a verifier that requires that Linux kernels and grub modules
46968b6
have appended signatures, and commands to manage the list of trusted
46968b6
certificates for verification.
b141171
46968b6
Verification must be enabled by setting check_appended_signatures. If
46968b6
GRUB is locked down when the module is loaded, verification will be
46968b6
enabled and locked automatically.
b141171
46968b6
As with the PGP verifier, it is not a complete secure-boot solution:
46968b6
other mechanisms, such as a password or lockdown, must be used to ensure
46968b6
that a user cannot drop to the grub shell and disable verification.
b141171
b141171
Signed-off-by: Daniel Axtens <dja@axtens.net>
ac0e146
[pjones: fix missing format specifier]
ac0e146
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
b141171
---
b141171
 grub-core/Makefile.core.def                  |  12 +
46968b6
 grub-core/commands/appendedsig/appendedsig.c | 645 +++++++++++++++++++++++++++
b141171
 include/grub/file.h                          |   2 +
46968b6
 3 files changed, 659 insertions(+)
b141171
 create mode 100644 grub-core/commands/appendedsig/appendedsig.c
b141171
b141171
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
ed1787d
index a32e6ada59..6404384d90 100644
b141171
--- a/grub-core/Makefile.core.def
b141171
+++ b/grub-core/Makefile.core.def
46968b6
@@ -980,6 +980,18 @@ module = {
b141171
   cppflags = '-I$(srcdir)/lib/posix_wrap';
b141171
 };
b141171
 
b141171
+module = {
b141171
+  name = appendedsig;
b141171
+  common = commands/appendedsig/appendedsig.c;
b141171
+  common = commands/appendedsig/x509.c;
b141171
+  common = commands/appendedsig/pkcs7.c;
b141171
+  common = commands/appendedsig/asn1util.c;
b141171
+  common = commands/appendedsig/gnutls_asn1_tab.c;
b141171
+  common = commands/appendedsig/pkix_asn1_tab.c;
b141171
+  cflags = '$(CFLAGS_POSIX)';
b141171
+  cppflags = '-I$(srcdir)/lib/posix_wrap';
b141171
+};
b141171
+
b141171
 module = {
46968b6
   name = hdparm;
46968b6
   common = commands/hdparm.c;
b141171
diff --git a/grub-core/commands/appendedsig/appendedsig.c b/grub-core/commands/appendedsig/appendedsig.c
b141171
new file mode 100644
e622855
index 0000000000..bf8b18b620
b141171
--- /dev/null
b141171
+++ b/grub-core/commands/appendedsig/appendedsig.c
46968b6
@@ -0,0 +1,645 @@
b141171
+/*
b141171
+ *  GRUB  --  GRand Unified Bootloader
46968b6
+ *  Copyright (C) 2020-2021  IBM Corporation.
b141171
+ *
b141171
+ *  GRUB is free software: you can redistribute it and/or modify
b141171
+ *  it under the terms of the GNU General Public License as published by
b141171
+ *  the Free Software Foundation, either version 3 of the License, or
b141171
+ *  (at your option) any later version.
b141171
+ *
b141171
+ *  GRUB is distributed in the hope that it will be useful,
b141171
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
b141171
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b141171
+ *  GNU General Public License for more details.
b141171
+ *
b141171
+ *  You should have received a copy of the GNU General Public License
b141171
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
b141171
+ */
b141171
+
b141171
+#include <grub/types.h>
b141171
+#include <grub/misc.h>
b141171
+#include <grub/mm.h>
b141171
+#include <grub/err.h>
b141171
+#include <grub/dl.h>
b141171
+#include <grub/file.h>
b141171
+#include <grub/command.h>
b141171
+#include <grub/crypto.h>
b141171
+#include <grub/pkcs1_v15.h>
b141171
+#include <grub/i18n.h>
b141171
+#include <grub/gcrypt/gcrypt.h>
b141171
+#include <grub/kernel.h>
b141171
+#include <grub/extcmd.h>
b141171
+#include <grub/verify.h>
b141171
+#include <grub/libtasn1.h>
b141171
+#include <grub/env.h>
46968b6
+#include <grub/lockdown.h>
b141171
+
b141171
+#include "appendedsig.h"
b141171
+
b141171
+GRUB_MOD_LICENSE ("GPLv3+");
b141171
+
b141171
+const char magic[] = "~Module signature appended~\n";
b141171
+
b141171
+/*
b141171
+ * This structure is extracted from scripts/sign-file.c in the linux kernel
b141171
+ * source. It was licensed as LGPLv2.1+, which is GPLv3+ compatible.
b141171
+ */
b141171
+struct module_signature
b141171
+{
b141171
+  grub_uint8_t algo;		/* Public-key crypto algorithm [0] */
b141171
+  grub_uint8_t hash;		/* Digest algorithm [0] */
b141171
+  grub_uint8_t id_type;		/* Key identifier type [PKEY_ID_PKCS7] */
b141171
+  grub_uint8_t signer_len;	/* Length of signer's name [0] */
b141171
+  grub_uint8_t key_id_len;	/* Length of key identifier [0] */
b141171
+  grub_uint8_t __pad[3];
b141171
+  grub_uint32_t sig_len;	/* Length of signature data */
b141171
+} GRUB_PACKED;
b141171
+
b141171
+
b141171
+/* This represents an entire, parsed, appended signature */
b141171
+struct grub_appended_signature
b141171
+{
b141171
+  grub_size_t signature_len;		/* Length of PKCS#7 data +
b141171
+                                         * metadata + magic */
b141171
+
b141171
+  struct module_signature sig_metadata;	/* Module signature metadata */
b141171
+  struct pkcs7_signedData pkcs7;	/* Parsed PKCS#7 data */
b141171
+};
b141171
+
b141171
+/* Trusted certificates for verifying appended signatures */
b141171
+struct x509_certificate *grub_trusted_key;
b141171
+
b141171
+/*
b141171
+ * Force gcry_rsa to be a module dependency.
b141171
+ *
b141171
+ * If we use grub_crypto_pk_rsa, then then the gcry_rsa module won't be built
b141171
+ * in if you add 'appendedsig' to grub-install --modules. You would need to
b141171
+ * add 'gcry_rsa' too. That's confusing and seems suboptimal, especially when
b141171
+ * we only support RSA.
b141171
+ *
b141171
+ * Dynamic loading also causes some concerns. We can't load gcry_rsa from the
b141171
+ * the filesystem after we install the verifier - we won't be able to verify
b141171
+ * it without having it already present. We also shouldn't load it before we
b141171
+ * install the verifier, because that would mean it wouldn't be verified - an
b141171
+ * attacker could insert any code they wanted into the module.
b141171
+ *
b141171
+ * So instead, reference the internal symbol from gcry_rsa. That creates a
b141171
+ * direct dependency on gcry_rsa, so it will be built in when this module
b141171
+ * is built in. Being built in (assuming the core image is itself signed!)
b141171
+ * also resolves our concerns about loading from the filesystem.
b141171
+ */
b141171
+extern gcry_pk_spec_t _gcry_pubkey_spec_rsa;
b141171
+
b141171
+static int check_sigs = 0;
b141171
+
46968b6
+static const char *
46968b6
+grub_env_read_sec (struct grub_env_var *var __attribute__ ((unused)),
46968b6
+                   const char *val __attribute__ ((unused)))
46968b6
+{
46968b6
+  if (check_sigs == 2)
46968b6
+    return "forced";
46968b6
+  else if (check_sigs == 1)
46968b6
+    return "enforce";
46968b6
+  else
46968b6
+    return "no";
46968b6
+}
46968b6
+
b141171
+static char *
b141171
+grub_env_write_sec (struct grub_env_var *var __attribute__((unused)),
b141171
+		    const char *val)
b141171
+{
46968b6
+  /* Do not allow the value to be changed if set to forced */
46968b6
+  if (check_sigs == 2)
46968b6
+    return grub_strdup ("forced");
46968b6
+
46968b6
+  if ((*val == '2') || (*val == 'f'))
46968b6
+    check_sigs = 2;
46968b6
+  else if ((*val == '1') || (*val == 'e'))
46968b6
+    check_sigs = 1;
46968b6
+  else if ((*val == '0') || (*val == 'n'))
46968b6
+    check_sigs = 0;
46968b6
+
46968b6
+  return grub_strdup (grub_env_read_sec (NULL, NULL));
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+read_cert_from_file (grub_file_t f, struct x509_certificate *certificate)
b141171
+{
b141171
+  grub_err_t err;
b141171
+  grub_uint8_t *buf = NULL;
b141171
+  grub_ssize_t read_size;
b141171
+  grub_off_t total_read_size = 0;
b141171
+  grub_off_t file_size = grub_file_size (f);
b141171
+
b141171
+
b141171
+  if (file_size == GRUB_FILE_SIZE_UNKNOWN)
b141171
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
b141171
+		       N_("Cannot parse a certificate file of unknown size"));
b141171
+
b141171
+  buf = grub_zalloc (file_size);
b141171
+  if (!buf)
b141171
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
b141171
+		       N_("Could not allocate buffer for certificate file contents"));
b141171
+
b141171
+  while (total_read_size < file_size)
b141171
+    {
b141171
+      read_size =
b141171
+	grub_file_read (f, &buf[total_read_size],
b141171
+			file_size - total_read_size);
b141171
+      if (read_size < 0)
b141171
+	{
b141171
+	  err = grub_error (GRUB_ERR_READ_ERROR,
b141171
+			    N_("Error reading certificate file"));
b141171
+	  goto cleanup_buf;
b141171
+	}
b141171
+      total_read_size += read_size;
b141171
+    }
b141171
+
b141171
+  err = certificate_import (buf, total_read_size, certificate);
b141171
+  if (err != GRUB_ERR_NONE)
b141171
+    goto cleanup_buf;
b141171
+
b141171
+  return GRUB_ERR_NONE;
b141171
+
b141171
+cleanup_buf:
b141171
+  grub_free (buf);
b141171
+  return err;
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+extract_appended_signature (grub_uint8_t * buf, grub_size_t bufsize,
b141171
+			    struct grub_appended_signature *sig)
b141171
+{
b141171
+  grub_err_t err;
b141171
+  grub_size_t pkcs7_size;
b141171
+  grub_size_t remaining_len;
b141171
+  grub_uint8_t *appsigdata = buf + bufsize - grub_strlen (magic);
b141171
+
b141171
+  if (bufsize < grub_strlen (magic))
b141171
+    return grub_error (GRUB_ERR_BAD_SIGNATURE,
b141171
+		       N_("File too short for signature magic"));
b141171
+
b141171
+  if (grub_memcmp (appsigdata, (grub_uint8_t *) magic, grub_strlen (magic)))
b141171
+    return grub_error (GRUB_ERR_BAD_SIGNATURE,
b141171
+		       N_("Missing or invalid signature magic"));
b141171
+
b141171
+  remaining_len = bufsize - grub_strlen (magic);
b141171
+
b141171
+  if (remaining_len < sizeof (struct module_signature))
b141171
+    return grub_error (GRUB_ERR_BAD_SIGNATURE,
b141171
+		       N_("File too short for signature metadata"));
b141171
+
b141171
+  appsigdata -= sizeof (struct module_signature);
b141171
+
b141171
+  /* extract the metadata */
b141171
+  grub_memcpy (&(sig->sig_metadata), appsigdata,
b141171
+	       sizeof (struct module_signature));
b141171
+
b141171
+  remaining_len -= sizeof (struct module_signature);
b141171
+
b141171
+  if (sig->sig_metadata.id_type != 2)
b141171
+    return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("Wrong signature type"));
b141171
+
b141171
+#ifdef GRUB_TARGET_WORDS_BIGENDIAN
b141171
+  pkcs7_size = sig->sig_metadata.sig_len;
b141171
+#else
b141171
+  pkcs7_size = __builtin_bswap32 (sig->sig_metadata.sig_len);
b141171
+#endif
b141171
+
b141171
+  if (pkcs7_size > remaining_len)
b141171
+    return grub_error (GRUB_ERR_BAD_SIGNATURE,
b141171
+		       N_("File too short for PKCS#7 message"));
b141171
+
b141171
+  grub_dprintf ("appendedsig", "sig len %" PRIuGRUB_SIZE "\n", pkcs7_size);
b141171
+
b141171
+  sig->signature_len =
b141171
+    grub_strlen (magic) + sizeof (struct module_signature) + pkcs7_size;
b141171
+
b141171
+  /* rewind pointer and parse pkcs7 data */
b141171
+  appsigdata -= pkcs7_size;
b141171
+
b141171
+  err = parse_pkcs7_signedData (appsigdata, pkcs7_size, &sig->pkcs7);
b141171
+  if (err != GRUB_ERR_NONE)
b141171
+    return err;
b141171
+
b141171
+  return GRUB_ERR_NONE;
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+grub_verify_appended_signature (grub_uint8_t * buf, grub_size_t bufsize)
b141171
+{
b141171
+  grub_err_t err = GRUB_ERR_NONE;
b141171
+  grub_size_t datasize;
b141171
+  void *context;
b141171
+  unsigned char *hash;
b141171
+  gcry_mpi_t hashmpi;
b141171
+  gcry_err_code_t rc;
b141171
+  struct x509_certificate *pk;
b141171
+  struct grub_appended_signature sig;
b141171
+
b141171
+  if (!grub_trusted_key)
b141171
+    return grub_error (GRUB_ERR_BAD_SIGNATURE,
b141171
+		       N_("No trusted keys to verify against"));
b141171
+
b141171
+  err = extract_appended_signature (buf, bufsize, &sig);
b141171
+  if (err != GRUB_ERR_NONE)
b141171
+    return err;
b141171
+
b141171
+  datasize = bufsize - sig.signature_len;
b141171
+
b141171
+  context = grub_zalloc (sig.pkcs7.hash->contextsize);
b141171
+  if (!context)
b141171
+    return grub_errno;
b141171
+
b141171
+  sig.pkcs7.hash->init (context);
b141171
+  sig.pkcs7.hash->write (context, buf, datasize);
b141171
+  sig.pkcs7.hash->final (context);
b141171
+  hash = sig.pkcs7.hash->read (context);
b141171
+  grub_dprintf ("appendedsig",
b141171
+		"data size %" PRIxGRUB_SIZE ", hash %02x%02x%02x%02x...\n",
b141171
+		datasize, hash[0], hash[1], hash[2], hash[3]);
b141171
+
b141171
+  err = GRUB_ERR_BAD_SIGNATURE;
b141171
+  for (pk = grub_trusted_key; pk; pk = pk->next)
b141171
+    {
b141171
+      rc = grub_crypto_rsa_pad (&hashmpi, hash, sig.pkcs7.hash, pk->mpis[0]);
b141171
+      if (rc)
b141171
+	{
b141171
+	  err = grub_error (GRUB_ERR_BAD_SIGNATURE,
b141171
+			    N_("Error padding hash for RSA verification: %d"),
b141171
+			    rc);
b141171
+	  goto cleanup;
b141171
+	}
b141171
+
b141171
+      rc = _gcry_pubkey_spec_rsa.verify (0, hashmpi, &sig.pkcs7.sig_mpi,
b141171
+					 pk->mpis, NULL, NULL);
b141171
+      gcry_mpi_release (hashmpi);
b141171
+
b141171
+      if (rc == 0)
b141171
+	{
b141171
+	  grub_dprintf ("appendedsig", "verify with key '%s' succeeded\n",
b141171
+			pk->subject);
b141171
+	  err = GRUB_ERR_NONE;
b141171
+	  break;
b141171
+	}
b141171
+
b141171
+      grub_dprintf ("appendedsig", "verify with key '%s' failed with %d\n",
b141171
+		    pk->subject, rc);
b141171
+    }
b141171
+
b141171
+  /* If we didn't verify, provide a neat message */
b141171
+  if (err != GRUB_ERR_NONE)
b141171
+      err = grub_error (GRUB_ERR_BAD_SIGNATURE,
b141171
+			N_("Failed to verify signature against a trusted key"));
b141171
+
b141171
+cleanup:
b141171
+  grub_free (context);
b141171
+  pkcs7_signedData_release (&sig.pkcs7);
b141171
+
b141171
+  return err;
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+grub_cmd_verify_signature (grub_command_t cmd __attribute__((unused)),
b141171
+			   int argc, char **args)
b141171
+{
b141171
+  grub_file_t f;
b141171
+  grub_err_t err = GRUB_ERR_NONE;
b141171
+  grub_uint8_t *data;
b141171
+  grub_ssize_t read_size;
b141171
+  grub_off_t file_size, total_read_size = 0;
b141171
+
b141171
+  if (argc < 1)
b141171
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
b141171
+
b141171
+  grub_dprintf ("appendedsig", "verifying %s\n", args[0]);
b141171
+
b141171
+  f = grub_file_open (args[0], GRUB_FILE_TYPE_VERIFY_SIGNATURE);
b141171
+  if (!f)
b141171
+    {
b141171
+      err = grub_errno;
b141171
+      goto cleanup;
b141171
+    }
b141171
+
b141171
+  file_size = grub_file_size (f);
b141171
+  if (file_size == GRUB_FILE_SIZE_UNKNOWN)
b141171
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
b141171
+		       N_("Cannot verify the signature of a file of unknown size"));
b141171
+
b141171
+  data = grub_malloc (file_size);
b141171
+  if (!data)
b141171
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
ac0e146
+		       N_("Could not allocate data buffer size %"
b141171
+		       PRIuGRUB_UINT64_T " for verification"), file_size);
b141171
+
b141171
+  while (total_read_size < file_size)
b141171
+    {
b141171
+      read_size =
b141171
+	grub_file_read (f, &data[total_read_size],
b141171
+			file_size - total_read_size);
b141171
+      if (read_size < 0)
b141171
+	{
b141171
+	  err = grub_error (GRUB_ERR_READ_ERROR,
b141171
+			    N_("Error reading file to verify"));
b141171
+	  goto cleanup_data;
b141171
+	}
b141171
+      total_read_size += read_size;
b141171
+    }
b141171
+
b141171
+  err = grub_verify_appended_signature (data, file_size);
b141171
+
b141171
+cleanup_data:
b141171
+  grub_free (data);
b141171
+cleanup:
b141171
+  if (f)
b141171
+    grub_file_close (f);
b141171
+  return err;
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+grub_cmd_distrust (grub_command_t cmd __attribute__((unused)),
b141171
+		   int argc, char **args)
b141171
+{
b141171
+  unsigned long cert_num, i;
b141171
+  struct x509_certificate *cert, *prev;
b141171
+
b141171
+  if (argc != 1)
b141171
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("One argument expected"));
b141171
+
b141171
+  grub_errno = GRUB_ERR_NONE;
b141171
+  cert_num = grub_strtoul (args[0], NULL, 10);
b141171
+  if (grub_errno != GRUB_ERR_NONE)
b141171
+    return grub_errno;
b141171
+
b141171
+  if (cert_num < 1)
b141171
+    return grub_error (GRUB_ERR_BAD_ARGUMENT,
b141171
+		       N_("Certificate number too small - numbers start at 1"));
b141171
+
b141171
+  if (cert_num == 1)
b141171
+    {
b141171
+      cert = grub_trusted_key;
b141171
+      grub_trusted_key = cert->next;
b141171
+
b141171
+      certificate_release (cert);
b141171
+      grub_free (cert);
b141171
+      return GRUB_ERR_NONE;
b141171
+    }
b141171
+  i = 2;
b141171
+  prev = grub_trusted_key;
b141171
+  cert = grub_trusted_key->next;
b141171
+  while (cert)
b141171
+    {
b141171
+      if (i == cert_num)
b141171
+	{
b141171
+	  prev->next = cert->next;
b141171
+	  certificate_release (cert);
b141171
+	  grub_free (cert);
b141171
+	  return GRUB_ERR_NONE;
b141171
+	}
b141171
+      i++;
b141171
+      prev = cert;
b141171
+      cert = cert->next;
b141171
+    }
b141171
+
b141171
+  return grub_error (GRUB_ERR_BAD_ARGUMENT,
b141171
+		     N_("No certificate number %d found - only %d certificates in the store"),
b141171
+		     cert_num, i - 1);
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+grub_cmd_trust (grub_command_t cmd __attribute__((unused)),
b141171
+		int argc, char **args)
b141171
+{
b141171
+  grub_file_t certf;
b141171
+  struct x509_certificate *cert = NULL;
b141171
+  grub_err_t err;
b141171
+
b141171
+  if (argc != 1)
b141171
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
b141171
+
b141171
+  certf = grub_file_open (args[0],
b141171
+			  GRUB_FILE_TYPE_CERTIFICATE_TRUST
b141171
+			  | GRUB_FILE_TYPE_NO_DECOMPRESS);
b141171
+  if (!certf)
b141171
+    return grub_errno;
b141171
+
b141171
+
b141171
+  cert = grub_zalloc (sizeof (struct x509_certificate));
b141171
+  if (!cert)
b141171
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
b141171
+		       N_("Could not allocate memory for certificate"));
b141171
+
b141171
+  err = read_cert_from_file (certf, cert);
b141171
+  grub_file_close (certf);
b141171
+  if (err != GRUB_ERR_NONE)
b141171
+    {
b141171
+      grub_free (cert);
b141171
+      return err;
b141171
+    }
b141171
+  grub_dprintf ("appendedsig", "Loaded certificate with CN: %s\n",
b141171
+		cert->subject);
b141171
+
b141171
+  cert->next = grub_trusted_key;
b141171
+  grub_trusted_key = cert;
b141171
+
b141171
+  return GRUB_ERR_NONE;
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+grub_cmd_list (grub_command_t cmd __attribute__((unused)),
b141171
+	       int argc __attribute__((unused)),
b141171
+	       char **args __attribute__((unused)))
b141171
+{
b141171
+  struct x509_certificate *cert;
b141171
+  int cert_num = 1;
b141171
+  grub_size_t i;
b141171
+
b141171
+  for (cert = grub_trusted_key; cert; cert = cert->next)
b141171
+    {
b141171
+      grub_printf (N_("Certificate %d:\n"), cert_num);
b141171
+
b141171
+      grub_printf (N_("\tSerial: "));
b141171
+      for (i = 0; i < cert->serial_len - 1; i++)
b141171
+	{
b141171
+	  grub_printf ("%02x:", cert->serial[i]);
b141171
+	}
b141171
+      grub_printf ("%02x\n", cert->serial[cert->serial_len - 1]);
b141171
+
b141171
+      grub_printf ("\tCN: %s\n\n", cert->subject);
b141171
+      cert_num++;
b141171
+
b141171
+    }
b141171
+
b141171
+  return GRUB_ERR_NONE;
b141171
+}
b141171
+
b141171
+static grub_err_t
46968b6
+appendedsig_init (grub_file_t io __attribute__((unused)),
46968b6
+		  enum grub_file_type type,
b141171
+		  void **context __attribute__((unused)),
b141171
+		  enum grub_verify_flags *flags)
b141171
+{
b141171
+  if (!check_sigs)
b141171
+    {
b141171
+      *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
b141171
+      return GRUB_ERR_NONE;
b141171
+    }
b141171
+
b141171
+  switch (type & GRUB_FILE_TYPE_MASK)
b141171
+    {
b141171
+    case GRUB_FILE_TYPE_CERTIFICATE_TRUST:
b141171
+      /*
b141171
+       * This is a certificate to add to trusted keychain.
b141171
+       *
b141171
+       * This needs to be verified or blocked. Ideally we'd write an x509
b141171
+       * verifier, but we lack the hubris required to take this on. Instead,
b141171
+       * require that it have an appended signature.
b141171
+       */
b141171
+
b141171
+      /* Fall through */
b141171
+
b141171
+    case GRUB_FILE_TYPE_LINUX_KERNEL:
46968b6
+    case GRUB_FILE_TYPE_GRUB_MODULE:
46968b6
+      /*
46968b6
+       * Appended signatures are only defined for ELF binaries.
46968b6
+       * Out of an abundance of caution, we only verify Linux kernels and
46968b6
+       * GRUB modules at this point.
46968b6
+       */
b141171
+      *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK;
b141171
+      return GRUB_ERR_NONE;
b141171
+
46968b6
+    case GRUB_FILE_TYPE_ACPI_TABLE:
46968b6
+    case GRUB_FILE_TYPE_DEVICE_TREE_IMAGE:
b141171
+      /*
46968b6
+       * It is possible to use appended signature verification without
46968b6
+       * lockdown - like the PGP verifier. When combined with an embedded
46968b6
+       * config file in a signed grub binary, this could still be a meaningful
46968b6
+       * secure-boot chain - so long as it isn't subverted by something like a
46968b6
+       * rouge ACPI table or DT image. Defer them explicitly.
b141171
+       */
46968b6
+      *flags = GRUB_VERIFY_FLAGS_DEFER_AUTH;
46968b6
+      return GRUB_ERR_NONE;
46968b6
+
46968b6
+    default:
b141171
+      *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
b141171
+      return GRUB_ERR_NONE;
b141171
+    }
b141171
+}
b141171
+
b141171
+static grub_err_t
b141171
+appendedsig_write (void *ctxt __attribute__((unused)),
b141171
+		   void *buf, grub_size_t size)
b141171
+{
b141171
+  return grub_verify_appended_signature (buf, size);
b141171
+}
b141171
+
b141171
+struct grub_file_verifier grub_appendedsig_verifier = {
b141171
+  .name = "appendedsig",
b141171
+  .init = appendedsig_init,
b141171
+  .write = appendedsig_write,
b141171
+};
b141171
+
b141171
+static grub_ssize_t
b141171
+pseudo_read (struct grub_file *file, char *buf, grub_size_t len)
b141171
+{
b141171
+  grub_memcpy (buf, (grub_uint8_t *) file->data + file->offset, len);
b141171
+  return len;
b141171
+}
b141171
+
b141171
+/* Filesystem descriptor.  */
b141171
+static struct grub_fs pseudo_fs = {
b141171
+  .name = "pseudo",
b141171
+  .fs_read = pseudo_read
b141171
+};
b141171
+
b141171
+static grub_command_t cmd_verify, cmd_list, cmd_distrust, cmd_trust;
b141171
+
b141171
+GRUB_MOD_INIT (appendedsig)
b141171
+{
b141171
+  int rc;
b141171
+  struct grub_module_header *header;
b141171
+
46968b6
+  /* If in lockdown, immediately enter forced mode */
46968b6
+  if (grub_is_lockdown () == GRUB_LOCKDOWN_ENABLED)
46968b6
+    check_sigs = 2;
b141171
+
b141171
+  grub_trusted_key = NULL;
b141171
+
46968b6
+  grub_register_variable_hook ("check_appended_signatures",
46968b6
+  			       grub_env_read_sec,
b141171
+			       grub_env_write_sec);
b141171
+  grub_env_export ("check_appended_signatures");
b141171
+
b141171
+  rc = asn1_init ();
b141171
+  if (rc)
b141171
+    grub_fatal ("Error initing ASN.1 data structures: %d: %s\n", rc,
b141171
+		asn1_strerror (rc));
b141171
+
b141171
+  FOR_MODULES (header)
b141171
+  {
b141171
+    struct grub_file pseudo_file;
b141171
+    struct x509_certificate *pk = NULL;
b141171
+    grub_err_t err;
b141171
+
b141171
+    /* Not an ELF module, skip.  */
b141171
+    if (header->type != OBJ_TYPE_X509_PUBKEY)
b141171
+      continue;
b141171
+
b141171
+    grub_memset (&pseudo_file, 0, sizeof (pseudo_file));
b141171
+    pseudo_file.fs = &pseudo_fs;
b141171
+    pseudo_file.size = header->size - sizeof (struct grub_module_header);
b141171
+    pseudo_file.data = (char *) header + sizeof (struct grub_module_header);
b141171
+
b141171
+    grub_dprintf ("appendedsig",
b141171
+		  "Found an x509 key, size=%" PRIuGRUB_UINT64_T "\n",
b141171
+		  pseudo_file.size);
b141171
+
b141171
+    pk = grub_zalloc (sizeof (struct x509_certificate));
b141171
+    if (!pk)
b141171
+      {
b141171
+	grub_fatal ("Out of memory loading initial certificates");
b141171
+      }
b141171
+
b141171
+    err = read_cert_from_file (&pseudo_file, pk);
b141171
+    if (err != GRUB_ERR_NONE)
b141171
+      grub_fatal ("Error loading initial key: %s", grub_errmsg);
b141171
+
b141171
+    grub_dprintf ("appendedsig", "loaded certificate CN='%s'\n", pk->subject);
b141171
+
b141171
+    pk->next = grub_trusted_key;
b141171
+    grub_trusted_key = pk;
b141171
+  }
b141171
+
b141171
+  cmd_trust =
b141171
+    grub_register_command ("trust_certificate", grub_cmd_trust,
b141171
+			   N_("X509_CERTIFICATE"),
b141171
+			   N_("Add X509_CERTIFICATE to trusted certificates."));
b141171
+  cmd_list =
b141171
+    grub_register_command ("list_certificates", grub_cmd_list, 0,
b141171
+			   N_("Show the list of trusted x509 certificates."));
b141171
+  cmd_verify =
b141171
+    grub_register_command ("verify_appended", grub_cmd_verify_signature,
b141171
+			   N_("FILE"),
b141171
+			   N_("Verify FILE against the trusted x509 certificates."));
b141171
+  cmd_distrust =
b141171
+    grub_register_command ("distrust_certificate", grub_cmd_distrust,
b141171
+			   N_("CERT_NUMBER"),
b141171
+			   N_("Remove CERT_NUMBER (as listed by list_certificates) from trusted certificates."));
b141171
+
b141171
+  grub_verifier_register (&grub_appendedsig_verifier);
b141171
+  grub_dl_set_persistent (mod);
b141171
+}
b141171
+
b141171
+GRUB_MOD_FINI (appendedsig)
b141171
+{
b141171
+  /*
b141171
+   * grub_dl_set_persistent should prevent this from actually running, but
b141171
+   * it does still run under emu.
b141171
+   */
b141171
+
b141171
+  grub_verifier_unregister (&grub_appendedsig_verifier);
b141171
+  grub_unregister_command (cmd_verify);
b141171
+  grub_unregister_command (cmd_list);
b141171
+  grub_unregister_command (cmd_trust);
b141171
+  grub_unregister_command (cmd_distrust);
b141171
+}
b141171
diff --git a/include/grub/file.h b/include/grub/file.h
e622855
index 31567483cc..96827a4f89 100644
b141171
--- a/include/grub/file.h
b141171
+++ b/include/grub/file.h
b141171
@@ -80,6 +80,8 @@ enum grub_file_type
b141171
     GRUB_FILE_TYPE_PUBLIC_KEY,
b141171
     /* File holding public key to add to trused keys.  */
b141171
     GRUB_FILE_TYPE_PUBLIC_KEY_TRUST,
b141171
+    /* File holding x509 certificiate to add to trusted keys.  */
b141171
+    GRUB_FILE_TYPE_CERTIFICATE_TRUST,
b141171
     /* File of which we intend to print a blocklist to the user.  */
b141171
     GRUB_FILE_TYPE_PRINT_BLOCKLIST,
b141171
     /* File we intend to use for test loading or testing speed.  */