aboutsummaryrefslogtreecommitdiff
path: root/bvb_verify_unittest.cc
blob: ebabd8ddfc98dc5ea46c9363d99b319bc2214c52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <iostream>

#include <endian.h>
#include <inttypes.h>
#include <string.h>

#include <base/files/file_util.h>
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>

#include "bvb_unittest_util.h"
#include "bvb_refimpl.h"

class VerifyTest : public BaseBvbToolTest {
public:
  VerifyTest() {}

protected:

  // Helper function for ModificationDetection test. Modifies
  // boot_image_ in a number of places in the sub-array at |offset| of
  // size |length| and checks that bvb_verify_boot_image() returns
  // |expected_result|.
  bool test_modification(BvbVerifyResult expected_result,
                         size_t offset, size_t length);

};

TEST_F(VerifyTest, BootImageStructSize) {
  EXPECT_EQ(8192UL, sizeof(BvbBootImageHeader));
}

TEST_F(VerifyTest, CheckSHA256RSA2048) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, CheckSHA256RSA4096) {
  GenerateBootImage("SHA256_RSA4096", "", 0
                    , base::FilePath("test/testkey_rsa4096.pem"));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, CheckSHA256RSA8192) {
  GenerateBootImage("SHA256_RSA8192", "", 0,
                    base::FilePath("test/testkey_rsa8192.pem"));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, CheckSHA512RSA2048) {
  GenerateBootImage("SHA512_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, CheckSHA512RSA4096) {
  GenerateBootImage("SHA512_RSA4096", "", 0,
                    base::FilePath("test/testkey_rsa4096.pem"));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, CheckSHA512RSA8192) {
  GenerateBootImage("SHA512_RSA8192", "", 0,
                    base::FilePath("test/testkey_rsa8192.pem"));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, CheckBiggerLength) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));
  // Check that it's OK if we pass a bigger length than what the
  // header indicates.
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(),
                                  boot_image_.size() + 8192,
                                  NULL, NULL));
}

TEST_F(VerifyTest, CheckUnsigned) {
  GenerateBootImage("", "", 0, base::FilePath(""));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK_NOT_SIGNED,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, BadMagic) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));
  boot_image_[0] = 'A';
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}


TEST_F(VerifyTest, MajorVersionCheck) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());
  h->header_version_major = htobe32(1 + be32toh(h->header_version_major));
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}


TEST_F(VerifyTest, MinorVersionCheck) {
  GenerateBootImage("", "", 0, base::FilePath(""));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());
  h->header_version_minor = htobe32(1 + be32toh(h->header_version_minor));
  EXPECT_EQ(BVB_VERIFY_RESULT_OK_NOT_SIGNED,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, BlockSizesAddUpToLessThanLength) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());
  BvbBootImageHeader backup = *h;

  // Check that the sum of the three block lengths is less than passed
  // in size. Use a size that's a multiple of 64 to avoid failure on
  // earlier check.
  uint64_t size = boot_image_.size() & (~0x3f);

  h->authentication_data_block_size = htobe64(size);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
  *h = backup;

  h->auxilary_data_block_size = htobe64(size);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
  *h = backup;

  h->payload_data_block_size = htobe64(size);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
  *h = backup;

  // Overflow checks - choose overflow candidate so it's a multiple of
  // 64 otherwise we'll fail on an earlier check.
  size = 0xffffffffffffffc0UL;

  h->authentication_data_block_size = htobe64(size);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
  *h = backup;

  h->auxilary_data_block_size = htobe64(size);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
  *h = backup;

  h->payload_data_block_size = htobe64(size);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
  *h = backup;

  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, BlockSizesMultipleOf64) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());
  BvbBootImageHeader backup = *h;

  h->authentication_data_block_size =
      htobe32(be32toh(h->authentication_data_block_size) - 32);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size() - 32,
                                  NULL, NULL));
  *h = backup;

  h->auxilary_data_block_size =
      htobe32(be32toh(h->auxilary_data_block_size) - 32);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size() - 32,
                                  NULL, NULL));
  *h = backup;

  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, HashOutOfBounds) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());

  // Check we catch when hash data goes out of bounds.
  h->hash_offset = htobe64(4);
  h->hash_size = htobe64(be64toh(h->authentication_data_block_size));
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));

  // Overflow checks.
  h->hash_offset = htobe64(4);
  h->hash_size = htobe64(0xfffffffffffffffeUL);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, SignatureOutOfBounds) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());

  // Check we catch when signature data goes out of bounds.
  h->signature_offset = htobe64(4);
  h->signature_size = htobe64(be64toh(h->authentication_data_block_size));
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));

  // Overflow checks.
  h->signature_offset = htobe64(4);
  h->signature_size = htobe64(0xfffffffffffffffeUL);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, PublicKeyOutOfBounds) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());

  // Check we catch when public key data goes out of bounds.
  h->public_key_offset = htobe64(4);
  h->public_key_size = htobe64(be64toh(h->auxilary_data_block_size));
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));

  // Overflow checks.
  h->public_key_offset = htobe64(4);
  h->public_key_size = htobe64(0xfffffffffffffffeUL);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, KernelOutOfBounds) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());

  // Check we catch when kernel data goes out of bounds.
  h->kernel_offset = htobe64(4);
  h->kernel_size = htobe64(be64toh(h->payload_data_block_size));
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));

  // Overflow checks.
  h->kernel_offset = htobe64(4);
  h->kernel_size = htobe64(0xfffffffffffffffeUL);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, InitrdOutOfBounds) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());

  // Check we catch when initrd data goes out of bounds.
  h->initrd_offset = htobe64(4);
  h->initrd_size = htobe64(be64toh(h->payload_data_block_size));
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));

  // Overflow checks.
  h->initrd_offset = htobe64(4);
  h->initrd_size = htobe64(0xfffffffffffffffeUL);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, InvalidAlgorithmField) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());
  BvbBootImageHeader backup = *h;

  // Check we bail on unknown algorithm.
  h->algorithm_type = htobe32(_BVB_ALGORITHM_NUM_TYPES);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
  *h = backup;
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

