blob: 4f748e8624a27cce6118e0ce9e5fae56bcb59050 [file] [log] [blame]
Bill Richardson6f396152014-07-15 12:52:19 -07001/* Copyright 2011 The Chromium OS Authors. All rights reserved.
Randall Spanglerdcab8fa2010-06-15 14:50:51 -07002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Verified boot firmware utility
6 */
7
8#include <getopt.h>
Bill Richardson31d95c22014-08-24 22:07:17 -07009#include <inttypes.h> /* For PRIu64 */
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070010#include <stddef.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <unistd.h>
14
15#include "cryptolib.h"
Bill Richardson6f396152014-07-15 12:52:19 -070016#include "futility.h"
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070017#include "host_common.h"
18#include "kernel_blob.h"
Bill Richardson78299022014-06-20 14:33:00 -070019#include "util_misc.h"
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070020#include "vboot_common.h"
21
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070022/* Command line options */
23enum {
Bill Richardson31d95c22014-08-24 22:07:17 -070024 OPT_MODE_VBLOCK = 1000,
25 OPT_MODE_VERIFY,
26 OPT_KEYBLOCK,
27 OPT_SIGNPUBKEY,
28 OPT_SIGNPRIVATE,
29 OPT_VERSION,
30 OPT_FV,
31 OPT_KERNELKEY,
32 OPT_FLAGS,
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070033};
34
Mike Frysinger7351ed72014-08-18 10:47:42 -040035static const struct option long_opts[] = {
Bill Richardson31d95c22014-08-24 22:07:17 -070036 {"vblock", 1, 0, OPT_MODE_VBLOCK},
37 {"verify", 1, 0, OPT_MODE_VERIFY},
38 {"keyblock", 1, 0, OPT_KEYBLOCK},
39 {"signpubkey", 1, 0, OPT_SIGNPUBKEY},
40 {"signprivate", 1, 0, OPT_SIGNPRIVATE},
41 {"version", 1, 0, OPT_VERSION},
42 {"fv", 1, 0, OPT_FV},
43 {"kernelkey", 1, 0, OPT_KERNELKEY},
44 {"flags", 1, 0, OPT_FLAGS},
45 {NULL, 0, 0, 0}
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070046};
47
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070048/* Print help and return error */
Bill Richardson779796f2014-09-23 11:47:40 -070049static void print_help(const char *prog)
Bill Richardson31d95c22014-08-24 22:07:17 -070050{
Bill Richardson779796f2014-09-23 11:47:40 -070051 printf("\nUsage: " MYNAME " %s <--vblock|--verify> <file> [OPTIONS]\n"
52 "\n"
53 "For '--vblock <file>', required OPTIONS are:\n"
54 "\n"
55 " --keyblock <file> Key block in .keyblock format\n"
56 " --signprivate <file>"
57 " Signing private key in .vbprivk format\n"
58 " --version <number> Firmware version\n"
59 " --fv <file> Firmware volume to sign\n"
60 " --kernelkey <file> Kernel subkey in .vbpubk format\n"
61 "\n"
62 "optional OPTIONS are:\n"
63 " --flags <number> Preamble flags (defaults to 0)\n"
64 "\n"
65 "For '--verify <file>', required OPTIONS are:\n"
66 "\n"
67 " --signpubkey <file>"
68 " Signing public key in .vbpubk format\n"
69 " --fv <file> Firmware volume to verify\n"
70 "\n"
71 "For '--verify <file>', optional OPTIONS are:\n"
72 " --kernelkey <file>"
73 " Write the kernel subkey to this file\n\n",
74 prog);
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070075}
76
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070077/* Create a firmware .vblock */
Bill Richardson31d95c22014-08-24 22:07:17 -070078static int Vblock(const char *outfile, const char *keyblock_file,
79 const char *signprivate, uint64_t version,
80 const char *fv_file, const char *kernelkey_file,
81 uint32_t preamble_flags)
82{
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070083
Bill Richardson31d95c22014-08-24 22:07:17 -070084 VbPrivateKey *signing_key;
85 VbPublicKey *kernel_subkey;
86 VbSignature *body_sig;
87 VbFirmwarePreambleHeader *preamble;
88 VbKeyBlockHeader *key_block;
89 uint64_t key_block_size;
90 uint8_t *fv_data;
91 uint64_t fv_size;
92 FILE *f;
93 uint64_t i;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -070094
Bill Richardson31d95c22014-08-24 22:07:17 -070095 if (!outfile) {
96 VbExError("Must specify output filename\n");
97 return 1;
98 }
99 if (!keyblock_file || !signprivate || !kernelkey_file) {
100 VbExError("Must specify all keys\n");
101 return 1;
102 }
103 if (!fv_file) {
104 VbExError("Must specify firmware volume\n");
105 return 1;
106 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700107
Bill Richardson31d95c22014-08-24 22:07:17 -0700108 /* Read the key block and keys */
109 key_block =
110 (VbKeyBlockHeader *) ReadFile(keyblock_file, &key_block_size);
111 if (!key_block) {
112 VbExError("Error reading key block.\n");
113 return 1;
114 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700115
Bill Richardson31d95c22014-08-24 22:07:17 -0700116 signing_key = PrivateKeyRead(signprivate);
117 if (!signing_key) {
118 VbExError("Error reading signing key.\n");
119 return 1;
120 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700121
Bill Richardson31d95c22014-08-24 22:07:17 -0700122 kernel_subkey = PublicKeyRead(kernelkey_file);
123 if (!kernel_subkey) {
124 VbExError("Error reading kernel subkey.\n");
125 return 1;
126 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700127
Bill Richardson31d95c22014-08-24 22:07:17 -0700128 /* Read and sign the firmware volume */
129 fv_data = ReadFile(fv_file, &fv_size);
130 if (!fv_data)
131 return 1;
132 if (!fv_size) {
133 VbExError("Empty firmware volume file\n");
134 return 1;
135 }
136 body_sig = CalculateSignature(fv_data, fv_size, signing_key);
137 if (!body_sig) {
138 VbExError("Error calculating body signature\n");
139 return 1;
140 }
141 free(fv_data);
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700142
Bill Richardson31d95c22014-08-24 22:07:17 -0700143 /* Create preamble */
144 preamble = CreateFirmwarePreamble(version,
145 kernel_subkey,
146 body_sig,
147 signing_key, preamble_flags);
148 if (!preamble) {
149 VbExError("Error creating preamble.\n");
150 return 1;
151 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700152
Bill Richardson31d95c22014-08-24 22:07:17 -0700153 /* Write the output file */
154 f = fopen(outfile, "wb");
155 if (!f) {
156 VbExError("Can't open output file %s\n", outfile);
157 return 1;
158 }
159 i = ((1 != fwrite(key_block, key_block_size, 1, f)) ||
160 (1 != fwrite(preamble, preamble->preamble_size, 1, f)));
161 fclose(f);
162 if (i) {
163 VbExError("Can't write output file %s\n", outfile);
164 unlink(outfile);
165 return 1;
166 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700167
Bill Richardson31d95c22014-08-24 22:07:17 -0700168 /* Success */
169 return 0;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700170}
171
Bill Richardson31d95c22014-08-24 22:07:17 -0700172static int Verify(const char *infile, const char *signpubkey,
173 const char *fv_file, const char *kernelkey_file)
174{
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700175
Bill Richardson31d95c22014-08-24 22:07:17 -0700176 VbKeyBlockHeader *key_block;
177 VbFirmwarePreambleHeader *preamble;
178 VbPublicKey *data_key;
179 VbPublicKey *sign_key;
180 VbPublicKey *kernel_subkey;
181 RSAPublicKey *rsa;
182 uint8_t *blob;
183 uint64_t blob_size;
184 uint8_t *fv_data;
185 uint64_t fv_size;
186 uint64_t now = 0;
187 uint32_t flags;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700188
Bill Richardson31d95c22014-08-24 22:07:17 -0700189 if (!infile || !signpubkey || !fv_file) {
190 VbExError("Must specify filename, signpubkey, and fv\n");
191 return 1;
192 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700193
Bill Richardson31d95c22014-08-24 22:07:17 -0700194 /* Read public signing key */
195 sign_key = PublicKeyRead(signpubkey);
196 if (!sign_key) {
197 VbExError("Error reading signpubkey.\n");
198 return 1;
199 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700200
Bill Richardson31d95c22014-08-24 22:07:17 -0700201 /* Read blob */
202 blob = ReadFile(infile, &blob_size);
203 if (!blob) {
204 VbExError("Error reading input file\n");
205 return 1;
206 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700207
Bill Richardson31d95c22014-08-24 22:07:17 -0700208 /* Read firmware volume */
209 fv_data = ReadFile(fv_file, &fv_size);
210 if (!fv_data) {
211 VbExError("Error reading firmware volume\n");
212 return 1;
213 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700214
Bill Richardson31d95c22014-08-24 22:07:17 -0700215 /* Verify key block */
216 key_block = (VbKeyBlockHeader *) blob;
217 if (0 != KeyBlockVerify(key_block, blob_size, sign_key, 0)) {
218 VbExError("Error verifying key block.\n");
219 return 1;
220 }
221 free(sign_key);
222 now += key_block->key_block_size;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700223
Bill Richardson31d95c22014-08-24 22:07:17 -0700224 printf("Key block:\n");
225 data_key = &key_block->data_key;
226 printf(" Size: %" PRIu64 "\n",
227 key_block->key_block_size);
228 printf(" Flags: %" PRIu64 " (ignored)\n",
229 key_block->key_block_flags);
230 printf(" Data key algorithm: %" PRIu64 " %s\n", data_key->algorithm,
231 (data_key->algorithm <
232 kNumAlgorithms ? algo_strings[data_key->
233 algorithm] : "(invalid)"));
234 printf(" Data key version: %" PRIu64 "\n", data_key->key_version);
235 printf(" Data key sha1sum: ");
236 PrintPubKeySha1Sum(data_key);
237 printf("\n");
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700238
Bill Richardson31d95c22014-08-24 22:07:17 -0700239 rsa = PublicKeyToRSA(&key_block->data_key);
240 if (!rsa) {
241 VbExError("Error parsing data key.\n");
242 return 1;
243 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700244
Bill Richardson31d95c22014-08-24 22:07:17 -0700245 /* Verify preamble */
246 preamble = (VbFirmwarePreambleHeader *) (blob + now);
247 if (0 != VerifyFirmwarePreamble(preamble, blob_size - now, rsa)) {
248 VbExError("Error verifying preamble.\n");
249 return 1;
250 }
251 now += preamble->preamble_size;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700252
Bill Richardson31d95c22014-08-24 22:07:17 -0700253 flags = VbGetFirmwarePreambleFlags(preamble);
254 printf("Preamble:\n");
255 printf(" Size: %" PRIu64 "\n",
256 preamble->preamble_size);
257 printf(" Header version: %" PRIu32 ".%" PRIu32 "\n",
258 preamble->header_version_major, preamble->header_version_minor);
259 printf(" Firmware version: %" PRIu64 "\n",
260 preamble->firmware_version);
261 kernel_subkey = &preamble->kernel_subkey;
262 printf(" Kernel key algorithm: %" PRIu64 " %s\n",
263 kernel_subkey->algorithm,
264 (kernel_subkey->algorithm < kNumAlgorithms ?
265 algo_strings[kernel_subkey->algorithm] : "(invalid)"));
266 printf(" Kernel key version: %" PRIu64 "\n",
267 kernel_subkey->key_version);
268 printf(" Kernel key sha1sum: ");
269 PrintPubKeySha1Sum(kernel_subkey);
270 printf("\n");
271 printf(" Firmware body size: %" PRIu64 "\n",
272 preamble->body_signature.data_size);
273 printf(" Preamble flags: %" PRIu32 "\n", flags);
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700274
Bill Richardson31d95c22014-08-24 22:07:17 -0700275 /* TODO: verify body size same as signature size */
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700276
Bill Richardson31d95c22014-08-24 22:07:17 -0700277 /* Verify body */
278 if (flags & VB_FIRMWARE_PREAMBLE_USE_RO_NORMAL) {
279 printf
280 ("Preamble requests USE_RO_NORMAL;"
281 " skipping body verification.\n");
282 } else {
283 if (0 !=
284 VerifyData(fv_data, fv_size, &preamble->body_signature,
285 rsa)) {
286 VbExError("Error verifying firmware body.\n");
287 return 1;
288 }
289 printf("Body verification succeeded.\n");
290 }
Bill Richardson60bcbe32010-09-09 14:53:56 -0700291
Bill Richardson31d95c22014-08-24 22:07:17 -0700292 if (kernelkey_file) {
293 if (0 != PublicKeyWrite(kernelkey_file, kernel_subkey)) {
294 VbExError("Unable to write kernel subkey\n");
295 return 1;
296 }
297 }
Bill Richardson60bcbe32010-09-09 14:53:56 -0700298
Bill Richardson31d95c22014-08-24 22:07:17 -0700299 return 0;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700300}
301
Bill Richardson31d95c22014-08-24 22:07:17 -0700302static int do_vbutil_firmware(int argc, char *argv[])
303{
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700304
Bill Richardson31d95c22014-08-24 22:07:17 -0700305 char *filename = NULL;
306 char *key_block_file = NULL;
307 char *signpubkey = NULL;
308 char *signprivate = NULL;
309 uint64_t version = 0;
310 char *fv_file = NULL;
311 char *kernelkey_file = NULL;
312 uint32_t preamble_flags = 0;
313 int mode = 0;
314 int parse_error = 0;
315 char *e;
316 int i;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700317
Bill Richardson31d95c22014-08-24 22:07:17 -0700318 while ((i = getopt_long(argc, argv, "", long_opts, NULL)) != -1) {
319 switch (i) {
320 case '?':
321 /* Unhandled option */
322 printf("Unknown option\n");
323 parse_error = 1;
324 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700325
Bill Richardson31d95c22014-08-24 22:07:17 -0700326 case OPT_MODE_VBLOCK:
327 case OPT_MODE_VERIFY:
328 mode = i;
329 filename = optarg;
330 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700331
Bill Richardson31d95c22014-08-24 22:07:17 -0700332 case OPT_KEYBLOCK:
333 key_block_file = optarg;
334 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700335
Bill Richardson31d95c22014-08-24 22:07:17 -0700336 case OPT_SIGNPUBKEY:
337 signpubkey = optarg;
338 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700339
Bill Richardson31d95c22014-08-24 22:07:17 -0700340 case OPT_SIGNPRIVATE:
341 signprivate = optarg;
342 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700343
Bill Richardson31d95c22014-08-24 22:07:17 -0700344 case OPT_FV:
345 fv_file = optarg;
346 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700347
Bill Richardson31d95c22014-08-24 22:07:17 -0700348 case OPT_KERNELKEY:
349 kernelkey_file = optarg;
350 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700351
Bill Richardson31d95c22014-08-24 22:07:17 -0700352 case OPT_VERSION:
353 version = strtoul(optarg, &e, 0);
354 if (!*optarg || (e && *e)) {
355 printf("Invalid --version\n");
356 parse_error = 1;
357 }
358 break;
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700359
Bill Richardson31d95c22014-08-24 22:07:17 -0700360 case OPT_FLAGS:
361 preamble_flags = strtoul(optarg, &e, 0);
362 if (!*optarg || (e && *e)) {
363 printf("Invalid --flags\n");
364 parse_error = 1;
365 }
366 break;
367 }
368 }
Randall Spanglera712e012011-07-13 09:48:41 -0700369
Bill Richardson779796f2014-09-23 11:47:40 -0700370 if (parse_error) {
371 print_help(argv[0]);
372 return 1;
373 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700374
Bill Richardson31d95c22014-08-24 22:07:17 -0700375 switch (mode) {
376 case OPT_MODE_VBLOCK:
377 return Vblock(filename, key_block_file, signprivate, version,
378 fv_file, kernelkey_file, preamble_flags);
379 case OPT_MODE_VERIFY:
380 return Verify(filename, signpubkey, fv_file, kernelkey_file);
381 default:
Bill Richardson779796f2014-09-23 11:47:40 -0700382 fprintf(stderr, "Must specify a mode.\n");
383 print_help(argv[0]);
384 return 1;
Bill Richardson31d95c22014-08-24 22:07:17 -0700385 }
Randall Spanglerdcab8fa2010-06-15 14:50:51 -0700386}
Bill Richardson6f396152014-07-15 12:52:19 -0700387
388DECLARE_FUTIL_COMMAND(vbutil_firmware, do_vbutil_firmware,
Bill Richardson1eae8732015-02-05 12:36:15 -0800389 VBOOT_VERSION_1_0,
Bill Richardson779796f2014-09-23 11:47:40 -0700390 "Verified boot firmware utility",
391 print_help);