dcavalca / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone
5975ab
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5975ab
From: Vladimir Serbinenko <phcoder@gmail.com>
5975ab
Date: Sun, 5 Feb 2017 14:25:47 +0100
5975ab
Subject: [PATCH] verifiers: Framework core
5975ab
5975ab
Verifiers framework provides core file verification functionality which
5975ab
can be used by various security mechanisms, e.g., UEFI secure boot, TPM,
5975ab
PGP signature verification, etc.
5975ab
5975ab
The patch contains PGP code changes and probably they should be extracted
5975ab
to separate patch for the sake of clarity.
5975ab
5975ab
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
5975ab
Signed-off-by: Daniel Kiper <daniel.kiper@oracle.com>
5975ab
Reviewed-by: Ross Philipson <ross.philipson@oracle.com>
5975ab
(cherry picked from commit 75a919e334f8514b6adbc024743cfcd9b88fa394)
5975ab
Signed-off-by: Daniel Axtens <dja@axtens.net>
5975ab
---
5975ab
 grub-core/Makefile.core.def    |   5 +
5975ab
 grub-core/commands/verifiers.c | 197 ++++++++++++++
5975ab
 grub-core/commands/verify.c    | 566 ++++++++++++++++++++---------------------
5975ab
 include/grub/file.h            |   2 +-
5975ab
 include/grub/list.h            |   1 +
5975ab
 include/grub/verify.h          |  65 +++++
5975ab
 6 files changed, 539 insertions(+), 297 deletions(-)
5975ab
 create mode 100644 grub-core/commands/verifiers.c
5975ab
 create mode 100644 include/grub/verify.h
5975ab
5975ab
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
5975ab
index c8a50b4fcfa..29c3bf6cd66 100644
5975ab
--- a/grub-core/Makefile.core.def
5975ab
+++ b/grub-core/Makefile.core.def
5975ab
@@ -921,6 +921,11 @@ module = {
5975ab
   cppflags = '-I$(srcdir)/lib/posix_wrap';
5975ab
 };
5975ab
 
