David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person |
| 5 | * obtaining a copy of this software and associated documentation |
| 6 | * files (the "Software"), to deal in the Software without |
| 7 | * restriction, including without limitation the rights to use, copy, |
| 8 | * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | * of the Software, and to permit persons to whom the Software is |
| 10 | * furnished to do so, subject to the following conditions: |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 11 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 14 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <iostream> |
| 26 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 27 | #include <base/files/file_util.h> |
| 28 | #include <base/strings/string_util.h> |
| 29 | #include <base/strings/stringprintf.h> |
| 30 | |
| 31 | #include "avb_unittest_util.h" |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 32 | #include "fake_avb_ops.h" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 33 | |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 34 | namespace avb { |
| 35 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 36 | class AvbSlotVerifyTest : public BaseAvbToolTest { |
| 37 | public: |
| 38 | AvbSlotVerifyTest() {} |
| 39 | |
| 40 | virtual void SetUp() override { |
| 41 | BaseAvbToolTest::SetUp(); |
| 42 | ops_.set_partition_dir(testdir_); |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 43 | ops_.set_stored_rollback_indexes({{0, 0}, {1, 0}, {2, 0}, {3, 0}}); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 44 | ops_.set_stored_is_device_unlocked(false); |
| 45 | } |
| 46 | |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 47 | void CmdlineWithHashtreeVerification(bool hashtree_verification_on); |
| 48 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 49 | FakeAvbOps ops_; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | TEST_F(AvbSlotVerifyTest, Basic) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 53 | GenerateVBMetaImage("vbmeta_a.img", |
| 54 | "SHA256_RSA2048", |
| 55 | 0, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 56 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 57 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 58 | |
| 59 | ops_.set_expected_public_key( |
| 60 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 61 | |
| 62 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 63 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 64 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 65 | avb_slot_verify(ops_.avb_ops(), |
| 66 | requested_partitions, |
| 67 | "_a", |
| 68 | false /* allow_verification_error */, |
| 69 | &slot_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 70 | EXPECT_NE(nullptr, slot_data); |
| 71 | EXPECT_EQ( |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 72 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 73 | "androidboot.vbmeta.avb_version=1.0 " |
| 74 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 75 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=1152 " |
David Zeuthen | 88b13e1 | 2016-10-03 17:24:55 -0400 | [diff] [blame] | 76 | "androidboot.vbmeta.digest=" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 77 | "4161a7e655eabe16c3fe714de5d43736e7c0a190cf08d36c946d2509ce071e4d", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 78 | std::string(slot_data->cmdline)); |
| 79 | avb_slot_verify_data_free(slot_data); |
| 80 | } |
| 81 | |
| 82 | TEST_F(AvbSlotVerifyTest, BasicSha512) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 83 | GenerateVBMetaImage("vbmeta_a.img", |
| 84 | "SHA512_RSA2048", |
| 85 | 0, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 86 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 87 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 88 | |
| 89 | ops_.set_expected_public_key( |
| 90 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 91 | |
| 92 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 93 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 94 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 95 | avb_slot_verify(ops_.avb_ops(), |
| 96 | requested_partitions, |
| 97 | "_a", |
| 98 | false /* allow_verification_error */, |
| 99 | &slot_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 100 | EXPECT_NE(nullptr, slot_data); |
| 101 | EXPECT_EQ( |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 102 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 103 | "androidboot.vbmeta.avb_version=1.0 " |
| 104 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 105 | "androidboot.vbmeta.hash_alg=sha512 androidboot.vbmeta.size=1152 " |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 106 | "androidboot.vbmeta.digest=" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 107 | "cb913d2f1a884f4e04c1db5bb181f3133fd16ac02fb367a20ef0776c0b07b3656ad1f081" |
| 108 | "e01932cf70f38b8960877470b448f1588dff022808387cc52fa77e77", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 109 | std::string(slot_data->cmdline)); |
| 110 | avb_slot_verify_data_free(slot_data); |
| 111 | } |
| 112 | |
| 113 | TEST_F(AvbSlotVerifyTest, BasicUnlocked) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 114 | GenerateVBMetaImage("vbmeta_a.img", |
| 115 | "SHA256_RSA2048", |
| 116 | 0, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 117 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 118 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 119 | |
| 120 | ops_.set_expected_public_key( |
| 121 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 122 | |
| 123 | ops_.set_stored_is_device_unlocked(true); |
| 124 | |
| 125 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 126 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 127 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 128 | avb_slot_verify(ops_.avb_ops(), |
| 129 | requested_partitions, |
| 130 | "_a", |
| 131 | false /* allow_verification_error */, |
| 132 | &slot_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 133 | EXPECT_NE(nullptr, slot_data); |
| 134 | EXPECT_EQ( |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 135 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 136 | "androidboot.vbmeta.avb_version=1.0 " |
| 137 | "androidboot.vbmeta.device_state=unlocked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 138 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=1152 " |
David Zeuthen | 88b13e1 | 2016-10-03 17:24:55 -0400 | [diff] [blame] | 139 | "androidboot.vbmeta.digest=" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 140 | "4161a7e655eabe16c3fe714de5d43736e7c0a190cf08d36c946d2509ce071e4d", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 141 | std::string(slot_data->cmdline)); |
| 142 | avb_slot_verify_data_free(slot_data); |
| 143 | } |
| 144 | |
| 145 | TEST_F(AvbSlotVerifyTest, SlotDataIsCorrect) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 146 | GenerateVBMetaImage("vbmeta_a.img", |
| 147 | "SHA256_RSA2048", |
| 148 | 0, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 149 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 150 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 151 | |
| 152 | ops_.set_expected_public_key( |
| 153 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 154 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 155 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 156 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 157 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 158 | avb_slot_verify(ops_.avb_ops(), |
| 159 | requested_partitions, |
| 160 | "_a", |
| 161 | false /* allow_verification_error */, |
| 162 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 163 | EXPECT_NE(nullptr, slot_data); |
| 164 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TEST_F(AvbSlotVerifyTest, WrongPublicKey) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 168 | GenerateVBMetaImage("vbmeta_a.img", |
| 169 | "SHA256_RSA2048", |
| 170 | 0, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 171 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 172 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 173 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 174 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 175 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 176 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 177 | avb_slot_verify(ops_.avb_ops(), |
| 178 | requested_partitions, |
| 179 | "_a", |
| 180 | false /* allow_verification_error */, |
| 181 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 182 | EXPECT_EQ(nullptr, slot_data); |
| 183 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 184 | avb_slot_verify(ops_.avb_ops(), |
| 185 | requested_partitions, |
| 186 | "_a", |
| 187 | true /* allow_verification_error */, |
| 188 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 189 | EXPECT_NE(nullptr, slot_data); |
| 190 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | TEST_F(AvbSlotVerifyTest, NoImage) { |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 194 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 195 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 196 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_IO, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 197 | avb_slot_verify(ops_.avb_ops(), |
| 198 | requested_partitions, |
| 199 | "_a", |
| 200 | false /* allow_verification_error */, |
| 201 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 202 | EXPECT_EQ(nullptr, slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | TEST_F(AvbSlotVerifyTest, UnsignedVBMeta) { |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 206 | GenerateVBMetaImage("vbmeta_a.img", |
| 207 | "", |
| 208 | 0, |
| 209 | base::FilePath(""), |
| 210 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 211 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 212 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 213 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 214 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 215 | avb_slot_verify(ops_.avb_ops(), |
| 216 | requested_partitions, |
| 217 | "_a", |
| 218 | false /* allow_verification_error */, |
| 219 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 220 | EXPECT_EQ(nullptr, slot_data); |
| 221 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 222 | avb_slot_verify(ops_.avb_ops(), |
| 223 | requested_partitions, |
| 224 | "_a", |
| 225 | true /* allow_verification_error */, |
| 226 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 227 | EXPECT_NE(nullptr, slot_data); |
| 228 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | TEST_F(AvbSlotVerifyTest, CorruptedImage) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 232 | GenerateVBMetaImage("vbmeta_a.img", |
| 233 | "SHA256_RSA2048", |
| 234 | 0, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 235 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 236 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 237 | |
| 238 | // Corrupt four bytes of data in the end of the image. Since the aux |
| 239 | // data is at the end and this data is signed, this will change the |
| 240 | // value of the computed hash. |
| 241 | uint8_t corrupt_data[4] = {0xff, 0xff, 0xff, 0xff}; |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 242 | EXPECT_EQ(AVB_IO_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 243 | ops_.avb_ops()->write_to_partition(ops_.avb_ops(), |
| 244 | "vbmeta_a", |
| 245 | -4, // offset from end |
| 246 | sizeof corrupt_data, |
| 247 | corrupt_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 248 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 249 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 250 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 251 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 252 | avb_slot_verify(ops_.avb_ops(), |
| 253 | requested_partitions, |
| 254 | "_a", |
| 255 | false /* allow_verification_error */, |
| 256 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 257 | EXPECT_EQ(nullptr, slot_data); |
| 258 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 259 | avb_slot_verify(ops_.avb_ops(), |
| 260 | requested_partitions, |
| 261 | "_a", |
| 262 | true /* allow_verification_error */, |
| 263 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 264 | EXPECT_NE(nullptr, slot_data); |
| 265 | avb_slot_verify_data_free(slot_data); |
| 266 | } |
| 267 | |
| 268 | TEST_F(AvbSlotVerifyTest, CorruptedMetadata) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 269 | GenerateVBMetaImage("vbmeta_a.img", |
| 270 | "SHA256_RSA2048", |
| 271 | 0, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 272 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 273 | "--internal_release_string \"\""); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 274 | |
| 275 | // Corrupt four bytes of data in the beginning of the image. Unlike |
| 276 | // the CorruptedImage test-case above (which is valid metadata) this |
| 277 | // will make the metadata invalid and render the slot unbootable |
| 278 | // even if the device is unlocked. Specifically no AvbSlotVerifyData |
| 279 | // is returned. |
| 280 | uint8_t corrupt_data[4] = {0xff, 0xff, 0xff, 0xff}; |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 281 | EXPECT_EQ(AVB_IO_RESULT_OK, |
| 282 | ops_.avb_ops()->write_to_partition(ops_.avb_ops(), |
| 283 | "vbmeta_a", |
| 284 | 0, // offset: beginning |
| 285 | sizeof corrupt_data, |
| 286 | corrupt_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 287 | |
| 288 | AvbSlotVerifyData* slot_data = NULL; |
| 289 | const char* requested_partitions[] = {"boot", NULL}; |
| 290 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 291 | avb_slot_verify(ops_.avb_ops(), |
| 292 | requested_partitions, |
| 293 | "_a", |
| 294 | false /* allow_verification_error */, |
| 295 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 296 | EXPECT_EQ(nullptr, slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | TEST_F(AvbSlotVerifyTest, RollbackIndex) { |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 300 | GenerateVBMetaImage("vbmeta_a.img", |
| 301 | "SHA256_RSA2048", |
| 302 | 42, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 303 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 304 | "--internal_release_string \"\""); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 305 | |
| 306 | ops_.set_expected_public_key( |
| 307 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 308 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 309 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 310 | const char* requested_partitions[] = {"boot", NULL}; |
| 311 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 312 | // First try with 42 as the stored rollback index - this should |
| 313 | // succeed since the image rollback index is 42 (as set above). |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 314 | ops_.set_stored_rollback_indexes({{0, 42}}); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 315 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 316 | avb_slot_verify(ops_.avb_ops(), |
| 317 | requested_partitions, |
| 318 | "_a", |
| 319 | false /* allow_verification_error */, |
| 320 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 321 | EXPECT_NE(nullptr, slot_data); |
| 322 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 323 | |
| 324 | // Then try with 43 for the stored rollback index - this should fail |
| 325 | // because the image has rollback index 42 which is less than 43. |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 326 | ops_.set_stored_rollback_indexes({{0, 43}}); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 327 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 328 | avb_slot_verify(ops_.avb_ops(), |
| 329 | requested_partitions, |
| 330 | "_a", |
| 331 | false /* allow_verification_error */, |
| 332 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 333 | EXPECT_EQ(nullptr, slot_data); |
| 334 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 335 | avb_slot_verify(ops_.avb_ops(), |
| 336 | requested_partitions, |
| 337 | "_a", |
| 338 | true /* allow_verification_error */, |
| 339 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 340 | EXPECT_NE(nullptr, slot_data); |
| 341 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | TEST_F(AvbSlotVerifyTest, HashDescriptorInVBMeta) { |
| 345 | const size_t boot_partition_size = 16 * 1024 * 1024; |
| 346 | const size_t boot_image_size = 5 * 1024 * 1024; |
| 347 | base::FilePath boot_path = GenerateImage("boot_a.img", boot_image_size); |
| 348 | |
| 349 | EXPECT_COMMAND( |
| 350 | 0, |
| 351 | "./avbtool add_hash_footer" |
| 352 | " --image %s" |
| 353 | " --rollback_index 0" |
| 354 | " --partition_name boot" |
| 355 | " --partition_size %zd" |
| 356 | " --kernel_cmdline 'cmdline in hash footer $(ANDROID_SYSTEM_PARTUUID)'" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 357 | " --salt deadbeef" |
| 358 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 359 | boot_path.value().c_str(), |
| 360 | boot_partition_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 361 | |
| 362 | GenerateVBMetaImage( |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 363 | "vbmeta_a.img", |
| 364 | "SHA256_RSA2048", |
| 365 | 4, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 366 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 367 | base::StringPrintf( |
| 368 | "--include_descriptors_from_image %s" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 369 | " --kernel_cmdline 'cmdline in vbmeta $(ANDROID_BOOT_PARTUUID)'" |
| 370 | " --internal_release_string \"\"", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 371 | boot_path.value().c_str())); |
| 372 | |
| 373 | EXPECT_EQ( |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 374 | "Minimum libavb version: 1.0\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 375 | "Header Block: 256 bytes\n" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 376 | "Authentication Block: 320 bytes\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 377 | "Auxiliary Block: 896 bytes\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 378 | "Algorithm: SHA256_RSA2048\n" |
| 379 | "Rollback Index: 4\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 380 | "Flags: 0\n" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 381 | "Release String: ''\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 382 | "Descriptors:\n" |
| 383 | " Kernel Cmdline descriptor:\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 384 | " Flags: 0\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 385 | " Kernel Cmdline: 'cmdline in vbmeta " |
| 386 | "$(ANDROID_BOOT_PARTUUID)'\n" |
| 387 | " Hash descriptor:\n" |
| 388 | " Image Size: 5242880 bytes\n" |
| 389 | " Hash Algorithm: sha256\n" |
| 390 | " Partition Name: boot\n" |
| 391 | " Salt: deadbeef\n" |
| 392 | " Digest: " |
| 393 | "184cb36243adb8b87d2d8c4802de32125fe294ec46753d732144ee65df68a23d\n" |
| 394 | " Kernel Cmdline descriptor:\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 395 | " Flags: 0\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 396 | " Kernel Cmdline: 'cmdline in hash footer " |
| 397 | "$(ANDROID_SYSTEM_PARTUUID)'\n", |
| 398 | InfoImage(vbmeta_image_path_)); |
| 399 | |
| 400 | EXPECT_COMMAND(0, |
| 401 | "./avbtool erase_footer" |
| 402 | " --image %s", |
| 403 | boot_path.value().c_str()); |
| 404 | |
| 405 | // With no footer, 'avbtool info_image' should fail (exit status 1). |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 406 | EXPECT_COMMAND( |
| 407 | 1, "./avbtool info_image --image %s", boot_path.value().c_str()); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 408 | |
| 409 | ops_.set_expected_public_key( |
| 410 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 411 | |
| 412 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 413 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 414 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 415 | avb_slot_verify(ops_.avb_ops(), |
| 416 | requested_partitions, |
| 417 | "_a", |
| 418 | false /* allow_verification_error */, |
| 419 | &slot_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 420 | EXPECT_NE(nullptr, slot_data); |
| 421 | |
| 422 | // Now verify the slot data. The vbmeta data should match our |
| 423 | // vbmeta_image_ member. |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 424 | EXPECT_EQ(size_t(1), slot_data->num_vbmeta_images); |
| 425 | EXPECT_EQ("vbmeta", std::string(slot_data->vbmeta_images[0].partition_name)); |
| 426 | EXPECT_EQ(slot_data->vbmeta_images[0].vbmeta_size, vbmeta_image_.size()); |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 427 | EXPECT_EQ(0, |
| 428 | memcmp(vbmeta_image_.data(), |
| 429 | slot_data->vbmeta_images[0].vbmeta_data, |
| 430 | slot_data->vbmeta_images[0].vbmeta_size)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 431 | |
| 432 | // The boot image data should match what is generated above with |
| 433 | // GenerateImage(). |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 434 | EXPECT_EQ(size_t(1), slot_data->num_loaded_partitions); |
| 435 | EXPECT_EQ("boot", |
| 436 | std::string(slot_data->loaded_partitions[0].partition_name)); |
| 437 | EXPECT_EQ(boot_image_size, slot_data->loaded_partitions[0].data_size); |
| 438 | for (size_t n = 0; n < slot_data->loaded_partitions[0].data_size; n++) { |
| 439 | EXPECT_EQ(slot_data->loaded_partitions[0].data[n], uint8_t(n)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // This should match the two cmdlines with a space (U+0020) between |
| 443 | // them and the $(ANDROID_SYSTEM_PARTUUID) and |
| 444 | // $(ANDROID_BOOT_PARTUUID) variables replaced. |
| 445 | EXPECT_EQ( |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 446 | "cmdline in vbmeta 1234-fake-guid-for:boot_a cmdline in hash footer " |
| 447 | "1234-fake-guid-for:system_a " |
| 448 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 449 | "androidboot.vbmeta.avb_version=1.0 " |
| 450 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 451 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=1472 " |
David Zeuthen | 88b13e1 | 2016-10-03 17:24:55 -0400 | [diff] [blame] | 452 | "androidboot.vbmeta.digest=" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 453 | "34cdb59b955aa35d4da97701f304fabf7392eecca8c50ff1a0b7b6e1c9aaa1b8", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 454 | std::string(slot_data->cmdline)); |
| 455 | EXPECT_EQ(4UL, slot_data->rollback_indexes[0]); |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 456 | for (size_t n = 1; n < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; n++) { |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 457 | EXPECT_EQ(0UL, slot_data->rollback_indexes[n]); |
| 458 | } |
| 459 | avb_slot_verify_data_free(slot_data); |
| 460 | } |
| 461 | |
| 462 | TEST_F(AvbSlotVerifyTest, HashDescriptorInVBMetaCorruptBoot) { |
| 463 | size_t boot_partition_size = 16 * 1024 * 1024; |
| 464 | base::FilePath boot_path = GenerateImage("boot_a.img", 5 * 1024 * 1024); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 465 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 466 | |
| 467 | EXPECT_COMMAND(0, |
| 468 | "./avbtool add_hash_footer" |
| 469 | " --image %s" |
| 470 | " --rollback_index 0" |
| 471 | " --partition_name boot" |
| 472 | " --partition_size %zd" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 473 | " --salt deadbeef" |
| 474 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 475 | boot_path.value().c_str(), |
| 476 | boot_partition_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 477 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 478 | GenerateVBMetaImage("vbmeta_a.img", |
| 479 | "SHA256_RSA2048", |
| 480 | 0, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 481 | base::FilePath("test/data/testkey_rsa2048.pem"), |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 482 | base::StringPrintf("--include_descriptors_from_image %s" |
| 483 | " --internal_release_string \"\"", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 484 | boot_path.value().c_str())); |
| 485 | |
| 486 | EXPECT_COMMAND(0, |
| 487 | "./avbtool erase_footer" |
| 488 | " --image %s", |
| 489 | boot_path.value().c_str()); |
| 490 | |
| 491 | ops_.set_expected_public_key( |
| 492 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 493 | |
| 494 | // So far, so good. |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 495 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 496 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 497 | avb_slot_verify(ops_.avb_ops(), |
| 498 | requested_partitions, |
| 499 | "_a", |
| 500 | false /* allow_verification_error */, |
| 501 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 502 | EXPECT_NE(nullptr, slot_data); |
| 503 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 504 | |
| 505 | // Now corrupt boot_a.img and expect verification error. |
| 506 | uint8_t corrupt_data[4] = {0xff, 0xff, 0xff, 0xff}; |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 507 | EXPECT_EQ(AVB_IO_RESULT_OK, |
| 508 | ops_.avb_ops()->write_to_partition(ops_.avb_ops(), |
| 509 | "boot_a", |
| 510 | 1024 * 1024, // offset: 1 MiB |
| 511 | sizeof corrupt_data, |
| 512 | corrupt_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 513 | |
| 514 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 515 | avb_slot_verify(ops_.avb_ops(), |
| 516 | requested_partitions, |
| 517 | "_a", |
| 518 | false /* allow_verification_error */, |
| 519 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 520 | EXPECT_EQ(nullptr, slot_data); |
| 521 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 522 | avb_slot_verify(ops_.avb_ops(), |
| 523 | requested_partitions, |
| 524 | "_a", |
| 525 | true /* allow_verification_error */, |
| 526 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 527 | EXPECT_NE(nullptr, slot_data); |
| 528 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | TEST_F(AvbSlotVerifyTest, HashDescriptorInChainedPartition) { |
| 532 | size_t boot_partition_size = 16 * 1024 * 1024; |
| 533 | const size_t boot_image_size = 5 * 1024 * 1024; |
| 534 | base::FilePath boot_path = GenerateImage("boot_a.img", boot_image_size); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 535 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 536 | |
| 537 | EXPECT_COMMAND(0, |
| 538 | "./avbtool add_hash_footer" |
| 539 | " --image %s" |
| 540 | " --kernel_cmdline 'cmdline2 in hash footer'" |
| 541 | " --rollback_index 12" |
| 542 | " --partition_name boot" |
| 543 | " --partition_size %zd" |
| 544 | " --algorithm SHA256_RSA4096" |
| 545 | " --key test/data/testkey_rsa4096.pem" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 546 | " --salt deadbeef" |
| 547 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 548 | boot_path.value().c_str(), |
| 549 | boot_partition_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 550 | |
| 551 | base::FilePath pk_path = testdir_.Append("testkey_rsa4096.avbpubkey"); |
| 552 | EXPECT_COMMAND( |
| 553 | 0, |
| 554 | "./avbtool extract_public_key --key test/data/testkey_rsa4096.pem" |
| 555 | " --output %s", |
| 556 | pk_path.value().c_str()); |
| 557 | |
| 558 | GenerateVBMetaImage( |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 559 | "vbmeta_a.img", |
| 560 | "SHA256_RSA2048", |
| 561 | 11, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 562 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 563 | base::StringPrintf("--chain_partition boot:1:%s" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 564 | " --kernel_cmdline 'cmdline2 in vbmeta'" |
| 565 | " --internal_release_string \"\"", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 566 | pk_path.value().c_str())); |
| 567 | |
| 568 | EXPECT_EQ( |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 569 | "Minimum libavb version: 1.0\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 570 | "Header Block: 256 bytes\n" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 571 | "Authentication Block: 320 bytes\n" |
David Zeuthen | 5cb2db9 | 2016-10-27 15:14:14 -0400 | [diff] [blame] | 572 | "Auxiliary Block: 1728 bytes\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 573 | "Algorithm: SHA256_RSA2048\n" |
| 574 | "Rollback Index: 11\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 575 | "Flags: 0\n" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 576 | "Release String: ''\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 577 | "Descriptors:\n" |
| 578 | " Chain Partition descriptor:\n" |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 579 | " Partition Name: boot\n" |
| 580 | " Rollback Index Location: 1\n" |
| 581 | " Public key (sha1): " |
| 582 | "2597c218aae470a130f61162feaae70afd97f011\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 583 | " Kernel Cmdline descriptor:\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 584 | " Flags: 0\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 585 | " Kernel Cmdline: 'cmdline2 in vbmeta'\n", |
| 586 | InfoImage(vbmeta_image_path_)); |
| 587 | |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 588 | EXPECT_EQ( |
| 589 | "Footer version: 1.0\n" |
| 590 | "Image size: 16777216 bytes\n" |
| 591 | "Original image size: 5242880 bytes\n" |
| 592 | "VBMeta offset: 5242880\n" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 593 | "VBMeta size: 2112 bytes\n" |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 594 | "--\n" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 595 | "Minimum libavb version: 1.0\n" |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 596 | "Header Block: 256 bytes\n" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 597 | "Authentication Block: 576 bytes\n" |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 598 | "Auxiliary Block: 1280 bytes\n" |
| 599 | "Algorithm: SHA256_RSA4096\n" |
| 600 | "Rollback Index: 12\n" |
| 601 | "Flags: 0\n" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 602 | "Release String: ''\n" |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 603 | "Descriptors:\n" |
| 604 | " Hash descriptor:\n" |
| 605 | " Image Size: 5242880 bytes\n" |
| 606 | " Hash Algorithm: sha256\n" |
| 607 | " Partition Name: boot\n" |
| 608 | " Salt: deadbeef\n" |
| 609 | " Digest: " |
| 610 | "184cb36243adb8b87d2d8c4802de32125fe294ec46753d732144ee65df68a23d\n" |
| 611 | " Kernel Cmdline descriptor:\n" |
| 612 | " Flags: 0\n" |
| 613 | " Kernel Cmdline: 'cmdline2 in hash footer'\n", |
| 614 | InfoImage(boot_path)); |
| 615 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 616 | ops_.set_expected_public_key( |
| 617 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 618 | |
| 619 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 620 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 621 | avb_slot_verify(ops_.avb_ops(), |
| 622 | requested_partitions, |
| 623 | "_a", |
| 624 | false /* allow_verification_error */, |
| 625 | &slot_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 626 | EXPECT_NE(nullptr, slot_data); |
| 627 | |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 628 | // Now verify the slot data. We should have two vbmeta |
| 629 | // structs. Verify both of them. Note that the A/B suffix isn't |
| 630 | // appended. |
| 631 | EXPECT_EQ(size_t(2), slot_data->num_vbmeta_images); |
| 632 | EXPECT_EQ("vbmeta", std::string(slot_data->vbmeta_images[0].partition_name)); |
| 633 | EXPECT_EQ(slot_data->vbmeta_images[0].vbmeta_size, vbmeta_image_.size()); |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 634 | EXPECT_EQ(0, |
| 635 | memcmp(vbmeta_image_.data(), |
| 636 | slot_data->vbmeta_images[0].vbmeta_data, |
| 637 | slot_data->vbmeta_images[0].vbmeta_size)); |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 638 | // And for the second vbmeta struct we check that the descriptors |
| 639 | // match the info_image output from above. |
| 640 | EXPECT_EQ("boot", std::string(slot_data->vbmeta_images[1].partition_name)); |
| 641 | const AvbDescriptor** descriptors = |
| 642 | avb_descriptor_get_all(slot_data->vbmeta_images[1].vbmeta_data, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 643 | slot_data->vbmeta_images[1].vbmeta_size, |
| 644 | NULL); |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 645 | EXPECT_NE(nullptr, descriptors); |
| 646 | AvbHashDescriptor hash_desc; |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 647 | EXPECT_EQ(true, |
| 648 | avb_hash_descriptor_validate_and_byteswap( |
| 649 | ((AvbHashDescriptor*)descriptors[0]), &hash_desc)); |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 650 | const uint8_t* desc_end = reinterpret_cast<const uint8_t*>(descriptors[0]) + |
| 651 | sizeof(AvbHashDescriptor); |
| 652 | uint64_t o = 0; |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 653 | EXPECT_EQ("boot", |
| 654 | std::string(reinterpret_cast<const char*>(desc_end + o), |
| 655 | hash_desc.partition_name_len)); |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 656 | o += hash_desc.partition_name_len; |
| 657 | EXPECT_EQ("deadbeef", mem_to_hexstring(desc_end + o, hash_desc.salt_len)); |
| 658 | o += hash_desc.salt_len; |
| 659 | EXPECT_EQ("184cb36243adb8b87d2d8c4802de32125fe294ec46753d732144ee65df68a23d", |
| 660 | mem_to_hexstring(desc_end + o, hash_desc.digest_len)); |
| 661 | AvbKernelCmdlineDescriptor cmdline_desc; |
| 662 | EXPECT_EQ(true, |
| 663 | avb_kernel_cmdline_descriptor_validate_and_byteswap( |
| 664 | ((AvbKernelCmdlineDescriptor*)descriptors[1]), &cmdline_desc)); |
| 665 | desc_end = reinterpret_cast<const uint8_t*>(descriptors[1]) + |
| 666 | sizeof(AvbKernelCmdlineDescriptor); |
| 667 | EXPECT_EQ("cmdline2 in hash footer", |
| 668 | std::string(reinterpret_cast<const char*>(desc_end), |
| 669 | cmdline_desc.kernel_cmdline_length)); |
| 670 | avb_free(descriptors); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 671 | |
| 672 | // The boot image data should match what is generated above with |
| 673 | // GenerateImage(). |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 674 | EXPECT_EQ(size_t(1), slot_data->num_loaded_partitions); |
| 675 | EXPECT_EQ("boot", |
| 676 | std::string(slot_data->loaded_partitions[0].partition_name)); |
| 677 | EXPECT_EQ(boot_image_size, slot_data->loaded_partitions[0].data_size); |
| 678 | for (size_t n = 0; n < slot_data->loaded_partitions[0].data_size; n++) { |
| 679 | EXPECT_EQ(slot_data->loaded_partitions[0].data[n], uint8_t(n)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // This should match the two cmdlines with a space (U+0020) between them. |
| 683 | EXPECT_EQ( |
| 684 | "cmdline2 in hash footer cmdline2 in vbmeta " |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 685 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 686 | "androidboot.vbmeta.avb_version=1.0 " |
| 687 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 688 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=4416 " |
David Zeuthen | 88b13e1 | 2016-10-03 17:24:55 -0400 | [diff] [blame] | 689 | "androidboot.vbmeta.digest=" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 690 | "4a45faa9adfeb94e9154fe682c11fef1a1a3d829b67cbf1a12ac7f0aa4f8e2e4", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 691 | std::string(slot_data->cmdline)); |
| 692 | EXPECT_EQ(11UL, slot_data->rollback_indexes[0]); |
| 693 | EXPECT_EQ(12UL, slot_data->rollback_indexes[1]); |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 694 | for (size_t n = 2; n < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; n++) { |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 695 | EXPECT_EQ(0UL, slot_data->rollback_indexes[n]); |
| 696 | } |
| 697 | avb_slot_verify_data_free(slot_data); |
| 698 | } |
| 699 | |
| 700 | TEST_F(AvbSlotVerifyTest, HashDescriptorInChainedPartitionCorruptBoot) { |
| 701 | size_t boot_partition_size = 16 * 1024 * 1024; |
| 702 | base::FilePath boot_path = GenerateImage("boot_a.img", 5 * 1024 * 1024); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 703 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 704 | |
| 705 | EXPECT_COMMAND(0, |
| 706 | "./avbtool add_hash_footer" |
| 707 | " --image %s" |
| 708 | " --rollback_index 0" |
| 709 | " --partition_name boot" |
| 710 | " --partition_size %zd" |
| 711 | " --algorithm SHA256_RSA4096" |
| 712 | " --key test/data/testkey_rsa4096.pem" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 713 | " --salt deadbeef" |
| 714 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 715 | boot_path.value().c_str(), |
| 716 | boot_partition_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 717 | |
| 718 | base::FilePath pk_path = testdir_.Append("testkey_rsa4096.avbpubkey"); |
| 719 | EXPECT_COMMAND( |
| 720 | 0, |
| 721 | "./avbtool extract_public_key --key test/data/testkey_rsa4096.pem" |
| 722 | " --output %s", |
| 723 | pk_path.value().c_str()); |
| 724 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 725 | GenerateVBMetaImage("vbmeta_a.img", |
| 726 | "SHA256_RSA2048", |
| 727 | 0, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 728 | base::FilePath("test/data/testkey_rsa2048.pem"), |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 729 | base::StringPrintf("--chain_partition boot:1:%s" |
| 730 | " --internal_release_string \"\"", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 731 | pk_path.value().c_str())); |
| 732 | |
| 733 | ops_.set_expected_public_key( |
| 734 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 735 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 736 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 737 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 738 | avb_slot_verify(ops_.avb_ops(), |
| 739 | requested_partitions, |
| 740 | "_a", |
| 741 | false /* allow_verification_error */, |
| 742 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 743 | EXPECT_NE(nullptr, slot_data); |
| 744 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 745 | |
| 746 | // Now corrupt boot_a.img and expect verification error. |
| 747 | uint8_t corrupt_data[4] = {0xff, 0xff, 0xff, 0xff}; |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 748 | EXPECT_EQ(AVB_IO_RESULT_OK, |
| 749 | ops_.avb_ops()->write_to_partition(ops_.avb_ops(), |
| 750 | "boot_a", |
| 751 | 1024 * 1024, // offset: 1 MiB |
| 752 | sizeof corrupt_data, |
| 753 | corrupt_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 754 | |
| 755 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 756 | avb_slot_verify(ops_.avb_ops(), |
| 757 | requested_partitions, |
| 758 | "_a", |
| 759 | false /* allow_verification_error */, |
| 760 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 761 | EXPECT_EQ(nullptr, slot_data); |
| 762 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 763 | avb_slot_verify(ops_.avb_ops(), |
| 764 | requested_partitions, |
| 765 | "_a", |
| 766 | true /* allow_verification_error */, |
| 767 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 768 | EXPECT_NE(nullptr, slot_data); |
| 769 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | TEST_F(AvbSlotVerifyTest, HashDescriptorInChainedPartitionKeyMismatch) { |
| 773 | size_t boot_partition_size = 16 * 1024 * 1024; |
| 774 | base::FilePath boot_path = GenerateImage("boot_a.img", 5 * 1024 * 1024); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 775 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 776 | |
| 777 | // Use different key to sign vbmeta in boot_a (we use the 8192 bit |
| 778 | // key) than what's in the chained partition descriptor (which is |
| 779 | // the 4096 bit key) and expect |
| 780 | // AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED. |
| 781 | |
| 782 | EXPECT_COMMAND(0, |
| 783 | "./avbtool add_hash_footer" |
| 784 | " --image %s" |
| 785 | " --rollback_index 0" |
| 786 | " --partition_name boot" |
| 787 | " --partition_size %zd" |
| 788 | " --algorithm SHA256_RSA8192" |
| 789 | " --key test/data/testkey_rsa8192.pem" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 790 | " --salt deadbeef" |
| 791 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 792 | boot_path.value().c_str(), |
| 793 | boot_partition_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 794 | |
| 795 | base::FilePath pk_path = testdir_.Append("testkey_rsa4096.avbpubkey"); |
| 796 | EXPECT_COMMAND( |
| 797 | 0, |
| 798 | "./avbtool extract_public_key --key test/data/testkey_rsa4096.pem" |
| 799 | " --output %s", |
| 800 | pk_path.value().c_str()); |
| 801 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 802 | GenerateVBMetaImage("vbmeta_a.img", |
| 803 | "SHA256_RSA2048", |
| 804 | 0, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 805 | base::FilePath("test/data/testkey_rsa2048.pem"), |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 806 | base::StringPrintf("--chain_partition boot:1:%s" |
| 807 | " --internal_release_string \"\"", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 808 | pk_path.value().c_str())); |
| 809 | |
| 810 | ops_.set_expected_public_key( |
| 811 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 812 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 813 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 814 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 815 | avb_slot_verify(ops_.avb_ops(), |
| 816 | requested_partitions, |
| 817 | "_a", |
| 818 | false /* allow_verification_error */, |
| 819 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 820 | EXPECT_EQ(nullptr, slot_data); |
| 821 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 822 | avb_slot_verify(ops_.avb_ops(), |
| 823 | requested_partitions, |
| 824 | "_a", |
| 825 | true /* allow_verification_error */, |
| 826 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 827 | EXPECT_NE(nullptr, slot_data); |
| 828 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | TEST_F(AvbSlotVerifyTest, HashDescriptorInChainedPartitionRollbackIndexFail) { |
| 832 | size_t boot_partition_size = 16 * 1024 * 1024; |
| 833 | base::FilePath boot_path = GenerateImage("boot_a.img", 5 * 1024 * 1024); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 834 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 835 | |
| 836 | EXPECT_COMMAND(0, |
| 837 | "./avbtool add_hash_footer" |
| 838 | " --image %s" |
| 839 | " --rollback_index 10" |
| 840 | " --partition_name boot" |
| 841 | " --partition_size %zd" |
| 842 | " --algorithm SHA256_RSA4096" |
| 843 | " --key test/data/testkey_rsa4096.pem" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 844 | " --salt deadbeef" |
| 845 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 846 | boot_path.value().c_str(), |
| 847 | boot_partition_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 848 | |
| 849 | base::FilePath pk_path = testdir_.Append("testkey_rsa4096.avbpubkey"); |
| 850 | EXPECT_COMMAND( |
| 851 | 0, |
| 852 | "./avbtool extract_public_key --key test/data/testkey_rsa4096.pem" |
| 853 | " --output %s", |
| 854 | pk_path.value().c_str()); |
| 855 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 856 | GenerateVBMetaImage("vbmeta_a.img", |
| 857 | "SHA256_RSA2048", |
| 858 | 110, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 859 | base::FilePath("test/data/testkey_rsa2048.pem"), |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 860 | base::StringPrintf("--chain_partition boot:1:%s" |
| 861 | " --internal_release_string \"\"", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 862 | pk_path.value().c_str())); |
| 863 | |
| 864 | ops_.set_expected_public_key( |
| 865 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 866 | |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 867 | AvbSlotVerifyData* slot_data = NULL; |
| 868 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 869 | // Both images (vbmeta_a and boot_a) have rollback index 10 and 11 |
| 870 | // so it should work if the stored rollback indexes are 0 and 0. |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 871 | ops_.set_stored_rollback_indexes({{0, 0}, {1, 0}}); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 872 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 873 | avb_slot_verify(ops_.avb_ops(), |
| 874 | requested_partitions, |
| 875 | "_a", |
| 876 | false /* allow_verification_error */, |
| 877 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 878 | EXPECT_NE(nullptr, slot_data); |
| 879 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 880 | |
| 881 | // Check failure if we set the stored rollback index of the chained |
| 882 | // partition to 20 (see AvbSlotVerifyTest.RollbackIndex above |
| 883 | // where we test rollback index checks for the vbmeta partition). |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 884 | ops_.set_stored_rollback_indexes({{0, 0}, {1, 20}}); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 885 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 886 | avb_slot_verify(ops_.avb_ops(), |
| 887 | requested_partitions, |
| 888 | "_a", |
| 889 | false /* allow_verification_error */, |
| 890 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 891 | EXPECT_EQ(nullptr, slot_data); |
| 892 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 893 | avb_slot_verify(ops_.avb_ops(), |
| 894 | requested_partitions, |
| 895 | "_a", |
| 896 | true /* allow_verification_error */, |
| 897 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 898 | EXPECT_NE(nullptr, slot_data); |
| 899 | avb_slot_verify_data_free(slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 900 | |
| 901 | // Check failure if there is no rollback index slot 1 - in that case |
| 902 | // we expect an I/O error since ops->read_rollback_index() will |
| 903 | // fail. |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 904 | ops_.set_stored_rollback_indexes({{0, 0}}); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 905 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_ERROR_IO, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 906 | avb_slot_verify(ops_.avb_ops(), |
| 907 | requested_partitions, |
| 908 | "_a", |
| 909 | false /* allow_verification_error */, |
| 910 | &slot_data)); |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 911 | EXPECT_EQ(nullptr, slot_data); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | TEST_F(AvbSlotVerifyTest, ChainedPartitionNoSlots) { |
| 915 | size_t boot_partition_size = 16 * 1024 * 1024; |
| 916 | const size_t boot_image_size = 5 * 1024 * 1024; |
| 917 | base::FilePath boot_path = GenerateImage("boot.img", boot_image_size); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 918 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 919 | |
| 920 | EXPECT_COMMAND(0, |
| 921 | "./avbtool add_hash_footer" |
| 922 | " --image %s" |
| 923 | " --kernel_cmdline 'cmdline2 in hash footer'" |
| 924 | " --rollback_index 12" |
| 925 | " --partition_name boot" |
| 926 | " --partition_size %zd" |
| 927 | " --algorithm SHA256_RSA4096" |
| 928 | " --key test/data/testkey_rsa4096.pem" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 929 | " --salt deadbeef" |
| 930 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 931 | boot_path.value().c_str(), |
| 932 | boot_partition_size); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 933 | |
| 934 | base::FilePath pk_path = testdir_.Append("testkey_rsa4096.avbpubkey"); |
| 935 | EXPECT_COMMAND( |
| 936 | 0, |
| 937 | "./avbtool extract_public_key --key test/data/testkey_rsa4096.pem" |
| 938 | " --output %s", |
| 939 | pk_path.value().c_str()); |
| 940 | |
| 941 | GenerateVBMetaImage( |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 942 | "vbmeta.img", |
| 943 | "SHA256_RSA2048", |
| 944 | 11, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 945 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 946 | base::StringPrintf("--chain_partition boot:1:%s" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 947 | " --kernel_cmdline 'cmdline2 in vbmeta'" |
| 948 | " --internal_release_string \"\"", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 949 | pk_path.value().c_str())); |
| 950 | |
| 951 | EXPECT_EQ( |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 952 | "Minimum libavb version: 1.0\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 953 | "Header Block: 256 bytes\n" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 954 | "Authentication Block: 320 bytes\n" |
David Zeuthen | 5cb2db9 | 2016-10-27 15:14:14 -0400 | [diff] [blame] | 955 | "Auxiliary Block: 1728 bytes\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 956 | "Algorithm: SHA256_RSA2048\n" |
| 957 | "Rollback Index: 11\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 958 | "Flags: 0\n" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 959 | "Release String: ''\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 960 | "Descriptors:\n" |
| 961 | " Chain Partition descriptor:\n" |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 962 | " Partition Name: boot\n" |
| 963 | " Rollback Index Location: 1\n" |
| 964 | " Public key (sha1): " |
| 965 | "2597c218aae470a130f61162feaae70afd97f011\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 966 | " Kernel Cmdline descriptor:\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 967 | " Flags: 0\n" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 968 | " Kernel Cmdline: 'cmdline2 in vbmeta'\n", |
| 969 | InfoImage(vbmeta_image_path_)); |
| 970 | |
| 971 | ops_.set_expected_public_key( |
| 972 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 973 | |
| 974 | AvbSlotVerifyData* slot_data = NULL; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 975 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 976 | avb_slot_verify(ops_.avb_ops(), |
| 977 | requested_partitions, |
| 978 | "", |
| 979 | false /* allow_verification_error */, |
| 980 | &slot_data)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 981 | EXPECT_NE(nullptr, slot_data); |
| 982 | |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 983 | // Now verify the slot data. The first vbmeta data should match our |
| 984 | // vbmeta_image_ member and the second one should be for the 'boot' |
| 985 | // partition. |
| 986 | EXPECT_EQ(size_t(2), slot_data->num_vbmeta_images); |
| 987 | EXPECT_EQ("vbmeta", std::string(slot_data->vbmeta_images[0].partition_name)); |
| 988 | EXPECT_EQ(slot_data->vbmeta_images[0].vbmeta_size, vbmeta_image_.size()); |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 989 | EXPECT_EQ(0, |
| 990 | memcmp(vbmeta_image_.data(), |
| 991 | slot_data->vbmeta_images[0].vbmeta_data, |
| 992 | slot_data->vbmeta_images[0].vbmeta_size)); |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 993 | EXPECT_EQ("boot", std::string(slot_data->vbmeta_images[1].partition_name)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 994 | |
| 995 | // The boot image data should match what is generated above with |
| 996 | // GenerateImage(). |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 997 | EXPECT_EQ(size_t(1), slot_data->num_loaded_partitions); |
| 998 | EXPECT_EQ("boot", |
| 999 | std::string(slot_data->loaded_partitions[0].partition_name)); |
| 1000 | EXPECT_EQ(boot_image_size, slot_data->loaded_partitions[0].data_size); |
| 1001 | for (size_t n = 0; n < slot_data->loaded_partitions[0].data_size; n++) { |
| 1002 | EXPECT_EQ(slot_data->loaded_partitions[0].data[n], uint8_t(n)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | // This should match the two cmdlines with a space (U+0020) between |
David Zeuthen | 19c3843 | 2017-02-16 13:08:38 -0500 | [diff] [blame] | 1006 | // them. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1007 | EXPECT_EQ( |
| 1008 | "cmdline2 in hash footer cmdline2 in vbmeta " |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 1009 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1010 | "androidboot.vbmeta.avb_version=1.0 " |
| 1011 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1012 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=4416 " |
David Zeuthen | 88b13e1 | 2016-10-03 17:24:55 -0400 | [diff] [blame] | 1013 | "androidboot.vbmeta.digest=" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1014 | "4a45faa9adfeb94e9154fe682c11fef1a1a3d829b67cbf1a12ac7f0aa4f8e2e4", |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1015 | std::string(slot_data->cmdline)); |
| 1016 | EXPECT_EQ(11UL, slot_data->rollback_indexes[0]); |
| 1017 | EXPECT_EQ(12UL, slot_data->rollback_indexes[1]); |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 1018 | for (size_t n = 2; n < AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS; n++) { |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1019 | EXPECT_EQ(0UL, slot_data->rollback_indexes[n]); |
| 1020 | } |
| 1021 | avb_slot_verify_data_free(slot_data); |
| 1022 | } |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1023 | |
| 1024 | TEST_F(AvbSlotVerifyTest, PartitionsOtherThanBoot) { |
| 1025 | const size_t foo_partition_size = 16 * 1024 * 1024; |
| 1026 | const size_t bar_partition_size = 32 * 1024 * 1024; |
| 1027 | const size_t foo_image_size = 5 * 1024 * 1024; |
| 1028 | const size_t bar_image_size = 10 * 1024 * 1024; |
| 1029 | base::FilePath foo_path = GenerateImage("foo_a.img", foo_image_size); |
| 1030 | base::FilePath bar_path = GenerateImage("bar_a.img", bar_image_size); |
| 1031 | |
| 1032 | EXPECT_COMMAND(0, |
| 1033 | "./avbtool add_hash_footer" |
| 1034 | " --image %s" |
| 1035 | " --partition_name foo" |
| 1036 | " --partition_size %zd" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1037 | " --salt deadbeef" |
| 1038 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1039 | foo_path.value().c_str(), |
| 1040 | foo_partition_size); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1041 | |
| 1042 | EXPECT_COMMAND(0, |
| 1043 | "./avbtool add_hash_footer" |
| 1044 | " --image %s" |
| 1045 | " --partition_name bar" |
| 1046 | " --partition_size %zd" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1047 | " --salt deadbeef" |
| 1048 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1049 | bar_path.value().c_str(), |
| 1050 | bar_partition_size); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1051 | |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1052 | GenerateVBMetaImage("vbmeta_a.img", |
| 1053 | "SHA256_RSA2048", |
| 1054 | 4, |
| 1055 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 1056 | base::StringPrintf("--include_descriptors_from_image %s" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1057 | " --include_descriptors_from_image %s" |
| 1058 | " --internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1059 | foo_path.value().c_str(), |
| 1060 | bar_path.value().c_str())); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1061 | |
| 1062 | EXPECT_EQ( |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1063 | "Minimum libavb version: 1.0\n" |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1064 | "Header Block: 256 bytes\n" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1065 | "Authentication Block: 320 bytes\n" |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1066 | "Auxiliary Block: 896 bytes\n" |
| 1067 | "Algorithm: SHA256_RSA2048\n" |
| 1068 | "Rollback Index: 4\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1069 | "Flags: 0\n" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1070 | "Release String: ''\n" |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1071 | "Descriptors:\n" |
| 1072 | " Hash descriptor:\n" |
| 1073 | " Image Size: 5242880 bytes\n" |
| 1074 | " Hash Algorithm: sha256\n" |
| 1075 | " Partition Name: foo\n" |
| 1076 | " Salt: deadbeef\n" |
| 1077 | " Digest: " |
| 1078 | "184cb36243adb8b87d2d8c4802de32125fe294ec46753d732144ee65df68a23d\n" |
| 1079 | " Hash descriptor:\n" |
| 1080 | " Image Size: 10485760 bytes\n" |
| 1081 | " Hash Algorithm: sha256\n" |
| 1082 | " Partition Name: bar\n" |
| 1083 | " Salt: deadbeef\n" |
| 1084 | " Digest: " |
| 1085 | "baea4bbd261d0edf4d1fe5e6e5a36976c291eeba66b6a46fa81dba691327a727\n", |
| 1086 | InfoImage(vbmeta_image_path_)); |
| 1087 | |
| 1088 | ops_.set_expected_public_key( |
| 1089 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 1090 | |
| 1091 | AvbSlotVerifyData* slot_data = NULL; |
| 1092 | const char* requested_partitions[] = {"foo", "bar", NULL}; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 1093 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1094 | avb_slot_verify(ops_.avb_ops(), |
| 1095 | requested_partitions, |
| 1096 | "_a", |
| 1097 | false /* allow_verification_error */, |
| 1098 | &slot_data)); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1099 | EXPECT_NE(nullptr, slot_data); |
| 1100 | |
| 1101 | // Now verify the slot data. The vbmeta data should match our |
| 1102 | // vbmeta_image_ member. |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 1103 | EXPECT_EQ(size_t(1), slot_data->num_vbmeta_images); |
| 1104 | EXPECT_EQ("vbmeta", std::string(slot_data->vbmeta_images[0].partition_name)); |
| 1105 | EXPECT_EQ(slot_data->vbmeta_images[0].vbmeta_size, vbmeta_image_.size()); |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1106 | EXPECT_EQ(0, |
| 1107 | memcmp(vbmeta_image_.data(), |
| 1108 | slot_data->vbmeta_images[0].vbmeta_data, |
| 1109 | slot_data->vbmeta_images[0].vbmeta_size)); |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 1110 | |
| 1111 | // The 'foo' and 'bar' image data should match what is generated |
| 1112 | // above with GenerateImage(). |
| 1113 | EXPECT_EQ(size_t(2), slot_data->num_loaded_partitions); |
| 1114 | EXPECT_EQ("foo", std::string(slot_data->loaded_partitions[0].partition_name)); |
| 1115 | EXPECT_EQ(foo_image_size, slot_data->loaded_partitions[0].data_size); |
| 1116 | for (size_t n = 0; n < slot_data->loaded_partitions[0].data_size; n++) { |
| 1117 | EXPECT_EQ(slot_data->loaded_partitions[0].data[n], uint8_t(n)); |
| 1118 | } |
| 1119 | EXPECT_EQ("bar", std::string(slot_data->loaded_partitions[1].partition_name)); |
| 1120 | EXPECT_EQ(bar_image_size, slot_data->loaded_partitions[1].data_size); |
| 1121 | for (size_t n = 0; n < slot_data->loaded_partitions[1].data_size; n++) { |
| 1122 | EXPECT_EQ(slot_data->loaded_partitions[1].data[n], uint8_t(n)); |
| 1123 | } |
| 1124 | |
| 1125 | avb_slot_verify_data_free(slot_data); |
| 1126 | } |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 1127 | |
| 1128 | TEST_F(AvbSlotVerifyTest, PublicKeyMetadata) { |
| 1129 | base::FilePath md_path = GenerateImage("md.bin", 1536); |
| 1130 | |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1131 | GenerateVBMetaImage("vbmeta_a.img", |
| 1132 | "SHA256_RSA2048", |
| 1133 | 0, |
| 1134 | base::FilePath("test/data/testkey_rsa2048.pem"), |
| 1135 | base::StringPrintf("--public_key_metadata %s" |
| 1136 | " --internal_release_string \"\"", |
| 1137 | md_path.value().c_str())); |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 1138 | |
| 1139 | ops_.set_expected_public_key( |
| 1140 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 1141 | |
| 1142 | std::string md_data; |
| 1143 | ASSERT_TRUE(base::ReadFileToString(md_path, &md_data)); |
| 1144 | ops_.set_expected_public_key_metadata(md_data); |
| 1145 | |
| 1146 | AvbSlotVerifyData* slot_data = NULL; |
| 1147 | const char* requested_partitions[] = {"boot", NULL}; |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 1148 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1149 | avb_slot_verify(ops_.avb_ops(), |
| 1150 | requested_partitions, |
| 1151 | "_a", |
| 1152 | false /* allow_verification_error */, |
| 1153 | &slot_data)); |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 1154 | EXPECT_NE(nullptr, slot_data); |
| 1155 | EXPECT_EQ( |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 1156 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1157 | "androidboot.vbmeta.avb_version=1.0 " |
| 1158 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1159 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=2688 " |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 1160 | "androidboot.vbmeta.digest=" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1161 | "5edcaa54f40382ee6a2fc3b86cdf383348b35ed07955e83ea32d84b69a97eaa0", |
David Zeuthen | 18666ab | 2016-11-15 11:18:05 -0500 | [diff] [blame] | 1162 | std::string(slot_data->cmdline)); |
| 1163 | avb_slot_verify_data_free(slot_data); |
| 1164 | } |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1165 | |
| 1166 | void AvbSlotVerifyTest::CmdlineWithHashtreeVerification( |
| 1167 | bool hashtree_verification_on) { |
| 1168 | const size_t rootfs_size = 1028 * 1024; |
| 1169 | const size_t partition_size = 1536 * 1024; |
| 1170 | |
| 1171 | // Generate a 1028 KiB file with known content. |
| 1172 | std::vector<uint8_t> rootfs; |
| 1173 | rootfs.resize(rootfs_size); |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1174 | for (size_t n = 0; n < rootfs_size; n++) |
| 1175 | rootfs[n] = uint8_t(n); |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1176 | base::FilePath rootfs_path = testdir_.Append("rootfs.bin"); |
| 1177 | EXPECT_EQ(rootfs_size, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1178 | static_cast<const size_t>( |
| 1179 | base::WriteFile(rootfs_path, |
| 1180 | reinterpret_cast<const char*>(rootfs.data()), |
| 1181 | rootfs.size()))); |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1182 | |
| 1183 | EXPECT_COMMAND(0, |
| 1184 | "./avbtool add_hashtree_footer --salt d00df00d --image %s " |
| 1185 | "--partition_size %d --partition_name foobar " |
| 1186 | "--algorithm SHA256_RSA2048 " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1187 | "--key test/data/testkey_rsa2048.pem " |
| 1188 | "--internal_release_string \"\"", |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1189 | rootfs_path.value().c_str(), |
| 1190 | (int)partition_size); |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1191 | |
| 1192 | // Check that we correctly generate dm-verity kernel cmdline |
| 1193 | // snippets, if requested. |
| 1194 | GenerateVBMetaImage( |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1195 | "vbmeta_a.img", |
| 1196 | "SHA256_RSA2048", |
| 1197 | 4, |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1198 | base::FilePath("test/data/testkey_rsa2048.pem"), |
David Zeuthen | 5d4f4f2 | 2017-01-11 11:37:34 -0500 | [diff] [blame] | 1199 | base::StringPrintf("--setup_rootfs_from_kernel %s " |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1200 | "--kernel_cmdline should_be_in_both=1 " |
| 1201 | "--algorithm SHA256_RSA2048 " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1202 | "--flags %d " |
| 1203 | "--internal_release_string \"\"", |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1204 | rootfs_path.value().c_str(), |
| 1205 | hashtree_verification_on |
| 1206 | ? 0 |
| 1207 | : AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED)); |
| 1208 | |
| 1209 | EXPECT_EQ( |
| 1210 | base::StringPrintf( |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1211 | "Minimum libavb version: 1.0\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1212 | "Header Block: 256 bytes\n" |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1213 | "Authentication Block: 320 bytes\n" |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 1214 | "Auxiliary Block: 896 bytes\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1215 | "Algorithm: SHA256_RSA2048\n" |
| 1216 | "Rollback Index: 4\n" |
| 1217 | "Flags: %d\n" |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1218 | "Release String: ''\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1219 | "Descriptors:\n" |
| 1220 | " Kernel Cmdline descriptor:\n" |
| 1221 | " Flags: 1\n" |
| 1222 | " Kernel Cmdline: 'dm=\"1 vroot none ro 1,0 2056 verity " |
| 1223 | "1 PARTUUID=$(ANDROID_SYSTEM_PARTUUID) " |
| 1224 | "PARTUUID=$(ANDROID_SYSTEM_PARTUUID) 4096 4096 257 257 sha1 " |
David Zeuthen | a01e32f | 2017-01-24 17:32:38 -0500 | [diff] [blame] | 1225 | "e811611467dcd6e8dc4324e45f706c2bdd51db67 d00df00d 2 " |
| 1226 | "restart_on_corruption ignore_zero_blocks\" root=0xfd00'\n" |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1227 | " Kernel Cmdline descriptor:\n" |
| 1228 | " Flags: 2\n" |
| 1229 | " Kernel Cmdline: " |
| 1230 | "'root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)'\n" |
| 1231 | " Kernel Cmdline descriptor:\n" |
| 1232 | " Flags: 0\n" |
| 1233 | " Kernel Cmdline: 'should_be_in_both=1'\n", |
| 1234 | hashtree_verification_on ? 0 |
| 1235 | : AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED), |
| 1236 | InfoImage(vbmeta_image_path_)); |
| 1237 | |
| 1238 | ops_.set_expected_public_key( |
| 1239 | PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem"))); |
| 1240 | |
| 1241 | // Check that avb_slot_verify() picks the cmdline decsriptors based |
| 1242 | // on their flags value. |
| 1243 | AvbSlotVerifyData* slot_data = NULL; |
| 1244 | const char* requested_partitions[] = {"boot", NULL}; |
| 1245 | EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK, |
David Zeuthen | 4b6a634 | 2017-01-03 15:19:56 -0500 | [diff] [blame] | 1246 | avb_slot_verify(ops_.avb_ops(), |
| 1247 | requested_partitions, |
| 1248 | "_a", |
| 1249 | false /* allow_verification_error */, |
| 1250 | &slot_data)); |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1251 | EXPECT_NE(nullptr, slot_data); |
| 1252 | if (hashtree_verification_on) { |
| 1253 | EXPECT_EQ( |
| 1254 | "dm=\"1 vroot none ro 1,0 2056 verity 1 " |
| 1255 | "PARTUUID=1234-fake-guid-for:system_a " |
| 1256 | "PARTUUID=1234-fake-guid-for:system_a 4096 4096 257 257 sha1 " |
David Zeuthen | a01e32f | 2017-01-24 17:32:38 -0500 | [diff] [blame] | 1257 | "e811611467dcd6e8dc4324e45f706c2bdd51db67 d00df00d 2 " |
| 1258 | "restart_on_corruption ignore_zero_blocks\" root=0xfd00 " |
| 1259 | "should_be_in_both=1 " |
David Zeuthen | bc8f647 | 2016-11-21 14:59:15 -0500 | [diff] [blame] | 1260 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1261 | "androidboot.vbmeta.avb_version=1.0 " |
| 1262 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1263 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=1472 " |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1264 | "androidboot.vbmeta.digest=" |
David Zeuthen | a01e32f | 2017-01-24 17:32:38 -0500 | [diff] [blame] | 1265 | "32572f7a89fb44cc76e9e3adbc0cb5272d0604578b2179729aa52d2bba4061c3", |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1266 | std::string(slot_data->cmdline)); |
| 1267 | } else { |
| 1268 | EXPECT_EQ( |
| 1269 | "root=PARTUUID=1234-fake-guid-for:system_a should_be_in_both=1 " |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 1270 | "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a " |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame^] | 1271 | "androidboot.vbmeta.avb_version=1.0 " |
| 1272 | "androidboot.vbmeta.device_state=locked " |
David Zeuthen | d5db21d | 2017-01-24 10:11:38 -0500 | [diff] [blame] | 1273 | "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=1472 " |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1274 | "androidboot.vbmeta.digest=" |
David Zeuthen | a01e32f | 2017-01-24 17:32:38 -0500 | [diff] [blame] | 1275 | "bb630c27d09e10117092d4444ea57a99bf44101c46cf5424cfb86928a0873249", |
David Zeuthen | fd41eb9 | 2016-11-17 12:24:47 -0500 | [diff] [blame] | 1276 | std::string(slot_data->cmdline)); |
| 1277 | } |
| 1278 | avb_slot_verify_data_free(slot_data); |
| 1279 | } |
| 1280 | |
| 1281 | TEST_F(AvbSlotVerifyTest, CmdlineWithHashtreeVerificationOff) { |
| 1282 | CmdlineWithHashtreeVerification(false); |
| 1283 | } |
| 1284 | |
| 1285 | TEST_F(AvbSlotVerifyTest, CmdlineWithHashtreeVerificationOn) { |
| 1286 | CmdlineWithHashtreeVerification(true); |
| 1287 | } |
Darren Krahn | 72d5790 | 2016-12-12 18:34:08 -0800 | [diff] [blame] | 1288 | |
| 1289 | } // namespace avb |