dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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