TEST_F(VerifyTest, PublicKeyBlockTooSmall) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  BvbBootImageHeader *h =
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data());
  BvbBootImageHeader backup = *h;

  // Check we bail if the auxilary data block is too small.
  uint64_t change = be64toh(h->auxilary_data_block_size) - 64;
  h->auxilary_data_block_size = htobe64(change);
  EXPECT_EQ(BVB_VERIFY_RESULT_INVALID_BOOT_IMAGE_HEADER,
            bvb_verify_boot_image(boot_image_.data(),
                                  boot_image_.size() - change,
                                  NULL, NULL));
  *h = backup;
  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));
}

bool VerifyTest::test_modification(BvbVerifyResult expected_result,
                                    size_t offset, size_t length) {
  uint8_t *d = reinterpret_cast<uint8_t*>(boot_image_.data());
  const int kNumCheckpoints = 16;

  // Test |kNumCheckpoints| modifications in the start, middle, and
  // end of given sub-array.
  for (int n = 0; n <= kNumCheckpoints; n++) {
    size_t o = std::min(length*n/kNumCheckpoints, length - 1) + offset;
    d[o] ^= 0x80;
    BvbVerifyResult result = bvb_verify_boot_image(boot_image_.data(),
                                                   boot_image_.size(),
                                                   NULL, NULL);
    d[o] ^= 0x80;
    if (result != expected_result)
      return false;
  }

  return true;
}

TEST_F(VerifyTest, ModificationDetection) {
  GenerateBootImage("SHA256_RSA2048", "", 0,
                    base::FilePath("test/testkey_rsa2048.pem"));

  EXPECT_EQ(BVB_VERIFY_RESULT_OK,
            bvb_verify_boot_image(boot_image_.data(), boot_image_.size(),
                                  NULL, NULL));

  BvbBootImageHeader h;
  bvb_boot_image_header_to_host_byte_order(
      reinterpret_cast<BvbBootImageHeader*>(boot_image_.data()), &h);

  size_t header_block_offset = 0;
  size_t authentication_block_offset = header_block_offset + sizeof(BvbBootImageHeader);
  size_t auxilary_block_offset = authentication_block_offset + h.authentication_data_block_size;
  size_t payload_block_offset = auxilary_block_offset + h.auxilary_data_block_size;

  // Ensure we detect modification of the header data block. Do this
  // in a field that's not validated so INVALID_BOOT_IMAGE_HEADER
  // isn't returned.
  EXPECT_TRUE(test_modification(BVB_VERIFY_RESULT_HASH_MISMATCH,
                                offsetof(BvbBootImageHeader, kernel_cmdline),
                                BVB_KERNEL_CMDLINE_MAX_LEN));
  // Also check the |reserved| field.
  EXPECT_TRUE(test_modification(BVB_VERIFY_RESULT_HASH_MISMATCH,
                                offsetof(BvbBootImageHeader, reserved),
                                sizeof(BvbBootImageHeader().reserved)));

  // Ensure we detect modifications in the auxilary data block.
  EXPECT_TRUE(test_modification(BVB_VERIFY_RESULT_HASH_MISMATCH,
                                auxilary_block_offset,
                                h.auxilary_data_block_size));

  // Ensure we detect modifications in the payload key data block.
  EXPECT_TRUE(test_modification(BVB_VERIFY_RESULT_HASH_MISMATCH,
                                payload_block_offset,
                                h.payload_data_block_size));

  // Modifications in the hash part of the Authentication data block
  // should also yield HASH_MISMATCH. This is because the hash check
  // compares the calculated hash against the stored hash.
  EXPECT_TRUE(test_modification(BVB_VERIFY_RESULT_HASH_MISMATCH,
                                authentication_block_offset + h.hash_offset,
                                h.hash_size));

  // Modifications in the signature part of the Authentication data
  // block, should not cause a hash mismatch ... but will cause a
  // signature mismatch.
  EXPECT_TRUE(test_modification(BVB_VERIFY_RESULT_SIGNATURE_MISMATCH,
                                authentication_block_offset +
                                  h.signature_offset,
                                h.signature_size));

  // Mofications outside the hash and signature parts of the
  // Authentication data block are not detected. This is because it's
  // not part of the hash calculation.
  uint64_t offset = h.signature_offset + h.signature_size;
  ASSERT_LT(h.hash_offset, h.signature_offset);
  ASSERT_LT(offset + 1, h.authentication_data_block_size);
  EXPECT_TRUE(test_modification(BVB_VERIFY_RESULT_OK,
                                authentication_block_offset + offset,
                                h.authentication_data_block_size - offset));
}