futility: Use only vboot 2.0 APIs for keyblocks
[vboot.git] / futility / vb1_helper.c
1 /*
2  * Copyright 2014 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <errno.h>
8 #include <inttypes.h>           /* For PRIu64 */
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <openssl/rsa.h>
13
14 #include "2sysincludes.h"
15 #include "2api.h"
16 #include "2common.h"
17 #include "2rsa.h"
18 #include "2sha.h"
19 #include "file_type.h"
20 #include "futility.h"
21 #include "host_common.h"
22 #include "kernel_blob.h"
23 #include "util_misc.h"
24 #include "vb1_helper.h"
25 #include "vb2_common.h"
26
27 const char *vb1_crypto_name(uint32_t algo)
28 {
29         return algo < kNumAlgorithms ? algo_strings[algo] : "(invalid)";
30 }
31
32 /****************************************************************************/
33 /* Here are globals containing all the bits & pieces I'm working on.
34  *
35  * kernel vblock    = keyblock + kernel preamble + padding to 64K (or whatever)
36  * kernel blob      = 32-bit kernel + config file + params + bootloader stub +
37  *                    vmlinuz_header
38  * kernel partition = kernel vblock + kernel blob
39  *
40  * The VbKernelPreambleHeader.preamble_size includes the padding.
41  */
42
43 /* The keyblock, preamble, and kernel blob are kept in separate places. */
44 static struct vb2_keyblock *g_keyblock;
45 static VbKernelPreambleHeader *g_preamble;
46 static uint8_t *g_kernel_blob_data;
47 static uint64_t g_kernel_blob_size;
48
49 /* These refer to individual parts within the kernel blob. */
50 static uint8_t *g_kernel_data;
51 static uint64_t g_kernel_size;
52 static uint8_t *g_config_data;
53 static uint64_t g_config_size;
54 static uint8_t *g_param_data;
55 static uint64_t g_param_size;
56 static uint8_t *g_bootloader_data;
57 static uint64_t g_bootloader_size;
58 static uint8_t *g_vmlinuz_header_data;
59 static uint64_t g_vmlinuz_header_size;
60
61 static uint64_t g_ondisk_bootloader_addr;
62 static uint64_t g_ondisk_vmlinuz_header_addr;
63
64
65 /*
66  * Read the kernel command line from a file. Get rid of \n characters along
67  * the way and verify that the line fits into a 4K buffer.
68  *
69  * Return the buffer contaning the line on success (and set the line length
70  * using the passed in parameter), or NULL in case something goes wrong.
71  */
72 uint8_t *ReadConfigFile(const char *config_file, uint64_t *config_size)
73 {
74         uint8_t *config_buf;
75         int i;
76
77         config_buf = ReadFile(config_file, config_size);
78         if (!config_buf)
79                 return NULL;
80         Debug(" config file size=0x%" PRIx64 "\n", *config_size);
81         if (CROS_CONFIG_SIZE <= *config_size) { /* room for trailing '\0' */
82                 fprintf(stderr, "Config file %s is too large (>= %d bytes)\n",
83                         config_file, CROS_CONFIG_SIZE);
84                 return NULL;
85         }
86
87         /* Replace newlines with spaces */
88         for (i = 0; i < *config_size; i++)
89                 if ('\n' == config_buf[i])
90                         config_buf[i] = ' ';
91
92         return config_buf;
93 }
94
95 /****************************************************************************/
96
97 /* Return the smallest integral multiple of [alignment] that is equal
98  * to or greater than [val]. Used to determine the number of
99  * pages/sectors/blocks/whatever needed to contain [val]
100  * items/bytes/etc. */
101 static uint64_t roundup(uint64_t val, uint64_t alignment)
102 {
103         uint64_t rem = val % alignment;
104         if (rem)
105                 return val + (alignment - rem);
106         return val;
107 }
108
109 /* Match regexp /\b--\b/ to delimit the start of the kernel commandline. If we
110  * don't find one, we'll use the whole thing. */
111 static unsigned int find_cmdline_start(uint8_t *buf_ptr, unsigned int max_len)
112 {
113         char *input = (char *)buf_ptr;
114         int start = 0;
115         int i;
116         for (i = 0; i < max_len - 1 && input[i]; i++) {
117                 if ('-' == input[i] && '-' == input[i + 1]) {
118                         if ((i == 0 || ' ' == input[i - 1]) &&
119                             (i + 2 >= max_len || ' ' == input[i + 2])) {
120                                 /* found "--" with nothing before or after it */
121                                 start = i + 2;  /* hope for a trailing '\0' */
122                                 break;
123                         }
124                 }
125         }
126         while (' ' == input[start])     /* skip leading spaces */
127                 start++;
128
129         return start;
130 }
131
132 /* Offset of kernel command line string from the start of the kernel blob */
133 uint64_t kernel_cmd_line_offset(const struct vb2_kernel_preamble *preamble)
134 {
135         return preamble->bootloader_address - preamble->body_load_address -
136             CROS_CONFIG_SIZE - CROS_PARAMS_SIZE;
137 }
138
139 /* Returns the size of the 32-bit kernel, or negative on error. */
140 static int KernelSize(uint8_t *kernel_buf, uint64_t kernel_size,
141                       enum arch_t arch)
142 {
143         uint64_t kernel32_start = 0;
144         struct linux_kernel_params *lh;
145
146         /* Except for x86, the kernel is the kernel. */
147         if (arch != ARCH_X86)
148                 return kernel_size;
149
150         /* The first part of the x86 vmlinuz is a header, followed by
151          * a real-mode boot stub. We only want the 32-bit part. */
152         lh = (struct linux_kernel_params *)kernel_buf;
153         kernel32_start = (lh->setup_sects + 1) << 9;
154         if (kernel32_start >= kernel_size) {
155                 fprintf(stderr, "Malformed kernel\n");
156                 return -1;
157         }
158         return kernel_size - kernel32_start;
159 }
160
161 /* This extracts g_kernel_* and g_param_* from a standard vmlinuz file.
162  * It returns nonzero on error. */
163 static int PickApartVmlinuz(uint8_t *kernel_buf, uint64_t kernel_size,
164                             enum arch_t arch,
165                             uint64_t kernel_body_load_address)
166 {
167         uint64_t kernel32_start = 0;
168         uint64_t kernel32_size = kernel_size;
169         struct linux_kernel_params *lh, *params;
170
171         /* Except for x86, the kernel is the kernel. */
172         if (arch == ARCH_X86) {
173                 /* The first part of the x86 vmlinuz is a header, followed by
174                  * a real-mode boot stub. We only want the 32-bit part. */
175                 lh = (struct linux_kernel_params *)kernel_buf;
176                 kernel32_start = (lh->setup_sects + 1) << 9;
177                 if (kernel32_start >= kernel_size) {
178                         fprintf(stderr, "Malformed kernel\n");
179                         return -1;
180                 }
181                 kernel32_size = kernel_size - kernel32_start;
182
183                 Debug(" kernel16_start=0x%" PRIx64 "\n", 0);
184                 Debug(" kernel16_size=0x%" PRIx64 "\n", kernel32_start);
185
186                 /* Copy the original zeropage data from kernel_buf into
187                  * g_param_data, then tweak a few fields for our purposes */
188                 params = (struct linux_kernel_params *)(g_param_data);
189                 Memcpy(&(params->setup_sects), &(lh->setup_sects),
190                        offsetof(struct linux_kernel_params, e820_entries)
191                        - offsetof(struct linux_kernel_params, setup_sects));
192                 params->boot_flag = 0;
193                 params->ramdisk_image = 0;      /* we don't support initrd */
194                 params->ramdisk_size = 0;
195                 params->type_of_loader = 0xff;
196                 /* We need to point to the kernel commandline arg. On disk, it
197                  * will come right after the 32-bit part of the kernel. */
198                 params->cmd_line_ptr = kernel_body_load_address +
199                         roundup(kernel32_size, CROS_ALIGN) +
200                         find_cmdline_start(g_config_data, g_config_size);
201                 Debug(" cmdline_addr=0x%x\n", params->cmd_line_ptr);
202                 Debug(" version=0x%x\n", params->version);
203                 Debug(" kernel_alignment=0x%x\n", params->kernel_alignment);
204                 Debug(" relocatable_kernel=0x%x\n", params->relocatable_kernel);
205                 /* Add a fake e820 memory map with 2 entries. */
206                 params->n_e820_entry = 2;
207                 params->e820_entries[0].start_addr = 0x00000000;
208                 params->e820_entries[0].segment_size = 0x00001000;
209                 params->e820_entries[0].segment_type = E820_TYPE_RAM;
210                 params->e820_entries[1].start_addr = 0xfffff000;
211                 params->e820_entries[1].segment_size = 0x00001000;
212                 params->e820_entries[1].segment_type = E820_TYPE_RESERVED;
213         }
214
215         Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
216         Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
217
218         /* Keep just the 32-bit kernel. */
219         if (kernel32_size) {
220                 g_kernel_size = kernel32_size;
221                 Memcpy(g_kernel_data, kernel_buf + kernel32_start,
222                        g_kernel_size);
223         }
224
225         /* done */
226         return 0;
227 }
228
229 /* Split a kernel blob into separate g_kernel, g_param, g_config,
230  * g_bootloader, and g_vmlinuz_header parts. */
231 static void UnpackKernelBlob(uint8_t *kernel_blob_data)
232 {
233         uint64_t now;
234         uint64_t vmlinuz_header_size = 0;
235         uint64_t vmlinuz_header_address = 0;
236
237         /* We have to work backwards from the end, because the preamble
238            only describes the bootloader and vmlinuz stubs. */
239
240         /* Vmlinuz Header is at the end */
241         if (VbGetKernelVmlinuzHeader(g_preamble,
242                                      &vmlinuz_header_address,
243                                      &vmlinuz_header_size)
244             != VBOOT_SUCCESS) {
245                 fprintf(stderr, "Unable to retrieve Vmlinuz Header!");
246                 return;
247         }
248         if (vmlinuz_header_size) {
249                 now = vmlinuz_header_address - g_preamble->body_load_address;
250                 g_vmlinuz_header_size = vmlinuz_header_size;
251                 g_vmlinuz_header_data = kernel_blob_data + now;
252
253                 Debug("vmlinuz_header_size     = 0x%" PRIx64 "\n",
254                       g_vmlinuz_header_size);
255                 Debug("vmlinuz_header_ofs      = 0x%" PRIx64 "\n", now);
256         }
257
258         /* Where does the bootloader stub begin? */
259         now = g_preamble->bootloader_address - g_preamble->body_load_address;
260
261         /* Bootloader is at the end */
262         g_bootloader_size = g_preamble->bootloader_size;
263         g_bootloader_data = kernel_blob_data + now;
264         /* TODO: What to do if this is beyond the end of the blob? */
265
266         Debug("bootloader_size     = 0x%" PRIx64 "\n", g_bootloader_size);
267         Debug("bootloader_ofs      = 0x%" PRIx64 "\n", now);
268
269         /* Before that is the params */
270         now -= CROS_PARAMS_SIZE;
271         g_param_size = CROS_PARAMS_SIZE;
272         g_param_data = kernel_blob_data + now;
273         Debug("param_ofs           = 0x%" PRIx64 "\n", now);
274
275         /* Before that is the config */
276         now -= CROS_CONFIG_SIZE;
277         g_config_size = CROS_CONFIG_SIZE;
278         g_config_data = kernel_blob_data + now;
279         Debug("config_ofs          = 0x%" PRIx64 "\n", now);
280
281         /* The kernel starts at offset 0 and extends up to the config */
282         g_kernel_data = kernel_blob_data;
283         g_kernel_size = now;
284         Debug("kernel_size         = 0x%" PRIx64 "\n", g_kernel_size);
285 }
286
287
288 /* Replaces the config section of the specified kernel blob.
289  * Return nonzero on error. */
290 int UpdateKernelBlobConfig(uint8_t *kblob_data, uint64_t kblob_size,
291                            uint8_t *config_data, uint64_t config_size)
292 {
293         /* We should have already examined this blob. If not, we could do it
294          * again, but it's more likely due to an error. */
295         if (kblob_data != g_kernel_blob_data ||
296             kblob_size != g_kernel_blob_size) {
297                 fprintf(stderr, "Trying to update some other blob\n");
298                 return -1;
299         }
300
301         Memset(g_config_data, 0, g_config_size);
302         Memcpy(g_config_data, config_data, config_size);
303
304         return 0;
305 }
306
307 /* Split a kernel partition into separate vblock and blob parts. */
308 uint8_t *UnpackKPart(uint8_t *kpart_data, uint64_t kpart_size,
309                      uint64_t padding,
310                      struct vb2_keyblock **keyblock_ptr,
311                      VbKernelPreambleHeader **preamble_ptr,
312                      uint64_t *blob_size_ptr)
313 {
314         VbKernelPreambleHeader *preamble;
315         uint64_t vmlinuz_header_size = 0;
316         uint64_t vmlinuz_header_address = 0;
317         uint64_t now = 0;
318         uint32_t flags = 0;
319
320         /* Sanity-check the keyblock */
321         struct vb2_keyblock *keyblock = (struct vb2_keyblock *)kpart_data;
322         Debug("Keyblock is 0x%x bytes\n", keyblock->keyblock_size);
323         now += keyblock->keyblock_size;
324         if (now > kpart_size) {
325                 fprintf(stderr,
326                         "keyblock_size advances past the end of the blob\n");
327                 return NULL;
328         }
329         if (now > padding) {
330                 fprintf(stderr,
331                         "keyblock_size advances past %" PRIu64
332                         " byte padding\n",
333                         padding);
334                 return NULL;
335         }
336
337         /* LGTM */
338         g_keyblock = keyblock;
339
340         /* And the preamble */
341         preamble = (VbKernelPreambleHeader *)(kpart_data + now);
342         Debug("Preamble is 0x%" PRIx64 " bytes\n", preamble->preamble_size);
343         now += preamble->preamble_size;
344         if (now > kpart_size) {
345                 fprintf(stderr,
346                         "preamble_size advances past the end of the blob\n");
347                 return NULL;
348         }
349         if (now > padding) {
350                 fprintf(stderr, "preamble_size advances past %" PRIu64
351                         " byte padding\n", padding);
352                 return NULL;
353         }
354         /* LGTM */
355         Debug(" kernel_version = %d\n", preamble->kernel_version);
356         Debug(" bootloader_address = 0x%" PRIx64 "\n",
357               preamble->bootloader_address);
358         Debug(" bootloader_size = 0x%" PRIx64 "\n", preamble->bootloader_size);
359         Debug(" kern_blob_size = 0x%" PRIx64 "\n",
360               preamble->body_signature.data_size);
361
362         if (VbKernelHasFlags(preamble) == VBOOT_SUCCESS)
363                 flags = preamble->flags;
364         Debug(" flags = 0x%" PRIx32 "\n", flags);
365
366         g_preamble = preamble;
367         g_ondisk_bootloader_addr = g_preamble->bootloader_address;
368
369         if (VbGetKernelVmlinuzHeader(preamble,
370                                      &vmlinuz_header_address,
371                                      &vmlinuz_header_size)
372             != VBOOT_SUCCESS) {
373                 fprintf(stderr, "Unable to retrieve Vmlinuz Header!");
374                 return NULL;
375         }
376         if (vmlinuz_header_size) {
377                 Debug(" vmlinuz_header_address = 0x%" PRIx64 "\n",
378                       vmlinuz_header_address);
379                 Debug(" vmlinuz_header_size = 0x%" PRIx64 "\n",
380                       vmlinuz_header_size);
381                 g_ondisk_vmlinuz_header_addr = vmlinuz_header_address;
382         }
383
384         Debug("kernel blob is at offset 0x%" PRIx64 "\n", now);
385         g_kernel_blob_data = kpart_data + now;
386         g_kernel_blob_size = preamble->body_signature.data_size;
387
388         /* Sanity check */
389         if (g_kernel_blob_size < preamble->body_signature.data_size)
390                 fprintf(stderr,
391                         "Warning: kernel file only has 0x%" PRIx64 " bytes\n",
392                         g_kernel_blob_size);
393
394         /* Update the blob pointers */
395         UnpackKernelBlob(g_kernel_blob_data);
396
397         if (keyblock_ptr)
398                 *keyblock_ptr = keyblock;
399         if (preamble_ptr)
400                 *preamble_ptr = preamble;
401         if (blob_size_ptr)
402                 *blob_size_ptr = g_kernel_blob_size;
403
404         return g_kernel_blob_data;
405 }
406
407 uint8_t *SignKernelBlob(uint8_t *kernel_blob, uint64_t kernel_size,
408                         uint64_t padding,
409                         int version, uint64_t kernel_body_load_address,
410                         struct vb2_keyblock *keyblock,
411                         VbPrivateKey *signpriv_key,
412                         uint32_t flags, uint64_t *vblock_size_ptr)
413 {
414         VbSignature *body_sig;
415         VbKernelPreambleHeader *preamble;
416         uint64_t min_size = padding > keyblock->keyblock_size
417                 ? padding - keyblock->keyblock_size : 0;
418         void *outbuf;
419         uint64_t outsize;
420
421         /* Sign the kernel data */
422         body_sig = CalculateSignature(kernel_blob, kernel_size, signpriv_key);
423         if (!body_sig) {
424                 fprintf(stderr, "Error calculating body signature\n");
425                 return NULL;
426         }
427
428         /* Create preamble */
429         preamble = CreateKernelPreamble(version,
430                                         kernel_body_load_address,
431                                         g_ondisk_bootloader_addr,
432                                         g_bootloader_size,
433                                         body_sig,
434                                         g_ondisk_vmlinuz_header_addr,
435                                         g_vmlinuz_header_size,
436                                         flags,
437                                         min_size,
438                                         signpriv_key);
439         if (!preamble) {
440                 fprintf(stderr, "Error creating preamble.\n");
441                 return 0;
442         }
443
444         outsize = keyblock->keyblock_size + preamble->preamble_size;
445         outbuf = malloc(outsize);
446         Memset(outbuf, 0, outsize);
447         Memcpy(outbuf, keyblock, keyblock->keyblock_size);
448         Memcpy(outbuf + keyblock->keyblock_size,
449                preamble, preamble->preamble_size);
450
451         if (vblock_size_ptr)
452                 *vblock_size_ptr = outsize;
453         return outbuf;
454 }
455
456 /* Returns zero on success */
457 int WriteSomeParts(const char *outfile,
458                    void *part1_data, uint64_t part1_size,
459                    void *part2_data, uint64_t part2_size)
460 {
461         FILE *f;
462
463         /* Write the output file */
464         Debug("writing %s with 0x%" PRIx64 ", 0x%" PRIx64 "\n",
465               outfile, part1_size, part2_size);
466
467         f = fopen(outfile, "wb");
468         if (!f) {
469                 fprintf(stderr, "Can't open output file %s: %s\n",
470                         outfile, strerror(errno));
471                 return -1;
472         }
473
474         if (part1_data && part1_size) {
475                 if (1 != fwrite(part1_data, part1_size, 1, f)) {
476                         fprintf(stderr, "Can't write output file %s: %s\n",
477                                 outfile, strerror(errno));
478                         fclose(f);
479                         unlink(outfile);
480                         return -1;
481                 }
482         }
483
484         if (part2_data && part2_size) {
485                 if (1 != fwrite(part2_data, part2_size, 1, f)) {
486                         fprintf(stderr, "Can't write output file %s: %s\n",
487                                 outfile, strerror(errno));
488                         fclose(f);
489                         unlink(outfile);
490                         return -1;
491                 }
492         }
493
494         fclose(f);
495
496         /* Success */
497         return 0;
498 }
499
500 /* Returns 0 on success */
501 int VerifyKernelBlob(uint8_t *kernel_blob,
502                      uint64_t kernel_size,
503                      VbPublicKey *signpub_key,
504                      const char *keyblock_outfile,
505                      uint64_t min_version)
506 {
507         RSAPublicKey *rsa;
508         int rv = -1;
509         uint64_t vmlinuz_header_size = 0;
510         uint64_t vmlinuz_header_address = 0;
511
512         static uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE];
513         static struct vb2_workbuf wb;
514         vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
515
516         if (signpub_key) {
517                 struct vb2_public_key pubkey;
518                 if (VB2_SUCCESS !=
519                     vb2_unpack_key(&pubkey,
520                                    (uint8_t *)signpub_key,
521                                    signpub_key->key_offset +
522                                    signpub_key->key_size)) {
523                         fprintf(stderr, "Error unpacking signing key.\n");
524                         goto done;
525                 }
526                 if (VB2_SUCCESS !=
527                     vb2_verify_keyblock(g_keyblock, g_keyblock->keyblock_size,
528                                         &pubkey, &wb)) {
529                         fprintf(stderr, "Error verifying key block.\n");
530                         goto done;
531                 }
532         } else if (VB2_SUCCESS !=
533                    vb2_verify_keyblock_hash(g_keyblock,
534                                             g_keyblock->keyblock_size,
535                                             &wb)) {
536                 fprintf(stderr, "Error verifying key block.\n");
537                 goto done;
538         }
539
540         printf("Key block:\n");
541         struct vb2_packed_key *data_key = &g_keyblock->data_key;
542         printf("  Signature:           %s\n",
543                signpub_key ? "valid" : "ignored");
544         printf("  Size:                0x%x\n", g_keyblock->keyblock_size);
545         printf("  Flags:               %u ", g_keyblock->keyblock_flags);
546         if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_DEVELOPER_0)
547                 printf(" !DEV");
548         if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_DEVELOPER_1)
549                 printf(" DEV");
550         if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_RECOVERY_0)
551                 printf(" !REC");
552         if (g_keyblock->keyblock_flags & KEY_BLOCK_FLAG_RECOVERY_1)
553                 printf(" REC");
554         printf("\n");
555         printf("  Data key algorithm:  %u %s\n", data_key->algorithm,
556                (data_key->algorithm < kNumAlgorithms ?
557                 algo_strings[data_key->algorithm] : "(invalid)"));
558         printf("  Data key version:    %u\n", data_key->key_version);
559         printf("  Data key sha1sum:    %s\n",
560                packed_key_sha1_string(data_key));
561
562         if (keyblock_outfile) {
563                 FILE *f = NULL;
564                 f = fopen(keyblock_outfile, "wb");
565                 if (!f)  {
566                         fprintf(stderr, "Can't open key block file %s: %s\n",
567                                 keyblock_outfile, strerror(errno));
568                         goto done;
569                 }
570                 if (1 != fwrite(g_keyblock, g_keyblock->keyblock_size, 1, f)) {
571                         fprintf(stderr, "Can't write key block file %s: %s\n",
572                                 keyblock_outfile, strerror(errno));
573                         fclose(f);
574                         goto done;
575                 }
576                 fclose(f);
577         }
578
579         if (data_key->key_version < (min_version >> 16)) {
580                 fprintf(stderr, "Data key version %u is lower than minimum "
581                         "%" PRIu64 ".\n",
582                         data_key->key_version, (min_version >> 16));
583                 goto done;
584         }
585
586         rsa = PublicKeyToRSA((VbPublicKey *)data_key);
587         if (!rsa) {
588                 fprintf(stderr, "Error parsing data key.\n");
589                 goto done;
590         }
591
592         /* Verify preamble */
593         if (0 != VerifyKernelPreamble(g_preamble,
594                                       g_preamble->preamble_size, rsa)) {
595                 fprintf(stderr, "Error verifying preamble.\n");
596                 goto done;
597         }
598
599         printf("Preamble:\n");
600         printf("  Size:                0x%" PRIx64 "\n",
601                g_preamble->preamble_size);
602         printf("  Header version:      %" PRIu32 ".%" PRIu32 "\n",
603                g_preamble->header_version_major,
604                g_preamble->header_version_minor);
605         printf("  Kernel version:      %" PRIu64 "\n",
606                g_preamble->kernel_version);
607         printf("  Body load address:   0x%" PRIx64 "\n",
608                g_preamble->body_load_address);
609         printf("  Body size:           0x%" PRIx64 "\n",
610                g_preamble->body_signature.data_size);
611         printf("  Bootloader address:  0x%" PRIx64 "\n",
612                g_preamble->bootloader_address);
613         printf("  Bootloader size:     0x%" PRIx64 "\n",
614                g_preamble->bootloader_size);
615
616         if (VbGetKernelVmlinuzHeader(g_preamble,
617                                      &vmlinuz_header_address,
618                                      &vmlinuz_header_size)
619             != VBOOT_SUCCESS) {
620                 fprintf(stderr, "Unable to retrieve Vmlinuz Header!");
621                 goto done;
622         }
623         if (vmlinuz_header_size) {
624                 printf("  Vmlinuz header address: 0x%" PRIx64 "\n",
625                        vmlinuz_header_address);
626                 printf("  Vmlinuz header size:    0x%" PRIx64 "\n",
627                        vmlinuz_header_size);
628         }
629
630         if (VbKernelHasFlags(g_preamble) == VBOOT_SUCCESS)
631                 printf("  Flags          :       0x%" PRIx32 "\n",
632                        g_preamble->flags);
633
634         if (g_preamble->kernel_version < (min_version & 0xFFFF)) {
635                 fprintf(stderr,
636                         "Kernel version %" PRIu64 " is lower than minimum %"
637                         PRIu64 ".\n", g_preamble->kernel_version,
638                         (min_version & 0xFFFF));
639                 goto done;
640         }
641
642         /* Verify body */
643         if (0 != VerifyData(kernel_blob, kernel_size,
644                             &g_preamble->body_signature, rsa)) {
645                 fprintf(stderr, "Error verifying kernel body.\n");
646                 goto done;
647         }
648         printf("Body verification succeeded.\n");
649
650         printf("Config:\n%s\n", kernel_blob + kernel_cmd_line_offset(
651                      (struct vb2_kernel_preamble *)g_preamble));
652
653         rv = 0;
654 done:
655         return rv;
656 }
657
658
659 uint8_t *CreateKernelBlob(uint8_t *vmlinuz_buf, uint64_t vmlinuz_size,
660                           enum arch_t arch, uint64_t kernel_body_load_address,
661                           uint8_t *config_data, uint64_t config_size,
662                           uint8_t *bootloader_data, uint64_t bootloader_size,
663                           uint64_t *blob_size_ptr)
664 {
665         uint64_t now = 0;
666         int tmp;
667
668         /* We have all the parts. How much room do we need? */
669         tmp = KernelSize(vmlinuz_buf, vmlinuz_size, arch);
670         if (tmp < 0)
671                 return NULL;
672         g_kernel_size = tmp;
673         g_config_size = CROS_CONFIG_SIZE;
674         g_param_size = CROS_PARAMS_SIZE;
675         g_bootloader_size = roundup(bootloader_size, CROS_ALIGN);
676         g_vmlinuz_header_size = vmlinuz_size-g_kernel_size;
677         g_kernel_blob_size =
678                 roundup(g_kernel_size, CROS_ALIGN) +
679                 g_config_size                      +
680                 g_param_size                       +
681                 g_bootloader_size                  +
682                 g_vmlinuz_header_size;
683         Debug("g_kernel_blob_size  0x%" PRIx64 "\n", g_kernel_blob_size);
684
685         /* Allocate space for the blob. */
686         g_kernel_blob_data = malloc(g_kernel_blob_size);
687         Memset(g_kernel_blob_data, 0, g_kernel_blob_size);
688
689         /* Assign the sub-pointers */
690         g_kernel_data = g_kernel_blob_data + now;
691         Debug("g_kernel_size       0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
692               g_kernel_size, now);
693         now += roundup(g_kernel_size, CROS_ALIGN);
694
695         g_config_data = g_kernel_blob_data + now;
696         Debug("g_config_size       0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
697               g_config_size, now);
698         now += g_config_size;
699
700         g_param_data = g_kernel_blob_data + now;
701         Debug("g_param_size        0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
702               g_param_size, now);
703         now += g_param_size;
704
705         g_bootloader_data = g_kernel_blob_data + now;
706         Debug("g_bootloader_size   0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
707               g_bootloader_size, now);
708         g_ondisk_bootloader_addr = kernel_body_load_address + now;
709         Debug("g_ondisk_bootloader_addr   0x%" PRIx64 "\n",
710               g_ondisk_bootloader_addr);
711         now += g_bootloader_size;
712
713         if (g_vmlinuz_header_size) {
714                 g_vmlinuz_header_data = g_kernel_blob_data + now;
715                 Debug("g_vmlinuz_header_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
716                       g_vmlinuz_header_size, now);
717                 g_ondisk_vmlinuz_header_addr = kernel_body_load_address + now;
718                 Debug("g_ondisk_vmlinuz_header_addr   0x%" PRIx64 "\n",
719                       g_ondisk_vmlinuz_header_addr);
720         }
721
722         Debug("end of kern_blob at kern_blob+0x%" PRIx64 "\n", now);
723
724         /* Copy the kernel and params bits into the correct places */
725         if (0 != PickApartVmlinuz(vmlinuz_buf, vmlinuz_size,
726                                   arch, kernel_body_load_address)) {
727                 fprintf(stderr, "Error picking apart kernel file.\n");
728                 free(g_kernel_blob_data);
729                 g_kernel_blob_data = NULL;
730                 g_kernel_blob_size = 0;
731                 return NULL;
732         }
733
734         /* Copy the other bits too */
735         Memcpy(g_config_data, config_data, config_size);
736         Memcpy(g_bootloader_data, bootloader_data, bootloader_size);
737         if (g_vmlinuz_header_size) {
738                 Memcpy(g_vmlinuz_header_data,
739                        vmlinuz_buf,
740                        g_vmlinuz_header_size);
741         }
742
743         if (blob_size_ptr)
744                 *blob_size_ptr = g_kernel_blob_size;
745         return g_kernel_blob_data;
746 }
747
748 enum futil_file_type ft_recognize_vblock1(uint8_t *buf, uint32_t len)
749 {
750         int rv;
751
752         uint8_t workbuf[VB2_WORKBUF_RECOMMENDED_SIZE];
753         struct vb2_workbuf wb;
754         vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
755
756         /* Vboot 2.0 signature checks destroy the buffer, so make a copy */
757         uint8_t *buf2 = malloc(len);
758         memcpy(buf2, buf, len);
759
760         struct vb2_keyblock *keyblock = (struct vb2_keyblock *)buf;
761         if (VB2_SUCCESS != vb2_verify_keyblock_hash(keyblock, len, &wb)) {
762                 free(buf2);
763                 return FILE_TYPE_UNKNOWN;
764         }
765
766         /* Try unpacking the data key from the keyblock */
767         struct vb2_public_key data_key;
768         if (VB2_SUCCESS !=
769             vb2_unpack_key(&data_key, (const uint8_t *)&keyblock->data_key,
770                            keyblock->data_key.key_offset +
771                            keyblock->data_key.key_size)) {
772                 /* It looks like a bad keyblock, but still a keyblock */
773                 free(buf2);
774                 return FILE_TYPE_KEYBLOCK;
775         }
776
777         uint32_t more = keyblock->keyblock_size;
778
779         /* Followed by firmware preamble too? */
780         struct vb2_fw_preamble *pre2 = (struct vb2_fw_preamble *)(buf2 + more);
781         rv = vb2_verify_fw_preamble(pre2, len - more, &data_key, &wb);
782         free(buf2);
783         if (VB2_SUCCESS == rv)
784                 return FILE_TYPE_FW_PREAMBLE;
785
786         /* Or maybe kernel preamble? */
787         RSAPublicKey *rsa = PublicKeyToRSA((VbPublicKey *)&keyblock->data_key);
788         VbKernelPreambleHeader *kern_preamble =
789                 (VbKernelPreambleHeader *)(buf + more);
790         if (VBOOT_SUCCESS ==
791             VerifyKernelPreamble(kern_preamble, len - more, rsa))
792                 return FILE_TYPE_KERN_PREAMBLE;
793
794         /* No, just keyblock */
795         return FILE_TYPE_KEYBLOCK;
796 }
797
798 enum futil_file_type ft_recognize_vb1_key(uint8_t *buf, uint32_t len)
799 {
800         struct vb2_packed_key *pubkey = (struct vb2_packed_key *)buf;
801         VbPrivateKey key;
802         const unsigned char *start;
803
804         /* Maybe just a VbPublicKey? */
805         if (packed_key_looks_ok(pubkey, len))
806                 return FILE_TYPE_PUBKEY;
807
808         /* How about a VbPrivateKey? */
809         if (len < sizeof(key.algorithm))
810                 return FILE_TYPE_UNKNOWN;
811
812         key.algorithm = *(typeof(key.algorithm) *)buf;
813         start = buf + sizeof(key.algorithm);
814         key.rsa_private_key = d2i_RSAPrivateKey(NULL, &start,
815                                                 len - sizeof(key.algorithm));
816
817         if (key.rsa_private_key) {
818                 RSA_free(key.rsa_private_key);
819                 return FILE_TYPE_PRIVKEY;
820         }
821
822         return FILE_TYPE_UNKNOWN;
823 }