Blame SOURCES/0331-verifiers-Framework-core.patch

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