5975ab
+module = {
5975ab
+  name = verifiers;
5975ab
+  common = commands/verifiers.c;
5975ab
+};
5975ab
+
5975ab
 module = {
5975ab
   name = hdparm;
5975ab
   common = commands/hdparm.c;
5975ab
diff --git a/grub-core/commands/verifiers.c b/grub-core/commands/verifiers.c
5975ab
new file mode 100644
5975ab
index 00000000000..fde88318d4c
5975ab
--- /dev/null
5975ab
+++ b/grub-core/commands/verifiers.c
5975ab
@@ -0,0 +1,197 @@
5975ab
+/*
5975ab
+ *  GRUB  --  GRand Unified Bootloader
5975ab
+ *  Copyright (C) 2017  Free Software Foundation, Inc.
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
+ *  Verifiers helper.
5975ab
+ */
5975ab
+
5975ab
+#include <grub/file.h>
5975ab
+#include <grub/verify.h>
5975ab
+#include <grub/dl.h>
5975ab
+
5975ab
+GRUB_MOD_LICENSE ("GPLv3+");
5975ab
+
5975ab
+struct grub_file_verifier *grub_file_verifiers;
5975ab
+
5975ab
+struct grub_verified
5975ab
+{
5975ab
+  grub_file_t file;
5975ab
+  void *buf;
5975ab
+};
5975ab
+typedef struct grub_verified *grub_verified_t;
5975ab
+
5975ab
+static void
5975ab
+verified_free (grub_verified_t verified)
5975ab
+{
5975ab
+  if (verified)
5975ab
+    {
5975ab
+      grub_free (verified->buf);
5975ab
+      grub_free (verified);
5975ab
+    }
5975ab
+}
5975ab
+
5975ab
+static grub_ssize_t
5975ab
+verified_read (struct grub_file *file, char *buf, grub_size_t len)
5975ab
+{
5975ab
+  grub_verified_t verified = file->data;
5975ab
+
5975ab
+  grub_memcpy (buf, (char *) verified->buf + file->offset, len);
5975ab
+  return len;
5975ab
+}
5975ab
+
5975ab
+static grub_err_t
5975ab
+verified_close (struct grub_file *file)
5975ab
+{
5975ab
+  grub_verified_t verified = file->data;
5975ab
+
5975ab
+  grub_file_close (verified->file);
5975ab
+  verified_free (verified);
5975ab
+  file->data = 0;
5975ab
+
5975ab
+  /* Device and name are freed by parent. */
5975ab
+  file->device = 0;
5975ab
+  file->name = 0;
5975ab
+
5975ab
+  return grub_errno;
5975ab
+}
5975ab
+
5975ab
+struct grub_fs verified_fs =
5975ab
+{
5975ab
+  .name = "verified_read",
5975ab
+  .read = verified_read,
5975ab
+  .close = verified_close
5975ab
+};
5975ab
+
5975ab
+static grub_file_t
5975ab
+grub_verifiers_open (grub_file_t io, enum grub_file_type type)
5975ab
+{
5975ab
+  grub_verified_t verified = NULL;
5975ab
+  struct grub_file_verifier *ver;
5975ab
+  void *context;
5975ab
+  grub_file_t ret = 0;
5975ab
+  grub_err_t err;
5975ab
+
5975ab
+  grub_dprintf ("verify", "file: %s type: %d\n", io->name, type);
5975ab
+
5975ab
+  if ((type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_SIGNATURE
5975ab
+      || (type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_VERIFY_SIGNATURE
5975ab
+      || (type & GRUB_FILE_TYPE_SKIP_SIGNATURE))
5975ab
+    return io;
5975ab
+
5975ab
+  if (io->device->disk &&
5975ab
+      (io->device->disk->dev->id == GRUB_DISK_DEVICE_MEMDISK_ID
5975ab
+       || io->device->disk->dev->id == GRUB_DISK_DEVICE_PROCFS_ID))
5975ab
+    return io;
5975ab
+
5975ab
+  FOR_LIST_ELEMENTS(ver, grub_file_verifiers)
5975ab
+    {
5975ab
+      enum grub_verify_flags flags = 0;
5975ab
+      err = ver->init (io, type, &context, &flags);
5975ab
+      if (err)
5975ab
+	goto fail_noclose;
5975ab
+      if (!(flags & GRUB_VERIFY_FLAGS_SKIP_VERIFICATION))
5975ab
+	break;
5975ab
+    }
5975ab
+
5975ab
+  if (!ver)
5975ab
+    /* No verifiers wanted to verify. Just return underlying file. */
5975ab
+    return io;
5975ab
+
5975ab
+  ret = grub_malloc (sizeof (*ret));
5975ab
+  if (!ret)
5975ab
+    {
5975ab
+      goto fail;
5975ab
+    }
5975ab
+  *ret = *io;
5975ab
+
5975ab
+  ret->fs = &verified_fs;
5975ab
+  ret->not_easily_seekable = 0;
5975ab
+  if (ret->size >> (sizeof (grub_size_t) * GRUB_CHAR_BIT - 1))
5975ab
+    {
5975ab
+      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
5975ab
+		  N_("big file signature isn't implemented yet"));
5975ab
+      goto fail;
5975ab
+    }
5975ab
+  verified = grub_malloc (sizeof (*verified));
5975ab
+  if (!verified)
5975ab
+    {
5975ab
+      goto fail;
5975ab
+    }
5975ab
+  verified->buf = grub_malloc (ret->size);
5975ab
+  if (!verified->buf)
5975ab
+    {
5975ab
+      goto fail;
5975ab
+    }
5975ab
+  if (grub_file_read (io, verified->buf, ret->size) != (grub_ssize_t) ret->size)
5975ab
+    {
5975ab
+      if (!grub_errno)
5975ab
+	grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
5975ab
+		    io->name);
5975ab
+      goto fail;
5975ab
+    }
5975ab
+
5975ab
+  err = ver->write (context, verified->buf, ret->size);
5975ab
+  if (err)
5975ab
+    goto fail;
5975ab
+
5975ab
+  err = ver->fini ? ver->fini (context) : GRUB_ERR_NONE;
5975ab
+  if (err)
5975ab
+    goto fail;
5975ab
+
5975ab
+  if (ver->close)
5975ab
+    ver->close (context);
5975ab
+
5975ab
+  FOR_LIST_ELEMENTS_NEXT(ver, grub_file_verifiers)
5975ab
+    {
5975ab
+      enum grub_verify_flags flags = 0;
5975ab
+      err = ver->init (io, type, &context, &flags);
5975ab
+      if (err)
5975ab
+	goto fail_noclose;
5975ab
+      if (flags & GRUB_VERIFY_FLAGS_SKIP_VERIFICATION)
5975ab
+	continue;
5975ab
+      err = ver->write (context, verified->buf, ret->size);
5975ab
+      if (err)
5975ab
+	goto fail;
5975ab
+
5975ab
+      err = ver->fini ? ver->fini (context) : GRUB_ERR_NONE;
5975ab
+      if (err)
5975ab
+	goto fail;
5975ab
+
5975ab
+      if (ver->close)
5975ab
+	ver->close (context);
5975ab
+    }
5975ab
+
5975ab
+  verified->file = io;
5975ab
+  ret->data = verified;
5975ab
+  return ret;
5975ab
+
5975ab
+ fail:
5975ab
+  ver->close (context);
5975ab
+ fail_noclose:
5975ab
+  verified_free (verified);
5975ab
+  grub_free (ret);
5975ab
+  return NULL;
5975ab
+}
5975ab
+
5975ab
+GRUB_MOD_INIT(verifiers)
5975ab
+{
5975ab
+  grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verifiers_open);
5975ab
+}
5975ab
+
5975ab
+GRUB_MOD_FINI(verifiers)
5975ab
+{
5975ab
+  grub_file_filter_unregister (GRUB_FILE_FILTER_VERIFY);
5975ab
+}
5975ab
diff --git a/grub-core/commands/verify.c b/grub-core/commands/verify.c
5975ab
index f0dfeceebd4..29e74a64004 100644
5975ab
--- a/grub-core/commands/verify.c
5975ab
+++ b/grub-core/commands/verify.c
5975ab
@@ -30,16 +30,10 @@
5975ab
 #include <grub/env.h>
5975ab
 #include <grub/kernel.h>
5975ab
 #include <grub/extcmd.h>
5975ab
+#include <grub/verify.h>
5975ab
 
5975ab
 GRUB_MOD_LICENSE ("GPLv3+");
5975ab
 
5975ab
-struct grub_verified
5975ab
-{
5975ab
-  grub_file_t file;
5975ab
-  void *buf;
5975ab
-};
5975ab
-typedef struct grub_verified *grub_verified_t;
5975ab
-
5975ab
 enum
5975ab
   {
5975ab
     OPTION_SKIP_SIG = 0
5975ab
@@ -445,23 +439,27 @@ rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,
5975ab
   return ret;
5975ab
 }
5975ab
 
