Blame SOURCES/0194-appended-signatures-support-verifying-appended-signa.patch

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