vbutil_kernel: support exporting a keyblock file during verify

Reuses the --keyblock argument to output a keyblock if used
during Verify().

TEST=built, ran on a kernel; check if it worked for cgpt find -M :)
BUG=chromium-os:7451

Change-Id: Ibf1365dbdaeaf87442e0d12d048bc070f35662ad

Review URL: http://codereview.chromium.org/4160001
diff --git a/utility/vbutil_kernel.c b/utility/vbutil_kernel.c
index cb47681..c2b32d8 100644
--- a/utility/vbutil_kernel.c
+++ b/utility/vbutil_kernel.c
@@ -112,6 +112,8 @@
           "    --signpubkey <file>"
           "       Public key to verify kernel keyblock, in .vbpubk format\n"
           "    --verbose                 Print a more detailed report\n"
+          "    --keyblock <file>"
+          "       Outputs the verified key block, in .keyblock format\n"
           "\n",
           progname);
   return 1;
@@ -631,7 +633,8 @@
   return 0;
 }
 
-static int Verify(const char* infile, const char* signpubkey, int verbose) {
+static int Verify(const char* infile, const char* signpubkey, int verbose,
+                  const char* key_block_file) {
 
   VbKeyBlockHeader* key_block;
   VbKernelPreambleHeader* preamble;
@@ -672,6 +675,20 @@
   }
   now = key_block->key_block_size;
 
+  if (key_block_file) {
+    FILE* f = NULL;
+    f = fopen(key_block_file, "wb");
+    if (!f) {
+      error("Can't open key block file %s\n", key_block_file);
+      return 1;
+    }
+    if (1 != fwrite(key_block, key_block->key_block_size, 1, f)) {
+      error("Can't write key block file %s\n", key_block_file);
+      return 1;
+    }
+    fclose(f);
+  }
+
   printf("Key block:\n");
   data_key = &key_block->data_key;
   if (verbose)
@@ -883,7 +900,7 @@
       return r;
 
     case OPT_MODE_VERIFY:
-      return Verify(filename, signpubkey, verbose);
+      return Verify(filename, signpubkey, verbose, key_block_file);
 
     default:
       fprintf(stderr,