5975ab
-static grub_err_t
5975ab
-grub_verify_signature_real (char *buf, grub_size_t size,
5975ab
-			    grub_file_t f, grub_file_t sig,
5975ab
-			    struct grub_public_key *pkey)
5975ab
+struct grub_pubkey_context
5975ab
 {
5975ab
-  grub_size_t len;
5975ab
+  grub_file_t sig;
5975ab
+  struct signature_v4_header v4;
5975ab
   grub_uint8_t v;
5975ab
+  const gcry_md_spec_t *hash;
5975ab
+  void *hash_context;
5975ab
+};
5975ab
+
5975ab
+static grub_err_t
5975ab
+grub_verify_signature_init (struct grub_pubkey_context *ctxt, grub_file_t sig)
5975ab
+{
5975ab
+  grub_size_t len;
5975ab
   grub_uint8_t h;
5975ab
   grub_uint8_t t;
5975ab
   grub_uint8_t pk;
5975ab
-  const gcry_md_spec_t *hash;
5975ab
-  struct signature_v4_header v4;
5975ab
   grub_err_t err;
5975ab
-  grub_size_t i;
5975ab
-  gcry_mpi_t mpis[10];
5975ab
   grub_uint8_t type = 0;
5975ab
 
5975ab
+  grub_memset (ctxt, 0, sizeof (*ctxt));
5975ab
+
5975ab
   err = read_packet_header (sig, &type, &len;;
5975ab
   if (err)
5975ab
     return err;
5975ab
@@ -469,18 +467,18 @@ grub_verify_signature_real (char *buf, grub_size_t size,
5975ab
   if (type != 0x2)
5975ab
     return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
 
5975ab
-  if (grub_file_read (sig, &v, sizeof (v)) != sizeof (v))
5975ab
+  if (grub_file_read (sig, &ctxt->v, sizeof (ctxt->v)) != sizeof (ctxt->v))
5975ab
     return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
 
5975ab
-  if (v != 4)
5975ab
+  if (ctxt->v != 4)
5975ab
     return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
 
5975ab
-  if (grub_file_read (sig, &v4, sizeof (v4)) != sizeof (v4))
5975ab
+  if (grub_file_read (sig, &ctxt->v4, sizeof (ctxt->v4)) != sizeof (ctxt->v4))
5975ab
     return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
 
5975ab
-  h = v4.hash;
5975ab
-  t = v4.type;
5975ab
-  pk = v4.pkeyalgo;
5975ab
+  h = ctxt->v4.hash;
5975ab
+  t = ctxt->v4.type;
5975ab
+  pk = ctxt->v4.pkeyalgo;
5975ab
   
5975ab
   if (t != 0)
5975ab
     return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
@@ -491,183 +489,233 @@ grub_verify_signature_real (char *buf, grub_size_t size,
5975ab
   if (pk >= ARRAY_SIZE (pkalgos) || pkalgos[pk].name == NULL)
5975ab
     return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
 
5975ab
-  hash = grub_crypto_lookup_md_by_name (hashes[h]);
5975ab
-  if (!hash)
5975ab
+  ctxt->hash = grub_crypto_lookup_md_by_name (hashes[h]);
5975ab
+  if (!ctxt->hash)
5975ab
     return grub_error (GRUB_ERR_BAD_SIGNATURE, "hash `%s' not loaded", hashes[h]);
5975ab
 
5975ab
   grub_dprintf ("crypt", "alive\n");
5975ab
 
5975ab
-  {
5975ab
-    void *context = NULL;
5975ab
-    unsigned char *hval;
5975ab
-    grub_ssize_t rem = grub_be_to_cpu16 (v4.hashed_sub);
5975ab
-    grub_uint32_t headlen = grub_cpu_to_be32 (rem + 6);
5975ab
-    grub_uint8_t s;
5975ab
-    grub_uint16_t unhashed_sub;
5975ab
-    grub_ssize_t r;
5975ab
-    grub_uint8_t hash_start[2];
5975ab
-    gcry_mpi_t hmpi;
5975ab
-    grub_uint64_t keyid = 0;
5975ab
-    struct grub_public_subkey *sk;
5975ab
-    grub_uint8_t *readbuf = NULL;
5975ab
+  ctxt->sig = sig;
5975ab
 
5975ab
-    context = grub_zalloc (hash->contextsize);
5975ab
-    readbuf = grub_zalloc (READBUF_SIZE);
5975ab
-    if (!context || !readbuf)
5975ab
-      goto fail;
5975ab
-
5975ab
-    hash->init (context);
5975ab
-    if (buf)
5975ab
-      hash->write (context, buf, size);
5975ab
-    else 
5975ab
-      while (1)
5975ab
-	{
5975ab
-	  r = grub_file_read (f, readbuf, READBUF_SIZE);
5975ab
-	  if (r < 0)
5975ab
-	    goto fail;
5975ab
-	  if (r == 0)
5975ab
-	    break;
5975ab
-	  hash->write (context, readbuf, r);
5975ab
-	}
5975ab
-
5975ab
-    hash->write (context, &v, sizeof (v));
5975ab
-    hash->write (context, &v4, sizeof (v4));
5975ab
-    while (rem)
5975ab
-      {
5975ab
-	r = grub_file_read (sig, readbuf,
5975ab
-			    rem < READBUF_SIZE ? rem : READBUF_SIZE);
5975ab
-	if (r < 0)
5975ab
-	  goto fail;
5975ab
-	if (r == 0)
5975ab
-	  break;
5975ab
-	hash->write (context, readbuf, r);
5975ab
-	rem -= r;
5975ab
-      }
5975ab
-    hash->write (context, &v, sizeof (v));
5975ab
-    s = 0xff;
5975ab
-    hash->write (context, &s, sizeof (s));
5975ab
-    hash->write (context, &headlen, sizeof (headlen));
5975ab
-    r = grub_file_read (sig, &unhashed_sub, sizeof (unhashed_sub));
5975ab
-    if (r != sizeof (unhashed_sub))
5975ab
-      goto fail;
5975ab
-    {
5975ab
-      grub_uint8_t *ptr;
5975ab
-      grub_uint32_t l;
5975ab
-      rem = grub_be_to_cpu16 (unhashed_sub);
5975ab
-      if (rem > READBUF_SIZE)
5975ab
-	goto fail;
5975ab
-      r = grub_file_read (sig, readbuf, rem);
5975ab
-      if (r != rem)
5975ab
-	goto fail;
5975ab
-      for (ptr = readbuf; ptr < readbuf + rem; ptr += l)
5975ab
-	{
5975ab
-	  if (*ptr < 192)
5975ab
-	    l = *ptr++;
5975ab
-	  else if (*ptr < 255)
5975ab
-	    {
5975ab
-	      if (ptr + 1 >= readbuf + rem)
5975ab
-		break;
5975ab
-	      l = (((ptr[0] & ~192) << GRUB_CHAR_BIT) | ptr[1]) + 192;
5975ab
-	      ptr += 2;
5975ab
-	    }
5975ab
-	  else
5975ab
-	    {
5975ab
-	      if (ptr + 5 >= readbuf + rem)
5975ab
-		break;
5975ab
-	      l = grub_be_to_cpu32 (grub_get_unaligned32 (ptr + 1));
5975ab
-	      ptr += 5;
5975ab
-	    }
5975ab
-	  if (*ptr == 0x10 && l >= 8)
5975ab
-	    keyid = grub_get_unaligned64 (ptr + 1);
5975ab
-	}
5975ab
-    }
5975ab
-
5975ab
-    hash->final (context);
5975ab
-
5975ab
-    grub_dprintf ("crypt", "alive\n");
5975ab
-
5975ab
-    hval = hash->read (context);
5975ab
-
5975ab
-    if (grub_file_read (sig, hash_start, sizeof (hash_start)) != sizeof (hash_start))
5975ab
-      goto fail;
5975ab
-    if (grub_memcmp (hval, hash_start, sizeof (hash_start)) != 0)
5975ab
-      goto fail;
5975ab
-
5975ab
-    grub_dprintf ("crypt", "@ %x\n", (int)grub_file_tell (sig));
5975ab
-
5975ab
-    for (i = 0; i < pkalgos[pk].nmpisig; i++)
5975ab
-      {
5975ab
-	grub_uint16_t l;
5975ab
-	grub_size_t lb;
5975ab
-	grub_dprintf ("crypt", "alive\n");
5975ab
-	if (grub_file_read (sig, &l, sizeof (l)) != sizeof (l))
5975ab
-	  goto fail;
5975ab
-	grub_dprintf ("crypt", "alive\n");
5975ab
-	lb = (grub_be_to_cpu16 (l) + 7) / 8;
5975ab
-	grub_dprintf ("crypt", "l = 0x%04x\n", grub_be_to_cpu16 (l));
5975ab
-	if (lb > READBUF_SIZE - sizeof (grub_uint16_t))
5975ab
-	  goto fail;
5975ab
-	grub_dprintf ("crypt", "alive\n");
5975ab
-	if (grub_file_read (sig, readbuf + sizeof (grub_uint16_t), lb) != (grub_ssize_t) lb)
5975ab
-	  goto fail;
5975ab
-	grub_dprintf ("crypt", "alive\n");
5975ab
-	grub_memcpy (readbuf, &l, sizeof (l));
5975ab
-	grub_dprintf ("crypt", "alive\n");
5975ab
-
5975ab
-	if (gcry_mpi_scan (&mpis[i], GCRYMPI_FMT_PGP,
5975ab
-			   readbuf, lb + sizeof (grub_uint16_t), 0))
5975ab
-	  goto fail;
5975ab
-	grub_dprintf ("crypt", "alive\n");
5975ab
-      }
5975ab
-
5975ab
-    if (pkey)
5975ab
-      sk = grub_crypto_pk_locate_subkey (keyid, pkey);
5975ab
-    else
5975ab
-      sk = grub_crypto_pk_locate_subkey_in_trustdb (keyid);
5975ab
-    if (!sk)
5975ab
-      {
5975ab
-	/* TRANSLATORS: %08x is 32-bit key id.  */
5975ab
-	grub_error (GRUB_ERR_BAD_SIGNATURE, N_("public key %08x not found"),
5975ab
-		    keyid);
5975ab
-	goto fail;
5975ab
-      }
5975ab
-
5975ab
-    if (pkalgos[pk].pad (&hmpi, hval, hash, sk))
5975ab
-      goto fail;
5975ab
-    if (!*pkalgos[pk].algo)
5975ab
-      {
5975ab
-	grub_dl_load (pkalgos[pk].module);
5975ab
-	grub_errno = GRUB_ERR_NONE;
5975ab
-      }
5975ab
-
5975ab
-    if (!*pkalgos[pk].algo)
5975ab
-      {
5975ab
-	grub_error (GRUB_ERR_BAD_SIGNATURE, N_("module `%s' isn't loaded"),
5975ab
-		    pkalgos[pk].module);
5975ab
-	goto fail;
5975ab
-      }
5975ab
-    if ((*pkalgos[pk].algo)->verify (0, hmpi, mpis, sk->mpis, 0, 0))
5975ab
-      goto fail;
5975ab
-
5975ab
-    grub_free (context);
5975ab
-    grub_free (readbuf);
5975ab
-
5975ab
-    return GRUB_ERR_NONE;
5975ab
-
5975ab
-  fail:
5975ab
-    grub_free (context);
5975ab
-    grub_free (readbuf);
5975ab
-    if (!grub_errno)
5975ab
-      return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
+  ctxt->hash_context = grub_zalloc (ctxt->hash->contextsize);
5975ab
+  if (!ctxt->hash_context)
5975ab
     return grub_errno;
5975ab
+
5975ab
+  ctxt->hash->init (ctxt->hash_context);
5975ab
+
5975ab
+  return GRUB_ERR_NONE;
5975ab
+}
5975ab
+
5975ab
+static grub_err_t
5975ab
+grub_pubkey_write (void *ctxt_, void *buf, grub_size_t size)
5975ab
+{
5975ab
+  struct grub_pubkey_context *ctxt = ctxt_;
5975ab
+  ctxt->hash->write (ctxt->hash_context, buf, size);
5975ab
+  return GRUB_ERR_NONE;
5975ab
+}
5975ab
+
5975ab
+static grub_err_t
5975ab
+grub_verify_signature_real (struct grub_pubkey_context *ctxt,
5975ab
+			    struct grub_public_key *pkey)
5975ab
+{
5975ab
+  gcry_mpi_t mpis[10];
5975ab
+  grub_uint8_t pk = ctxt->v4.pkeyalgo;
5975ab
+  grub_size_t i;
5975ab
+  grub_uint8_t *readbuf = NULL;
5975ab
+  unsigned char *hval;
5975ab
+  grub_ssize_t rem = grub_be_to_cpu16 (ctxt->v4.hashed_sub);
5975ab
+  grub_uint32_t headlen = grub_cpu_to_be32 (rem + 6);
5975ab
+  grub_uint8_t s;
5975ab
+  grub_uint16_t unhashed_sub;
5975ab
+  grub_ssize_t r;
5975ab
+  grub_uint8_t hash_start[2];
5975ab
+  gcry_mpi_t hmpi;
5975ab
+  grub_uint64_t keyid = 0;
5975ab
+  struct grub_public_subkey *sk;
5975ab
+
5975ab
+  readbuf = grub_malloc (READBUF_SIZE);
5975ab
+  if (!readbuf)
5975ab
+    goto fail;
5975ab
+
5975ab
+  ctxt->hash->write (ctxt->hash_context, &ctxt->v, sizeof (ctxt->v));
5975ab
+  ctxt->hash->write (ctxt->hash_context, &ctxt->v4, sizeof (ctxt->v4));
5975ab
+  while (rem)
5975ab
+    {
5975ab
+      r = grub_file_read (ctxt->sig, readbuf,
5975ab
+			  rem < READBUF_SIZE ? rem : READBUF_SIZE);
5975ab
+      if (r < 0)
5975ab
+	goto fail;
5975ab
+      if (r == 0)
5975ab
+	break;
5975ab
+      ctxt->hash->write (ctxt->hash_context, readbuf, r);
5975ab
+      rem -= r;
5975ab
+    }
5975ab
+  ctxt->hash->write (ctxt->hash_context, &ctxt->v, sizeof (ctxt->v));
5975ab
+  s = 0xff;
5975ab
+  ctxt->hash->write (ctxt->hash_context, &s, sizeof (s));
5975ab
+  ctxt->hash->write (ctxt->hash_context, &headlen, sizeof (headlen));
5975ab
+  r = grub_file_read (ctxt->sig, &unhashed_sub, sizeof (unhashed_sub));
5975ab
+  if (r != sizeof (unhashed_sub))
5975ab
+    goto fail;
5975ab
+  {
5975ab
+    grub_uint8_t *ptr;
5975ab
+    grub_uint32_t l;
5975ab
+    rem = grub_be_to_cpu16 (unhashed_sub);
5975ab
+    if (rem > READBUF_SIZE)
5975ab
+      goto fail;
5975ab
+    r = grub_file_read (ctxt->sig, readbuf, rem);
5975ab
+    if (r != rem)
5975ab
+      goto fail;
5975ab
+    for (ptr = readbuf; ptr < readbuf + rem; ptr += l)
5975ab
+      {
5975ab
+	if (*ptr < 192)
5975ab
+	  l = *ptr++;
5975ab
+	else if (*ptr < 255)
5975ab
+	  {
5975ab
+	    if (ptr + 1 >= readbuf + rem)
5975ab
+	      break;
5975ab
+	    l = (((ptr[0] & ~192) << GRUB_CHAR_BIT) | ptr[1]) + 192;
5975ab
+	    ptr += 2;
5975ab
+	  }
5975ab
+	else
5975ab
+	  {
5975ab
+	    if (ptr + 5 >= readbuf + rem)
5975ab
+	      break;
5975ab
+	    l = grub_be_to_cpu32 (grub_get_unaligned32 (ptr + 1));
5975ab
+	    ptr += 5;
5975ab
+	  }
5975ab
+	if (*ptr == 0x10 && l >= 8)
5975ab
+	  keyid = grub_get_unaligned64 (ptr + 1);
5975ab
+      }
5975ab
   }
5975ab
+
5975ab
+  ctxt->hash->final (ctxt->hash_context);
5975ab
+
5975ab
+  grub_dprintf ("crypt", "alive\n");
5975ab
+
5975ab
+  hval = ctxt->hash->read (ctxt->hash_context);
5975ab
+
5975ab
+  if (grub_file_read (ctxt->sig, hash_start, sizeof (hash_start)) != sizeof (hash_start))
5975ab
+    goto fail;
5975ab
+  if (grub_memcmp (hval, hash_start, sizeof (hash_start)) != 0)
5975ab
+    goto fail;
5975ab
+
5975ab
+  grub_dprintf ("crypt", "@ %x\n", (int)grub_file_tell (ctxt->sig));
5975ab
+
5975ab
+  for (i = 0; i < pkalgos[pk].nmpisig; i++)
5975ab
+    {
5975ab
+      grub_uint16_t l;
5975ab
+      grub_size_t lb;
5975ab
+      grub_dprintf ("crypt", "alive\n");
5975ab
+      if (grub_file_read (ctxt->sig, &l, sizeof (l)) != sizeof (l))
5975ab
+	goto fail;
5975ab
+      grub_dprintf ("crypt", "alive\n");
5975ab
+      lb = (grub_be_to_cpu16 (l) + 7) / 8;
5975ab
+      grub_dprintf ("crypt", "l = 0x%04x\n", grub_be_to_cpu16 (l));
5975ab
+      if (lb > READBUF_SIZE - sizeof (grub_uint16_t))
5975ab
+	goto fail;
5975ab
+      grub_dprintf ("crypt", "alive\n");
5975ab
+      if (grub_file_read (ctxt->sig, readbuf + sizeof (grub_uint16_t), lb) != (grub_ssize_t) lb)
5975ab
+	goto fail;
5975ab
+      grub_dprintf ("crypt", "alive\n");
5975ab
+      grub_memcpy (readbuf, &l, sizeof (l));
5975ab
+      grub_dprintf ("crypt", "alive\n");
5975ab
+
5975ab
+      if (gcry_mpi_scan (&mpis[i], GCRYMPI_FMT_PGP,
5975ab
+			 readbuf, lb + sizeof (grub_uint16_t), 0))
5975ab
+	goto fail;
5975ab
+      grub_dprintf ("crypt", "alive\n");
5975ab
+    }
5975ab
+
5975ab
+  if (pkey)
5975ab
+    sk = grub_crypto_pk_locate_subkey (keyid, pkey);
5975ab
+  else
5975ab
+    sk = grub_crypto_pk_locate_subkey_in_trustdb (keyid);
5975ab
+  if (!sk)
5975ab
+    {
5975ab
+      /* TRANSLATORS: %08x is 32-bit key id.  */
5975ab
+      grub_error (GRUB_ERR_BAD_SIGNATURE, N_("public key %08x not found"),
5975ab
+		  keyid);
5975ab
+      goto fail;
5975ab
+    }
5975ab
+
5975ab
+  if (pkalgos[pk].pad (&hmpi, hval, ctxt->hash, sk))
5975ab
+    goto fail;
5975ab
+  if (!*pkalgos[pk].algo)
5975ab
+    {
5975ab
+      grub_dl_load (pkalgos[pk].module);
5975ab
+      grub_errno = GRUB_ERR_NONE;
5975ab
+    }
5975ab
+
5975ab
+  if (!*pkalgos[pk].algo)
5975ab
+    {
5975ab
+      grub_error (GRUB_ERR_BAD_SIGNATURE, N_("module `%s' isn't loaded"),
5975ab
+		  pkalgos[pk].module);
5975ab
+      goto fail;
5975ab
+    }
5975ab
+  if ((*pkalgos[pk].algo)->verify (0, hmpi, mpis, sk->mpis, 0, 0))
5975ab
+    goto fail;
5975ab
+
5975ab
+  grub_free (readbuf);
5975ab
+
5975ab
+  return GRUB_ERR_NONE;
5975ab
+
5975ab
+ fail:
5975ab
+  grub_free (readbuf);
5975ab
+  if (!grub_errno)
5975ab
+    return grub_error (GRUB_ERR_BAD_SIGNATURE, N_("bad signature"));
5975ab
+  return grub_errno;
5975ab
+}
5975ab
+
5975ab
+static void
5975ab
+grub_pubkey_close_real (struct grub_pubkey_context *ctxt)
5975ab
+{
5975ab
+  if (ctxt->sig)
5975ab
+    grub_file_close (ctxt->sig);
5975ab
+  if (ctxt->hash_context)
5975ab
+    grub_free (ctxt->hash_context);
5975ab
+}
5975ab
+
5975ab
+static void
5975ab
+grub_pubkey_close (void *ctxt)
5975ab
+{
5975ab
+  grub_pubkey_close_real (ctxt);
5975ab
+  grub_free (ctxt);
5975ab
 }
5975ab
 
5975ab
 grub_err_t
5975ab
 grub_verify_signature (grub_file_t f, grub_file_t sig,
5975ab
 		       struct grub_public_key *pkey)
5975ab
 {
5975ab
-  return grub_verify_signature_real (0, 0, f, sig, pkey);
5975ab
+  grub_err_t err;
5975ab
+  struct grub_pubkey_context ctxt;
5975ab
+  grub_uint8_t *readbuf = NULL;
5975ab
+
5975ab
+  err = grub_verify_signature_init (&ctxt, sig);
5975ab
+  if (err)
5975ab
+    return err;
5975ab
+
5975ab
+  readbuf = grub_zalloc (READBUF_SIZE);
5975ab
+  if (!readbuf)
5975ab
+    goto fail;
5975ab
+
5975ab
+  while (1)
5975ab
+    {
5975ab
+      grub_ssize_t r;
5975ab
+      r = grub_file_read (f, readbuf, READBUF_SIZE);
5975ab
+      if (r < 0)
5975ab
+	goto fail;
5975ab
+      if (r == 0)
5975ab
+	break;
5975ab
+      err = grub_pubkey_write (&ctxt, readbuf, r);
5975ab
+      if (err)
5975ab
+	return err;
5975ab
+    }
5975ab
+
5975ab
+  grub_verify_signature_real (&ctxt, pkey);
5975ab
+ fail:
5975ab
+  grub_pubkey_close_real (&ctxt);
5975ab
+  return grub_errno;
5975ab
 }
5975ab
 
5975ab
 static grub_err_t
5975ab
@@ -819,134 +867,52 @@ grub_cmd_verify_signature (grub_extcmd_context_t ctxt,
5975ab
 
5975ab
 static int sec = 0;
5975ab
 
5975ab
-static void
5975ab
-verified_free (grub_verified_t verified)
5975ab
-{
5975ab
-  if (verified)
5975ab
-    {
5975ab
-      grub_free (verified->buf);
5975ab
-      grub_free (verified);
5975ab
-    }
5975ab
-}
5975ab
-
5975ab
-static grub_ssize_t
5975ab
-verified_read (struct grub_file *file, char *buf, grub_size_t len)
5975ab
-{
5975ab
-  grub_verified_t verified = file->data;
5975ab
-
5975ab
-  grub_memcpy (buf, (char *) verified->buf + file->offset, len);
5975ab
-  return len;
5975ab
-}
5975ab
-
5975ab
 static grub_err_t
5975ab
-verified_close (struct grub_file *file)
5975ab
-{
5975ab
-  grub_verified_t verified = file->data;
5975ab
-
5975ab
-  grub_file_close (verified->file);
5975ab
-  verified_free (verified);
5975ab
-  file->data = 0;
5975ab
-
5975ab
-  /* device and name are freed by parent */
5975ab
-  file->device = 0;
5975ab
-  file->name = 0;
5975ab
-
5975ab
-  return grub_errno;
5975ab
-}
5975ab
-
5975ab
-struct grub_fs verified_fs =
5975ab
-{
5975ab
-  .name = "verified_read",
5975ab
-  .read = verified_read,
5975ab
-  .close = verified_close
5975ab
-};
5975ab
-
5975ab
-static grub_file_t
5975ab
-grub_pubkey_open (grub_file_t io, enum grub_file_type type)
5975ab
+grub_pubkey_init (grub_file_t io, enum grub_file_type type __attribute__ ((unused)),
5975ab
+		  void **context, enum grub_verify_flags *flags)
5975ab
 {
5975ab
   grub_file_t sig;
5975ab
   char *fsuf, *ptr;
5975ab
   grub_err_t err;
5975ab
-  grub_file_t ret;
5975ab
-  grub_verified_t verified;
5975ab
-
5975ab
-  if ((type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_SIGNATURE
5975ab
-      || (type & GRUB_FILE_TYPE_MASK) == GRUB_FILE_TYPE_VERIFY_SIGNATURE
5975ab
-      || (type & GRUB_FILE_TYPE_SKIP_SIGNATURE))
5975ab
-    return io;
5975ab
+  struct grub_pubkey_context *ctxt;
5975ab
 
5975ab
   if (!sec)
5975ab
-    return io;
5975ab
-  if (io->device->disk && 
5975ab
-      (io->device->disk->dev->id == GRUB_DISK_DEVICE_MEMDISK_ID
5975ab
-       || io->device->disk->dev->id == GRUB_DISK_DEVICE_PROCFS_ID))
5975ab
-    return io;
5975ab
+    {
5975ab
+      *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION;
5975ab
+      return GRUB_ERR_NONE;
5975ab
+    }
5975ab
+
5975ab
   fsuf = grub_malloc (grub_strlen (io->name) + sizeof (".sig"));
5975ab
   if (!fsuf)
5975ab
-    return NULL;
5975ab
+    return grub_errno;
5975ab
   ptr = grub_stpcpy (fsuf, io->name);
5975ab
   grub_memcpy (ptr, ".sig", sizeof (".sig"));
5975ab
 
5975ab
   sig = grub_file_open (fsuf, GRUB_FILE_TYPE_SIGNATURE);
5975ab
   grub_free (fsuf);
5975ab
   if (!sig)
5975ab
-    return NULL;
5975ab
+    return grub_errno;
5975ab
 
5975ab
-  ret = grub_malloc (sizeof (*ret));
5975ab
-  if (!ret)
5975ab
+  ctxt = grub_malloc (sizeof (*ctxt));
5975ab
+  if (!ctxt)
5975ab
     {
5975ab
       grub_file_close (sig);
5975ab
-      return NULL;
5975ab
+      return grub_errno;
5975ab
     }
5975ab
-  *ret = *io;
5975ab
-
5975ab
-  ret->fs = &verified_fs;
5975ab
-  ret->not_easily_seekable = 0;
5975ab
-  if (ret->size >> (sizeof (grub_size_t) * GRUB_CHAR_BIT - 1))
5975ab
-    {
5975ab
-      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
5975ab
-		  "big file signature isn't implemented yet");
5975ab
-      grub_file_close (sig);
5975ab
-      grub_free (ret);
5975ab
-      return NULL;
5975ab
-    }
5975ab
-  verified = grub_malloc (sizeof (*verified));
5975ab
-  if (!verified)
5975ab
-    {
5975ab
-      grub_file_close (sig);
5975ab
-      grub_free (ret);
5975ab
-      return NULL;
5975ab
-    }
5975ab
-  verified->buf = grub_malloc (ret->size);
5975ab
-  if (!verified->buf)
5975ab
-    {
5975ab
-      grub_file_close (sig);
5975ab
-      verified_free (verified);
5975ab
-      grub_free (ret);
5975ab
-      return NULL;
5975ab
-    }
5975ab
-  if (grub_file_read (io, verified->buf, ret->size) != (grub_ssize_t) ret->size)
5975ab
-    {
5975ab
-      if (!grub_errno)
5975ab
-	grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
5975ab
-		    io->name);
5975ab
-      grub_file_close (sig);
5975ab
-      verified_free (verified);
5975ab
-      grub_free (ret);
5975ab
-      return NULL;
5975ab
-    }
5975ab
-
5975ab
-  err = grub_verify_signature_real (verified->buf, ret->size, 0, sig, NULL);
5975ab
-  grub_file_close (sig);
5975ab
+  err = grub_verify_signature_init (ctxt, sig);
5975ab
   if (err)
5975ab
     {
5975ab
-      verified_free (verified);
5975ab
-      grub_free (ret);
5975ab
-      return NULL;
5975ab
+      grub_pubkey_close (ctxt);
5975ab
+      return err;
5975ab
     }
5975ab
-  verified->file = io;
5975ab
-  ret->data = verified;
5975ab
-  return ret;
5975ab
+  *context = ctxt;
5975ab
+  return GRUB_ERR_NONE;
5975ab
+}
5975ab
+
5975ab
+static grub_err_t
5975ab
+grub_pubkey_fini (void *ctxt)
5975ab
+{
5975ab
+  return grub_verify_signature_real (ctxt, NULL);
5975ab
 }
5975ab
 
5975ab
 static char *
5975ab
@@ -970,8 +936,16 @@ struct grub_fs pseudo_fs =
5975ab
   {
5975ab
     .name = "pseudo",
5975ab
     .read = pseudo_read
5975ab
-};
5975ab
+  };
5975ab
 
5975ab
+struct grub_file_verifier grub_pubkey_verifier =
5975ab
+  {
5975ab
+    .name = "pgp",
5975ab
+    .init = grub_pubkey_init,
5975ab
+    .fini = grub_pubkey_fini,
5975ab
+    .write = grub_pubkey_write,
5975ab
+    .close = grub_pubkey_close,
5975ab
+  };
5975ab
 
5975ab
 static grub_extcmd_t cmd, cmd_trust;
5975ab
 static grub_command_t cmd_distrust, cmd_list;
5975ab
@@ -986,8 +960,6 @@ GRUB_MOD_INIT(verify)
5975ab
     sec = 1;
5975ab
   else
5975ab
     sec = 0;
5975ab
-    
5975ab
-  grub_file_filter_register (GRUB_FILE_FILTER_PUBKEY, grub_pubkey_open);
5975ab
 
5975ab
   grub_register_variable_hook ("check_signatures", 0, grub_env_write_sec);
5975ab
   grub_env_export ("check_signatures");
5975ab
@@ -1033,11 +1005,13 @@ GRUB_MOD_INIT(verify)
5975ab
   cmd_distrust = grub_register_command ("distrust", grub_cmd_distrust,
5975ab
 					N_("PUBKEY_ID"),
5975ab
 					N_("Remove PUBKEY_ID from trusted keys."));
5975ab
+
5975ab
+  grub_verifier_register (&grub_pubkey_verifier);
5975ab
 }
5975ab
 
5975ab
 GRUB_MOD_FINI(verify)
5975ab
 {
5975ab
-  grub_file_filter_unregister (GRUB_FILE_FILTER_PUBKEY);
5975ab
+  grub_verifier_unregister (&grub_pubkey_verifier);
5975ab
   grub_unregister_extcmd (cmd);
5975ab
   grub_unregister_extcmd (cmd_trust);
5975ab
   grub_unregister_command (cmd_list);
5975ab
diff --git a/include/grub/file.h b/include/grub/file.h
5975ab
index 5b47c5f91f9..19dda67f68b 100644
5975ab
--- a/include/grub/file.h
5975ab
+++ b/include/grub/file.h
5975ab
@@ -171,7 +171,7 @@ extern grub_disk_read_hook_t EXPORT_VAR(grub_file_progress_hook);
5975ab
 /* Filters with lower ID are executed first.  */
5975ab
 typedef enum grub_file_filter_id
5975ab
   {
5975ab
-    GRUB_FILE_FILTER_PUBKEY,
5975ab
+    GRUB_FILE_FILTER_VERIFY,
5975ab
     GRUB_FILE_FILTER_GZIO,
5975ab
     GRUB_FILE_FILTER_XZIO,
5975ab
     GRUB_FILE_FILTER_LZOPIO,
5975ab
diff --git a/include/grub/list.h b/include/grub/list.h
5975ab
index d170ff6da02..b13acb96243 100644
5975ab
--- a/include/grub/list.h
5975ab
+++ b/include/grub/list.h
5975ab
@@ -35,6 +35,7 @@ void EXPORT_FUNC(grub_list_push) (grub_list_t *head, grub_list_t item);
5975ab
 void EXPORT_FUNC(grub_list_remove) (grub_list_t item);
5975ab
 
5975ab
 #define FOR_LIST_ELEMENTS(var, list) for ((var) = (list); (var); (var) = (var)->next)
5975ab
+#define FOR_LIST_ELEMENTS_NEXT(var, list) for ((var) = (var)->next; (var); (var) = (var)->next)
5975ab
 #define FOR_LIST_ELEMENTS_SAFE(var, nxt, list) for ((var) = (list), (nxt) = ((var) ? (var)->next : 0); (var); (var) = (nxt), ((nxt) = (var) ? (var)->next : 0))
5975ab
 
5975ab
 static inline void *
5975ab
diff --git a/include/grub/verify.h b/include/grub/verify.h
5975ab
new file mode 100644
5975ab
index 00000000000..298120f5776
5975ab
--- /dev/null
5975ab
+++ b/include/grub/verify.h
5975ab
@@ -0,0 +1,65 @@
5975ab
+/*
5975ab
+ *  GRUB  --  GRand Unified Bootloader
5975ab
+ *  Copyright (C) 2017  Free Software Foundation, Inc.
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/file.h>
5975ab
+#include <grub/list.h>
5975ab
+
5975ab
+enum grub_verify_flags
5975ab
+  {
5975ab
+    GRUB_VERIFY_FLAGS_SKIP_VERIFICATION	= 1,
5975ab
+    GRUB_VERIFY_FLAGS_SINGLE_CHUNK	= 2
5975ab
+  };
5975ab
+
5975ab
+struct grub_file_verifier
5975ab
+{
5975ab
+  struct grub_file_verifier *next;
5975ab
+  struct grub_file_verifier **prev;
5975ab
+
5975ab
+  const char *name;
5975ab
+
5975ab
+  /*
5975ab
+   * Check if file needs to be verified and set up context.
5975ab
+   * init/read/fini is structured in the same way as hash interface.
5975ab
+   */
5975ab
+  grub_err_t (*init) (grub_file_t io, enum grub_file_type type,
5975ab
+		      void **context, enum grub_verify_flags *flags);
5975ab
+
5975ab
+  /*
5975ab
+   * Right now we pass the whole file in one call but it may
5975ab
+   * change in the future. If you insist on single buffer you
5975ab
+   * need to set GRUB_VERIFY_FLAGS_SINGLE_CHUNK in verify_flags.
5975ab
+   */
5975ab
+  grub_err_t (*write) (void *context, void *buf, grub_size_t size);
5975ab
+
5975ab
+  grub_err_t (*fini) (void *context);
5975ab
+  void (*close) (void *context);
5975ab
+};
5975ab
+
5975ab
+extern struct grub_file_verifier *grub_file_verifiers;
5975ab
+
5975ab
+static inline void
5975ab
+grub_verifier_register (struct grub_file_verifier *ver)
5975ab
+{
5975ab
+  grub_list_push (GRUB_AS_LIST_P (&grub_file_verifiers), GRUB_AS_LIST (ver));
5975ab
+}
5975ab
+
5975ab
+static inline void
5975ab
+grub_verifier_unregister (struct grub_file_verifier *ver)
5975ab
+{
5975ab
+  grub_list_remove (GRUB_AS_LIST (ver));
5975ab
+}