1*897a1d94STom Rini // SPDX-License-Identifier: MIT
2d8f9d2afSIgor Opaniuk /*
3d8f9d2afSIgor Opaniuk * Copyright (C) 2016 The Android Open Source Project
4d8f9d2afSIgor Opaniuk */
5d8f9d2afSIgor Opaniuk
6d8f9d2afSIgor Opaniuk #include "avb_slot_verify.h"
7d8f9d2afSIgor Opaniuk #include "avb_chain_partition_descriptor.h"
8d8f9d2afSIgor Opaniuk #include "avb_cmdline.h"
9d8f9d2afSIgor Opaniuk #include "avb_footer.h"
10d8f9d2afSIgor Opaniuk #include "avb_hash_descriptor.h"
11d8f9d2afSIgor Opaniuk #include "avb_hashtree_descriptor.h"
12d8f9d2afSIgor Opaniuk #include "avb_kernel_cmdline_descriptor.h"
13d8f9d2afSIgor Opaniuk #include "avb_sha.h"
14d8f9d2afSIgor Opaniuk #include "avb_util.h"
15d8f9d2afSIgor Opaniuk #include "avb_vbmeta_image.h"
16d8f9d2afSIgor Opaniuk #include "avb_version.h"
17d8f9d2afSIgor Opaniuk
18d8f9d2afSIgor Opaniuk /* Maximum number of partitions that can be loaded with avb_slot_verify(). */
19d8f9d2afSIgor Opaniuk #define MAX_NUMBER_OF_LOADED_PARTITIONS 32
20d8f9d2afSIgor Opaniuk
21d8f9d2afSIgor Opaniuk /* Maximum number of vbmeta images that can be loaded with avb_slot_verify(). */
22d8f9d2afSIgor Opaniuk #define MAX_NUMBER_OF_VBMETA_IMAGES 32
23d8f9d2afSIgor Opaniuk
24d8f9d2afSIgor Opaniuk /* Maximum size of a vbmeta image - 64 KiB. */
25d8f9d2afSIgor Opaniuk #define VBMETA_MAX_SIZE (64 * 1024)
26d8f9d2afSIgor Opaniuk
27d8f9d2afSIgor Opaniuk /* Helper function to see if we should continue with verification in
28d8f9d2afSIgor Opaniuk * allow_verification_error=true mode if something goes wrong. See the
29d8f9d2afSIgor Opaniuk * comments for the avb_slot_verify() function for more information.
30d8f9d2afSIgor Opaniuk */
result_should_continue(AvbSlotVerifyResult result)31d8f9d2afSIgor Opaniuk static inline bool result_should_continue(AvbSlotVerifyResult result) {
32d8f9d2afSIgor Opaniuk switch (result) {
33d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
34d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
35d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
36d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
37d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
38d8f9d2afSIgor Opaniuk return false;
39d8f9d2afSIgor Opaniuk
40d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_OK:
41d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
42d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
43d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
44d8f9d2afSIgor Opaniuk return true;
45d8f9d2afSIgor Opaniuk }
46d8f9d2afSIgor Opaniuk
47d8f9d2afSIgor Opaniuk return false;
48d8f9d2afSIgor Opaniuk }
49d8f9d2afSIgor Opaniuk
load_full_partition(AvbOps * ops,const char * part_name,uint64_t image_size,uint8_t ** out_image_buf,bool * out_image_preloaded)50d8f9d2afSIgor Opaniuk static AvbSlotVerifyResult load_full_partition(AvbOps* ops,
51d8f9d2afSIgor Opaniuk const char* part_name,
52d8f9d2afSIgor Opaniuk uint64_t image_size,
53d8f9d2afSIgor Opaniuk uint8_t** out_image_buf,
54d8f9d2afSIgor Opaniuk bool* out_image_preloaded) {
55d8f9d2afSIgor Opaniuk size_t part_num_read;
56d8f9d2afSIgor Opaniuk AvbIOResult io_ret;
57d8f9d2afSIgor Opaniuk
58d8f9d2afSIgor Opaniuk /* Make sure that we do not overwrite existing data. */
59d8f9d2afSIgor Opaniuk avb_assert(*out_image_buf == NULL);
60d8f9d2afSIgor Opaniuk avb_assert(!*out_image_preloaded);
61d8f9d2afSIgor Opaniuk
62d8f9d2afSIgor Opaniuk /* We are going to implicitly cast image_size from uint64_t to size_t in the
63d8f9d2afSIgor Opaniuk * following code, so we need to make sure that the cast is safe. */
64d8f9d2afSIgor Opaniuk if (image_size != (size_t)(image_size)) {
65d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Partition size too large to load.\n", NULL);
66d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
67d8f9d2afSIgor Opaniuk }
68d8f9d2afSIgor Opaniuk
69d8f9d2afSIgor Opaniuk /* Try use a preloaded one. */
70d8f9d2afSIgor Opaniuk if (ops->get_preloaded_partition != NULL) {
71d8f9d2afSIgor Opaniuk io_ret = ops->get_preloaded_partition(
72d8f9d2afSIgor Opaniuk ops, part_name, image_size, out_image_buf, &part_num_read);
73d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
74d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
75d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
76d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
77d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
78d8f9d2afSIgor Opaniuk }
79d8f9d2afSIgor Opaniuk
80d8f9d2afSIgor Opaniuk if (*out_image_buf != NULL) {
81d8f9d2afSIgor Opaniuk if (part_num_read != image_size) {
82d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL);
83d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
84d8f9d2afSIgor Opaniuk }
85d8f9d2afSIgor Opaniuk *out_image_preloaded = true;
86d8f9d2afSIgor Opaniuk }
87d8f9d2afSIgor Opaniuk }
88d8f9d2afSIgor Opaniuk
89d8f9d2afSIgor Opaniuk /* Allocate and copy the partition. */
90d8f9d2afSIgor Opaniuk if (!*out_image_preloaded) {
91d8f9d2afSIgor Opaniuk *out_image_buf = avb_malloc(image_size);
92d8f9d2afSIgor Opaniuk if (*out_image_buf == NULL) {
93d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
94d8f9d2afSIgor Opaniuk }
95d8f9d2afSIgor Opaniuk
96d8f9d2afSIgor Opaniuk io_ret = ops->read_from_partition(ops,
97d8f9d2afSIgor Opaniuk part_name,
98d8f9d2afSIgor Opaniuk 0 /* offset */,
99d8f9d2afSIgor Opaniuk image_size,
100d8f9d2afSIgor Opaniuk *out_image_buf,
101d8f9d2afSIgor Opaniuk &part_num_read);
102d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
103d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
104d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
105d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Error loading data from partition.\n", NULL);
106d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
107d8f9d2afSIgor Opaniuk }
108d8f9d2afSIgor Opaniuk if (part_num_read != image_size) {
109d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Read incorrect number of bytes.\n", NULL);
110d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
111d8f9d2afSIgor Opaniuk }
112d8f9d2afSIgor Opaniuk }
113d8f9d2afSIgor Opaniuk
114d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_OK;
115d8f9d2afSIgor Opaniuk }
116d8f9d2afSIgor Opaniuk
read_persistent_digest(AvbOps * ops,const char * part_name,size_t expected_digest_size,uint8_t * out_digest)117d8f9d2afSIgor Opaniuk static AvbSlotVerifyResult read_persistent_digest(AvbOps* ops,
118d8f9d2afSIgor Opaniuk const char* part_name,
119d8f9d2afSIgor Opaniuk size_t expected_digest_size,
120d8f9d2afSIgor Opaniuk uint8_t* out_digest) {
121d8f9d2afSIgor Opaniuk char* persistent_value_name = NULL;
122d8f9d2afSIgor Opaniuk AvbIOResult io_ret = AVB_IO_RESULT_OK;
123d8f9d2afSIgor Opaniuk size_t stored_digest_size = 0;
124d8f9d2afSIgor Opaniuk
125d8f9d2afSIgor Opaniuk if (ops->read_persistent_value == NULL) {
126d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Persistent values are not implemented.\n", NULL);
127d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
128d8f9d2afSIgor Opaniuk }
129d8f9d2afSIgor Opaniuk persistent_value_name =
130d8f9d2afSIgor Opaniuk avb_strdupv(AVB_NPV_PERSISTENT_DIGEST_PREFIX, part_name, NULL);
131d8f9d2afSIgor Opaniuk if (persistent_value_name == NULL) {
132d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
133d8f9d2afSIgor Opaniuk }
134d8f9d2afSIgor Opaniuk io_ret = ops->read_persistent_value(ops,
135d8f9d2afSIgor Opaniuk persistent_value_name,
136d8f9d2afSIgor Opaniuk expected_digest_size,
137d8f9d2afSIgor Opaniuk out_digest,
138d8f9d2afSIgor Opaniuk &stored_digest_size);
139d8f9d2afSIgor Opaniuk avb_free(persistent_value_name);
140d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
141d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
142d8f9d2afSIgor Opaniuk } else if (io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_VALUE) {
143d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Persistent digest does not exist.\n", NULL);
144d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
145d8f9d2afSIgor Opaniuk } else if (io_ret == AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE ||
146d8f9d2afSIgor Opaniuk io_ret == AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE ||
147d8f9d2afSIgor Opaniuk expected_digest_size != stored_digest_size) {
148d8f9d2afSIgor Opaniuk avb_errorv(
149d8f9d2afSIgor Opaniuk part_name, ": Persistent digest is not of expected size.\n", NULL);
150d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
151d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
152d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Error reading persistent digest.\n", NULL);
153d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_ERROR_IO;
154d8f9d2afSIgor Opaniuk }
155d8f9d2afSIgor Opaniuk return AVB_SLOT_VERIFY_RESULT_OK;
156d8f9d2afSIgor Opaniuk }
157d8f9d2afSIgor Opaniuk
load_and_verify_hash_partition(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,bool allow_verification_error,const AvbDescriptor * descriptor,AvbSlotVerifyData * slot_data)158d8f9d2afSIgor Opaniuk static AvbSlotVerifyResult load_and_verify_hash_partition(
159d8f9d2afSIgor Opaniuk AvbOps* ops,
160d8f9d2afSIgor Opaniuk const char* const* requested_partitions,
161d8f9d2afSIgor Opaniuk const char* ab_suffix,
162d8f9d2afSIgor Opaniuk bool allow_verification_error,
163d8f9d2afSIgor Opaniuk const AvbDescriptor* descriptor,
164d8f9d2afSIgor Opaniuk AvbSlotVerifyData* slot_data) {
165d8f9d2afSIgor Opaniuk AvbHashDescriptor hash_desc;
166d8f9d2afSIgor Opaniuk const uint8_t* desc_partition_name = NULL;
167d8f9d2afSIgor Opaniuk const uint8_t* desc_salt;
168d8f9d2afSIgor Opaniuk const uint8_t* desc_digest;
169d8f9d2afSIgor Opaniuk char part_name[AVB_PART_NAME_MAX_SIZE];
170d8f9d2afSIgor Opaniuk AvbSlotVerifyResult ret;
171d8f9d2afSIgor Opaniuk AvbIOResult io_ret;
172d8f9d2afSIgor Opaniuk uint8_t* image_buf = NULL;
173d8f9d2afSIgor Opaniuk bool image_preloaded = false;
174d8f9d2afSIgor Opaniuk uint8_t* digest;
175d8f9d2afSIgor Opaniuk size_t digest_len;
176d8f9d2afSIgor Opaniuk const char* found;
177d8f9d2afSIgor Opaniuk uint64_t image_size;
178d8f9d2afSIgor Opaniuk size_t expected_digest_len = 0;
179d8f9d2afSIgor Opaniuk uint8_t expected_digest_buf[AVB_SHA512_DIGEST_SIZE];
180d8f9d2afSIgor Opaniuk const uint8_t* expected_digest = NULL;
181d8f9d2afSIgor Opaniuk
182d8f9d2afSIgor Opaniuk if (!avb_hash_descriptor_validate_and_byteswap(
183d8f9d2afSIgor Opaniuk (const AvbHashDescriptor*)descriptor, &hash_desc)) {
184d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
185d8f9d2afSIgor Opaniuk goto out;
186d8f9d2afSIgor Opaniuk }
187d8f9d2afSIgor Opaniuk
188d8f9d2afSIgor Opaniuk desc_partition_name =
189d8f9d2afSIgor Opaniuk ((const uint8_t*)descriptor) + sizeof(AvbHashDescriptor);
190d8f9d2afSIgor Opaniuk desc_salt = desc_partition_name + hash_desc.partition_name_len;
191d8f9d2afSIgor Opaniuk desc_digest = desc_salt + hash_desc.salt_len;
192d8f9d2afSIgor Opaniuk
193d8f9d2afSIgor Opaniuk if (!avb_validate_utf8(desc_partition_name, hash_desc.partition_name_len)) {
194d8f9d2afSIgor Opaniuk avb_error("Partition name is not valid UTF-8.\n");
195d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
196d8f9d2afSIgor Opaniuk goto out;
197d8f9d2afSIgor Opaniuk }
198d8f9d2afSIgor Opaniuk
199d8f9d2afSIgor Opaniuk /* Don't bother loading or validating unless the partition was
200d8f9d2afSIgor Opaniuk * requested in the first place.
201d8f9d2afSIgor Opaniuk */
202d8f9d2afSIgor Opaniuk found = avb_strv_find_str(requested_partitions,
203d8f9d2afSIgor Opaniuk (const char*)desc_partition_name,
204d8f9d2afSIgor Opaniuk hash_desc.partition_name_len);
205d8f9d2afSIgor Opaniuk if (found == NULL) {
206d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_OK;
207d8f9d2afSIgor Opaniuk goto out;
208d8f9d2afSIgor Opaniuk }
209d8f9d2afSIgor Opaniuk
210d8f9d2afSIgor Opaniuk if ((hash_desc.flags & AVB_HASH_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) != 0) {
211d8f9d2afSIgor Opaniuk /* No ab_suffix, just copy the partition name as is. */
212d8f9d2afSIgor Opaniuk if (hash_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
213d8f9d2afSIgor Opaniuk avb_error("Partition name does not fit.\n");
214d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
215d8f9d2afSIgor Opaniuk goto out;
216d8f9d2afSIgor Opaniuk }
217d8f9d2afSIgor Opaniuk avb_memcpy(part_name, desc_partition_name, hash_desc.partition_name_len);
218d8f9d2afSIgor Opaniuk part_name[hash_desc.partition_name_len] = '\0';
219d8f9d2afSIgor Opaniuk } else if (hash_desc.digest_len == 0 && avb_strlen(ab_suffix) != 0) {
220d8f9d2afSIgor Opaniuk /* No ab_suffix allowed for partitions without a digest in the descriptor
221d8f9d2afSIgor Opaniuk * because these partitions hold data unique to this device and are not
222d8f9d2afSIgor Opaniuk * updated using an A/B scheme.
223d8f9d2afSIgor Opaniuk */
224d8f9d2afSIgor Opaniuk avb_error("Cannot use A/B with a persistent digest.\n");
225d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
226d8f9d2afSIgor Opaniuk goto out;
227d8f9d2afSIgor Opaniuk } else {
228d8f9d2afSIgor Opaniuk /* Add ab_suffix to the partition name. */
229d8f9d2afSIgor Opaniuk if (!avb_str_concat(part_name,
230d8f9d2afSIgor Opaniuk sizeof part_name,
231d8f9d2afSIgor Opaniuk (const char*)desc_partition_name,
232d8f9d2afSIgor Opaniuk hash_desc.partition_name_len,
233d8f9d2afSIgor Opaniuk ab_suffix,
234d8f9d2afSIgor Opaniuk avb_strlen(ab_suffix))) {
235d8f9d2afSIgor Opaniuk avb_error("Partition name and suffix does not fit.\n");
236d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
237d8f9d2afSIgor Opaniuk goto out;
238d8f9d2afSIgor Opaniuk }
239d8f9d2afSIgor Opaniuk }
240d8f9d2afSIgor Opaniuk
241d8f9d2afSIgor Opaniuk /* If we're allowing verification errors then hash_desc.image_size
242d8f9d2afSIgor Opaniuk * may no longer match what's in the partition... so in this case
243d8f9d2afSIgor Opaniuk * just load the entire partition.
244d8f9d2afSIgor Opaniuk *
245d8f9d2afSIgor Opaniuk * For example, this can happen if a developer does 'fastboot flash
246d8f9d2afSIgor Opaniuk * boot /path/to/new/and/bigger/boot.img'. We want this to work
247d8f9d2afSIgor Opaniuk * since it's such a common workflow.
248d8f9d2afSIgor Opaniuk */
249d8f9d2afSIgor Opaniuk image_size = hash_desc.image_size;
250d8f9d2afSIgor Opaniuk if (allow_verification_error) {
251d8f9d2afSIgor Opaniuk if (ops->get_size_of_partition == NULL) {
252d8f9d2afSIgor Opaniuk avb_errorv(part_name,
253d8f9d2afSIgor Opaniuk ": The get_size_of_partition() operation is "
254d8f9d2afSIgor Opaniuk "not implemented so we may not load the entire partition. "
255d8f9d2afSIgor Opaniuk "Please implement.",
256d8f9d2afSIgor Opaniuk NULL);
257d8f9d2afSIgor Opaniuk } else {
258d8f9d2afSIgor Opaniuk io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
259d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
260d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
261d8f9d2afSIgor Opaniuk goto out;
262d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
263d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Error determining partition size.\n", NULL);
264d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
265d8f9d2afSIgor Opaniuk goto out;
266d8f9d2afSIgor Opaniuk }
267d8f9d2afSIgor Opaniuk avb_debugv(part_name, ": Loading entire partition.\n", NULL);
268d8f9d2afSIgor Opaniuk }
269d8f9d2afSIgor Opaniuk }
270d8f9d2afSIgor Opaniuk
271d8f9d2afSIgor Opaniuk ret = load_full_partition(
272d8f9d2afSIgor Opaniuk ops, part_name, image_size, &image_buf, &image_preloaded);
273d8f9d2afSIgor Opaniuk if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
274d8f9d2afSIgor Opaniuk goto out;
275d8f9d2afSIgor Opaniuk }
276d8f9d2afSIgor Opaniuk
277d8f9d2afSIgor Opaniuk if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha256") == 0) {
278d8f9d2afSIgor Opaniuk AvbSHA256Ctx sha256_ctx;
279d8f9d2afSIgor Opaniuk avb_sha256_init(&sha256_ctx);
280d8f9d2afSIgor Opaniuk avb_sha256_update(&sha256_ctx, desc_salt, hash_desc.salt_len);
281d8f9d2afSIgor Opaniuk avb_sha256_update(&sha256_ctx, image_buf, hash_desc.image_size);
282d8f9d2afSIgor Opaniuk digest = avb_sha256_final(&sha256_ctx);
283d8f9d2afSIgor Opaniuk digest_len = AVB_SHA256_DIGEST_SIZE;
284d8f9d2afSIgor Opaniuk } else if (avb_strcmp((const char*)hash_desc.hash_algorithm, "sha512") == 0) {
285d8f9d2afSIgor Opaniuk AvbSHA512Ctx sha512_ctx;
286d8f9d2afSIgor Opaniuk avb_sha512_init(&sha512_ctx);
287d8f9d2afSIgor Opaniuk avb_sha512_update(&sha512_ctx, desc_salt, hash_desc.salt_len);
288d8f9d2afSIgor Opaniuk avb_sha512_update(&sha512_ctx, image_buf, hash_desc.image_size);
289d8f9d2afSIgor Opaniuk digest = avb_sha512_final(&sha512_ctx);
290d8f9d2afSIgor Opaniuk digest_len = AVB_SHA512_DIGEST_SIZE;
291d8f9d2afSIgor Opaniuk } else {
292d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL);
293d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
294d8f9d2afSIgor Opaniuk goto out;
295d8f9d2afSIgor Opaniuk }
296d8f9d2afSIgor Opaniuk
297d8f9d2afSIgor Opaniuk if (hash_desc.digest_len == 0) {
298d8f9d2afSIgor Opaniuk // Expect a match to a persistent digest.
299d8f9d2afSIgor Opaniuk avb_debugv(part_name, ": No digest, using persistent digest.\n", NULL);
300d8f9d2afSIgor Opaniuk expected_digest_len = digest_len;
301d8f9d2afSIgor Opaniuk expected_digest = expected_digest_buf;
302d8f9d2afSIgor Opaniuk avb_assert(expected_digest_len <= sizeof(expected_digest_buf));
303d8f9d2afSIgor Opaniuk ret =
304d8f9d2afSIgor Opaniuk read_persistent_digest(ops, part_name, digest_len, expected_digest_buf);
305d8f9d2afSIgor Opaniuk if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
306d8f9d2afSIgor Opaniuk goto out;
307d8f9d2afSIgor Opaniuk }
308d8f9d2afSIgor Opaniuk } else {
309d8f9d2afSIgor Opaniuk // Expect a match to the digest in the descriptor.
310d8f9d2afSIgor Opaniuk expected_digest_len = hash_desc.digest_len;
311d8f9d2afSIgor Opaniuk expected_digest = desc_digest;
312d8f9d2afSIgor Opaniuk }
313d8f9d2afSIgor Opaniuk
314d8f9d2afSIgor Opaniuk if (digest_len != expected_digest_len) {
315d8f9d2afSIgor Opaniuk avb_errorv(
316d8f9d2afSIgor Opaniuk part_name, ": Digest in descriptor not of expected size.\n", NULL);
317d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
318d8f9d2afSIgor Opaniuk goto out;
319d8f9d2afSIgor Opaniuk }
320d8f9d2afSIgor Opaniuk
321d8f9d2afSIgor Opaniuk if (avb_safe_memcmp(digest, expected_digest, digest_len) != 0) {
322d8f9d2afSIgor Opaniuk avb_errorv(part_name,
323d8f9d2afSIgor Opaniuk ": Hash of data does not match digest in descriptor.\n",
324d8f9d2afSIgor Opaniuk NULL);
325d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
326d8f9d2afSIgor Opaniuk goto out;
327d8f9d2afSIgor Opaniuk }
328d8f9d2afSIgor Opaniuk
329d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_OK;
330d8f9d2afSIgor Opaniuk
331d8f9d2afSIgor Opaniuk out:
332d8f9d2afSIgor Opaniuk
333d8f9d2afSIgor Opaniuk /* If it worked and something was loaded, copy to slot_data. */
334d8f9d2afSIgor Opaniuk if ((ret == AVB_SLOT_VERIFY_RESULT_OK || result_should_continue(ret)) &&
335d8f9d2afSIgor Opaniuk image_buf != NULL) {
336d8f9d2afSIgor Opaniuk AvbPartitionData* loaded_partition;
337d8f9d2afSIgor Opaniuk if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
338d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
339d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
340d8f9d2afSIgor Opaniuk goto fail;
341d8f9d2afSIgor Opaniuk }
342d8f9d2afSIgor Opaniuk loaded_partition =
343d8f9d2afSIgor Opaniuk &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
344d8f9d2afSIgor Opaniuk loaded_partition->partition_name = avb_strdup(found);
345d8f9d2afSIgor Opaniuk loaded_partition->data_size = image_size;
346d8f9d2afSIgor Opaniuk loaded_partition->data = image_buf;
347d8f9d2afSIgor Opaniuk loaded_partition->preloaded = image_preloaded;
348d8f9d2afSIgor Opaniuk image_buf = NULL;
349d8f9d2afSIgor Opaniuk }
350d8f9d2afSIgor Opaniuk
351d8f9d2afSIgor Opaniuk fail:
352d8f9d2afSIgor Opaniuk if (image_buf != NULL && !image_preloaded) {
353d8f9d2afSIgor Opaniuk avb_free(image_buf);
354d8f9d2afSIgor Opaniuk }
355d8f9d2afSIgor Opaniuk return ret;
356d8f9d2afSIgor Opaniuk }
357d8f9d2afSIgor Opaniuk
load_requested_partitions(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,AvbSlotVerifyData * slot_data)358d8f9d2afSIgor Opaniuk static AvbSlotVerifyResult load_requested_partitions(
359d8f9d2afSIgor Opaniuk AvbOps* ops,
360d8f9d2afSIgor Opaniuk const char* const* requested_partitions,
361d8f9d2afSIgor Opaniuk const char* ab_suffix,
362d8f9d2afSIgor Opaniuk AvbSlotVerifyData* slot_data) {
363d8f9d2afSIgor Opaniuk AvbSlotVerifyResult ret;
364d8f9d2afSIgor Opaniuk uint8_t* image_buf = NULL;
365d8f9d2afSIgor Opaniuk bool image_preloaded = false;
366d8f9d2afSIgor Opaniuk size_t n;
367d8f9d2afSIgor Opaniuk
368d8f9d2afSIgor Opaniuk if (ops->get_size_of_partition == NULL) {
369d8f9d2afSIgor Opaniuk avb_error("get_size_of_partition() not implemented.\n");
370d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
371d8f9d2afSIgor Opaniuk goto out;
372d8f9d2afSIgor Opaniuk }
373d8f9d2afSIgor Opaniuk
374d8f9d2afSIgor Opaniuk for (n = 0; requested_partitions[n] != NULL; n++) {
375d8f9d2afSIgor Opaniuk char part_name[AVB_PART_NAME_MAX_SIZE];
376d8f9d2afSIgor Opaniuk AvbIOResult io_ret;
377d8f9d2afSIgor Opaniuk uint64_t image_size;
378d8f9d2afSIgor Opaniuk AvbPartitionData* loaded_partition;
379d8f9d2afSIgor Opaniuk
380d8f9d2afSIgor Opaniuk if (!avb_str_concat(part_name,
381d8f9d2afSIgor Opaniuk sizeof part_name,
382d8f9d2afSIgor Opaniuk requested_partitions[n],
383d8f9d2afSIgor Opaniuk avb_strlen(requested_partitions[n]),
384d8f9d2afSIgor Opaniuk ab_suffix,
385d8f9d2afSIgor Opaniuk avb_strlen(ab_suffix))) {
386d8f9d2afSIgor Opaniuk avb_error("Partition name and suffix does not fit.\n");
387d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
388d8f9d2afSIgor Opaniuk goto out;
389d8f9d2afSIgor Opaniuk }
390d8f9d2afSIgor Opaniuk
391d8f9d2afSIgor Opaniuk io_ret = ops->get_size_of_partition(ops, part_name, &image_size);
392d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
393d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
394d8f9d2afSIgor Opaniuk goto out;
395d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
396d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Error determining partition size.\n", NULL);
397d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
398d8f9d2afSIgor Opaniuk goto out;
399d8f9d2afSIgor Opaniuk }
400d8f9d2afSIgor Opaniuk avb_debugv(part_name, ": Loading entire partition.\n", NULL);
401d8f9d2afSIgor Opaniuk
402d8f9d2afSIgor Opaniuk ret = load_full_partition(
403d8f9d2afSIgor Opaniuk ops, part_name, image_size, &image_buf, &image_preloaded);
404d8f9d2afSIgor Opaniuk if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
405d8f9d2afSIgor Opaniuk goto out;
406d8f9d2afSIgor Opaniuk }
407d8f9d2afSIgor Opaniuk
408d8f9d2afSIgor Opaniuk /* Move to slot_data. */
409d8f9d2afSIgor Opaniuk if (slot_data->num_loaded_partitions == MAX_NUMBER_OF_LOADED_PARTITIONS) {
410d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Too many loaded partitions.\n", NULL);
411d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
412d8f9d2afSIgor Opaniuk goto out;
413d8f9d2afSIgor Opaniuk }
414d8f9d2afSIgor Opaniuk loaded_partition =
415d8f9d2afSIgor Opaniuk &slot_data->loaded_partitions[slot_data->num_loaded_partitions++];
416d8f9d2afSIgor Opaniuk loaded_partition->partition_name = avb_strdup(requested_partitions[n]);
417d8f9d2afSIgor Opaniuk if (loaded_partition->partition_name == NULL) {
418d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
419d8f9d2afSIgor Opaniuk goto out;
420d8f9d2afSIgor Opaniuk }
421d8f9d2afSIgor Opaniuk loaded_partition->data_size = image_size;
422d8f9d2afSIgor Opaniuk loaded_partition->data = image_buf; /* Transferring the owner. */
423d8f9d2afSIgor Opaniuk loaded_partition->preloaded = image_preloaded;
424d8f9d2afSIgor Opaniuk image_buf = NULL;
425d8f9d2afSIgor Opaniuk image_preloaded = false;
426d8f9d2afSIgor Opaniuk }
427d8f9d2afSIgor Opaniuk
428d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_OK;
429d8f9d2afSIgor Opaniuk
430d8f9d2afSIgor Opaniuk out:
431d8f9d2afSIgor Opaniuk /* Free the current buffer if any. */
432d8f9d2afSIgor Opaniuk if (image_buf != NULL && !image_preloaded) {
433d8f9d2afSIgor Opaniuk avb_free(image_buf);
434d8f9d2afSIgor Opaniuk }
435d8f9d2afSIgor Opaniuk /* Buffers that are already saved in slot_data will be handled by the caller
436d8f9d2afSIgor Opaniuk * even on failure. */
437d8f9d2afSIgor Opaniuk return ret;
438d8f9d2afSIgor Opaniuk }
439d8f9d2afSIgor Opaniuk
load_and_verify_vbmeta(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,bool allow_verification_error,AvbVBMetaImageFlags toplevel_vbmeta_flags,int rollback_index_location,const char * partition_name,size_t partition_name_len,const uint8_t * expected_public_key,size_t expected_public_key_length,AvbSlotVerifyData * slot_data,AvbAlgorithmType * out_algorithm_type,AvbCmdlineSubstList * out_additional_cmdline_subst)440d8f9d2afSIgor Opaniuk static AvbSlotVerifyResult load_and_verify_vbmeta(
441d8f9d2afSIgor Opaniuk AvbOps* ops,
442d8f9d2afSIgor Opaniuk const char* const* requested_partitions,
443d8f9d2afSIgor Opaniuk const char* ab_suffix,
444d8f9d2afSIgor Opaniuk bool allow_verification_error,
445d8f9d2afSIgor Opaniuk AvbVBMetaImageFlags toplevel_vbmeta_flags,
446d8f9d2afSIgor Opaniuk int rollback_index_location,
447d8f9d2afSIgor Opaniuk const char* partition_name,
448d8f9d2afSIgor Opaniuk size_t partition_name_len,
449d8f9d2afSIgor Opaniuk const uint8_t* expected_public_key,
450d8f9d2afSIgor Opaniuk size_t expected_public_key_length,
451d8f9d2afSIgor Opaniuk AvbSlotVerifyData* slot_data,
452d8f9d2afSIgor Opaniuk AvbAlgorithmType* out_algorithm_type,
453d8f9d2afSIgor Opaniuk AvbCmdlineSubstList* out_additional_cmdline_subst) {
454d8f9d2afSIgor Opaniuk char full_partition_name[AVB_PART_NAME_MAX_SIZE];
455d8f9d2afSIgor Opaniuk AvbSlotVerifyResult ret;
456d8f9d2afSIgor Opaniuk AvbIOResult io_ret;
457d8f9d2afSIgor Opaniuk size_t vbmeta_offset;
458d8f9d2afSIgor Opaniuk size_t vbmeta_size;
459d8f9d2afSIgor Opaniuk uint8_t* vbmeta_buf = NULL;
460d8f9d2afSIgor Opaniuk size_t vbmeta_num_read;
461d8f9d2afSIgor Opaniuk AvbVBMetaVerifyResult vbmeta_ret;
462d8f9d2afSIgor Opaniuk const uint8_t* pk_data;
463d8f9d2afSIgor Opaniuk size_t pk_len;
464d8f9d2afSIgor Opaniuk AvbVBMetaImageHeader vbmeta_header;
465d8f9d2afSIgor Opaniuk uint64_t stored_rollback_index;
466d8f9d2afSIgor Opaniuk const AvbDescriptor** descriptors = NULL;
467d8f9d2afSIgor Opaniuk size_t num_descriptors;
468d8f9d2afSIgor Opaniuk size_t n;
469d8f9d2afSIgor Opaniuk bool is_main_vbmeta;
470d8f9d2afSIgor Opaniuk bool is_vbmeta_partition;
471d8f9d2afSIgor Opaniuk AvbVBMetaData* vbmeta_image_data = NULL;
472d8f9d2afSIgor Opaniuk
473d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_OK;
474d8f9d2afSIgor Opaniuk
475d8f9d2afSIgor Opaniuk avb_assert(slot_data != NULL);
476d8f9d2afSIgor Opaniuk
477d8f9d2afSIgor Opaniuk /* Since we allow top-level vbmeta in 'boot', use
478d8f9d2afSIgor Opaniuk * rollback_index_location to determine whether we're the main
479d8f9d2afSIgor Opaniuk * vbmeta struct.
480d8f9d2afSIgor Opaniuk */
481d8f9d2afSIgor Opaniuk is_main_vbmeta = (rollback_index_location == 0);
482d8f9d2afSIgor Opaniuk is_vbmeta_partition = (avb_strcmp(partition_name, "vbmeta") == 0);
483d8f9d2afSIgor Opaniuk
484d8f9d2afSIgor Opaniuk if (!avb_validate_utf8((const uint8_t*)partition_name, partition_name_len)) {
485d8f9d2afSIgor Opaniuk avb_error("Partition name is not valid UTF-8.\n");
486d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
487d8f9d2afSIgor Opaniuk goto out;
488d8f9d2afSIgor Opaniuk }
489d8f9d2afSIgor Opaniuk
490d8f9d2afSIgor Opaniuk /* Construct full partition name. */
491d8f9d2afSIgor Opaniuk if (!avb_str_concat(full_partition_name,
492d8f9d2afSIgor Opaniuk sizeof full_partition_name,
493d8f9d2afSIgor Opaniuk partition_name,
494d8f9d2afSIgor Opaniuk partition_name_len,
495d8f9d2afSIgor Opaniuk ab_suffix,
496d8f9d2afSIgor Opaniuk avb_strlen(ab_suffix))) {
497d8f9d2afSIgor Opaniuk avb_error("Partition name and suffix does not fit.\n");
498d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
499d8f9d2afSIgor Opaniuk goto out;
500d8f9d2afSIgor Opaniuk }
501d8f9d2afSIgor Opaniuk
502d8f9d2afSIgor Opaniuk avb_debugv("Loading vbmeta struct from partition '",
503d8f9d2afSIgor Opaniuk full_partition_name,
504d8f9d2afSIgor Opaniuk "'.\n",
505d8f9d2afSIgor Opaniuk NULL);
506d8f9d2afSIgor Opaniuk
507d8f9d2afSIgor Opaniuk /* If we're loading from the main vbmeta partition, the vbmeta
508d8f9d2afSIgor Opaniuk * struct is in the beginning. Otherwise we have to locate it via a
509d8f9d2afSIgor Opaniuk * footer.
510d8f9d2afSIgor Opaniuk */
511d8f9d2afSIgor Opaniuk if (is_vbmeta_partition) {
512d8f9d2afSIgor Opaniuk vbmeta_offset = 0;
513d8f9d2afSIgor Opaniuk vbmeta_size = VBMETA_MAX_SIZE;
514d8f9d2afSIgor Opaniuk } else {
515d8f9d2afSIgor Opaniuk uint8_t footer_buf[AVB_FOOTER_SIZE];
516d8f9d2afSIgor Opaniuk size_t footer_num_read;
517d8f9d2afSIgor Opaniuk AvbFooter footer;
518d8f9d2afSIgor Opaniuk
519d8f9d2afSIgor Opaniuk io_ret = ops->read_from_partition(ops,
520d8f9d2afSIgor Opaniuk full_partition_name,
521d8f9d2afSIgor Opaniuk -AVB_FOOTER_SIZE,
522d8f9d2afSIgor Opaniuk AVB_FOOTER_SIZE,
523d8f9d2afSIgor Opaniuk footer_buf,
524d8f9d2afSIgor Opaniuk &footer_num_read);
525d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
526d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
527d8f9d2afSIgor Opaniuk goto out;
528d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
529d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name, ": Error loading footer.\n", NULL);
530d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
531d8f9d2afSIgor Opaniuk goto out;
532d8f9d2afSIgor Opaniuk }
533d8f9d2afSIgor Opaniuk avb_assert(footer_num_read == AVB_FOOTER_SIZE);
534d8f9d2afSIgor Opaniuk
535d8f9d2afSIgor Opaniuk if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf,
536d8f9d2afSIgor Opaniuk &footer)) {
537d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name, ": Error validating footer.\n", NULL);
538d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
539d8f9d2afSIgor Opaniuk goto out;
540d8f9d2afSIgor Opaniuk }
541d8f9d2afSIgor Opaniuk
542d8f9d2afSIgor Opaniuk /* Basic footer sanity check since the data is untrusted. */
543d8f9d2afSIgor Opaniuk if (footer.vbmeta_size > VBMETA_MAX_SIZE) {
544d8f9d2afSIgor Opaniuk avb_errorv(
545d8f9d2afSIgor Opaniuk full_partition_name, ": Invalid vbmeta size in footer.\n", NULL);
546d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
547d8f9d2afSIgor Opaniuk goto out;
548d8f9d2afSIgor Opaniuk }
549d8f9d2afSIgor Opaniuk
550d8f9d2afSIgor Opaniuk vbmeta_offset = footer.vbmeta_offset;
551d8f9d2afSIgor Opaniuk vbmeta_size = footer.vbmeta_size;
552d8f9d2afSIgor Opaniuk }
553d8f9d2afSIgor Opaniuk
554d8f9d2afSIgor Opaniuk vbmeta_buf = avb_malloc(vbmeta_size);
555d8f9d2afSIgor Opaniuk if (vbmeta_buf == NULL) {
556d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
557d8f9d2afSIgor Opaniuk goto out;
558d8f9d2afSIgor Opaniuk }
559d8f9d2afSIgor Opaniuk
560d8f9d2afSIgor Opaniuk io_ret = ops->read_from_partition(ops,
561d8f9d2afSIgor Opaniuk full_partition_name,
562d8f9d2afSIgor Opaniuk vbmeta_offset,
563d8f9d2afSIgor Opaniuk vbmeta_size,
564d8f9d2afSIgor Opaniuk vbmeta_buf,
565d8f9d2afSIgor Opaniuk &vbmeta_num_read);
566d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
567d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
568d8f9d2afSIgor Opaniuk goto out;
569d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
570d8f9d2afSIgor Opaniuk /* If we're looking for 'vbmeta' but there is no such partition,
571d8f9d2afSIgor Opaniuk * go try to get it from the boot partition instead.
572d8f9d2afSIgor Opaniuk */
573d8f9d2afSIgor Opaniuk if (is_main_vbmeta && io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION &&
574d8f9d2afSIgor Opaniuk is_vbmeta_partition) {
575d8f9d2afSIgor Opaniuk avb_debugv(full_partition_name,
576d8f9d2afSIgor Opaniuk ": No such partition. Trying 'boot' instead.\n",
577d8f9d2afSIgor Opaniuk NULL);
578d8f9d2afSIgor Opaniuk ret = load_and_verify_vbmeta(ops,
579d8f9d2afSIgor Opaniuk requested_partitions,
580d8f9d2afSIgor Opaniuk ab_suffix,
581d8f9d2afSIgor Opaniuk allow_verification_error,
582d8f9d2afSIgor Opaniuk 0 /* toplevel_vbmeta_flags */,
583d8f9d2afSIgor Opaniuk 0 /* rollback_index_location */,
584d8f9d2afSIgor Opaniuk "boot",
585d8f9d2afSIgor Opaniuk avb_strlen("boot"),
586d8f9d2afSIgor Opaniuk NULL /* expected_public_key */,
587d8f9d2afSIgor Opaniuk 0 /* expected_public_key_length */,
588d8f9d2afSIgor Opaniuk slot_data,
589d8f9d2afSIgor Opaniuk out_algorithm_type,
590d8f9d2afSIgor Opaniuk out_additional_cmdline_subst);
591d8f9d2afSIgor Opaniuk goto out;
592d8f9d2afSIgor Opaniuk } else {
593d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name, ": Error loading vbmeta data.\n", NULL);
594d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
595d8f9d2afSIgor Opaniuk goto out;
596d8f9d2afSIgor Opaniuk }
597d8f9d2afSIgor Opaniuk }
598d8f9d2afSIgor Opaniuk avb_assert(vbmeta_num_read <= vbmeta_size);
599d8f9d2afSIgor Opaniuk
600d8f9d2afSIgor Opaniuk /* Check if the image is properly signed and get the public key used
601d8f9d2afSIgor Opaniuk * to sign the image.
602d8f9d2afSIgor Opaniuk */
603d8f9d2afSIgor Opaniuk vbmeta_ret =
604d8f9d2afSIgor Opaniuk avb_vbmeta_image_verify(vbmeta_buf, vbmeta_num_read, &pk_data, &pk_len);
605d8f9d2afSIgor Opaniuk switch (vbmeta_ret) {
606d8f9d2afSIgor Opaniuk case AVB_VBMETA_VERIFY_RESULT_OK:
607d8f9d2afSIgor Opaniuk avb_assert(pk_data != NULL && pk_len > 0);
608d8f9d2afSIgor Opaniuk break;
609d8f9d2afSIgor Opaniuk
610d8f9d2afSIgor Opaniuk case AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED:
611d8f9d2afSIgor Opaniuk case AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH:
612d8f9d2afSIgor Opaniuk case AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH:
613d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION;
614d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
615d8f9d2afSIgor Opaniuk ": Error verifying vbmeta image: ",
616d8f9d2afSIgor Opaniuk avb_vbmeta_verify_result_to_string(vbmeta_ret),
617d8f9d2afSIgor Opaniuk "\n",
618d8f9d2afSIgor Opaniuk NULL);
619d8f9d2afSIgor Opaniuk if (!allow_verification_error) {
620d8f9d2afSIgor Opaniuk goto out;
621d8f9d2afSIgor Opaniuk }
622d8f9d2afSIgor Opaniuk break;
623d8f9d2afSIgor Opaniuk
624d8f9d2afSIgor Opaniuk case AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER:
625d8f9d2afSIgor Opaniuk /* No way to continue this case. */
626d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
627d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
628d8f9d2afSIgor Opaniuk ": Error verifying vbmeta image: invalid vbmeta header\n",
629d8f9d2afSIgor Opaniuk NULL);
630d8f9d2afSIgor Opaniuk goto out;
631d8f9d2afSIgor Opaniuk
632d8f9d2afSIgor Opaniuk case AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION:
633d8f9d2afSIgor Opaniuk /* No way to continue this case. */
634d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION;
635d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
636d8f9d2afSIgor Opaniuk ": Error verifying vbmeta image: unsupported AVB version\n",
637d8f9d2afSIgor Opaniuk NULL);
638d8f9d2afSIgor Opaniuk goto out;
639d8f9d2afSIgor Opaniuk }
640d8f9d2afSIgor Opaniuk
641d8f9d2afSIgor Opaniuk /* Byteswap the header. */
642d8f9d2afSIgor Opaniuk avb_vbmeta_image_header_to_host_byte_order((AvbVBMetaImageHeader*)vbmeta_buf,
643d8f9d2afSIgor Opaniuk &vbmeta_header);
644d8f9d2afSIgor Opaniuk
645d8f9d2afSIgor Opaniuk /* If we're the toplevel, assign flags so they'll be passed down. */
646d8f9d2afSIgor Opaniuk if (is_main_vbmeta) {
647d8f9d2afSIgor Opaniuk toplevel_vbmeta_flags = (AvbVBMetaImageFlags)vbmeta_header.flags;
648d8f9d2afSIgor Opaniuk } else {
649d8f9d2afSIgor Opaniuk if (vbmeta_header.flags != 0) {
650d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
651d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
652d8f9d2afSIgor Opaniuk ": chained vbmeta image has non-zero flags\n",
653d8f9d2afSIgor Opaniuk NULL);
654d8f9d2afSIgor Opaniuk goto out;
655d8f9d2afSIgor Opaniuk }
656d8f9d2afSIgor Opaniuk }
657d8f9d2afSIgor Opaniuk
658d8f9d2afSIgor Opaniuk /* Check if key used to make signature matches what is expected. */
659d8f9d2afSIgor Opaniuk if (pk_data != NULL) {
660d8f9d2afSIgor Opaniuk if (expected_public_key != NULL) {
661d8f9d2afSIgor Opaniuk avb_assert(!is_main_vbmeta);
662d8f9d2afSIgor Opaniuk if (expected_public_key_length != pk_len ||
663d8f9d2afSIgor Opaniuk avb_safe_memcmp(expected_public_key, pk_data, pk_len) != 0) {
664d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
665d8f9d2afSIgor Opaniuk ": Public key used to sign data does not match key in chain "
666d8f9d2afSIgor Opaniuk "partition descriptor.\n",
667d8f9d2afSIgor Opaniuk NULL);
668d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
669d8f9d2afSIgor Opaniuk if (!allow_verification_error) {
670d8f9d2afSIgor Opaniuk goto out;
671d8f9d2afSIgor Opaniuk }
672d8f9d2afSIgor Opaniuk }
673d8f9d2afSIgor Opaniuk } else {
674d8f9d2afSIgor Opaniuk bool key_is_trusted = false;
675d8f9d2afSIgor Opaniuk const uint8_t* pk_metadata = NULL;
676d8f9d2afSIgor Opaniuk size_t pk_metadata_len = 0;
677d8f9d2afSIgor Opaniuk
678d8f9d2afSIgor Opaniuk if (vbmeta_header.public_key_metadata_size > 0) {
679d8f9d2afSIgor Opaniuk pk_metadata = vbmeta_buf + sizeof(AvbVBMetaImageHeader) +
680d8f9d2afSIgor Opaniuk vbmeta_header.authentication_data_block_size +
681d8f9d2afSIgor Opaniuk vbmeta_header.public_key_metadata_offset;
682d8f9d2afSIgor Opaniuk pk_metadata_len = vbmeta_header.public_key_metadata_size;
683d8f9d2afSIgor Opaniuk }
684d8f9d2afSIgor Opaniuk
685d8f9d2afSIgor Opaniuk avb_assert(is_main_vbmeta);
686d8f9d2afSIgor Opaniuk io_ret = ops->validate_vbmeta_public_key(
687d8f9d2afSIgor Opaniuk ops, pk_data, pk_len, pk_metadata, pk_metadata_len, &key_is_trusted);
688d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
689d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
690d8f9d2afSIgor Opaniuk goto out;
691d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
692d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
693d8f9d2afSIgor Opaniuk ": Error while checking public key used to sign data.\n",
694d8f9d2afSIgor Opaniuk NULL);
695d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
696d8f9d2afSIgor Opaniuk goto out;
697d8f9d2afSIgor Opaniuk }
698d8f9d2afSIgor Opaniuk if (!key_is_trusted) {
699d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
700d8f9d2afSIgor Opaniuk ": Public key used to sign data rejected.\n",
701d8f9d2afSIgor Opaniuk NULL);
702d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED;
703d8f9d2afSIgor Opaniuk if (!allow_verification_error) {
704d8f9d2afSIgor Opaniuk goto out;
705d8f9d2afSIgor Opaniuk }
706d8f9d2afSIgor Opaniuk }
707d8f9d2afSIgor Opaniuk }
708d8f9d2afSIgor Opaniuk }
709d8f9d2afSIgor Opaniuk
710d8f9d2afSIgor Opaniuk /* Check rollback index. */
711d8f9d2afSIgor Opaniuk io_ret = ops->read_rollback_index(
712d8f9d2afSIgor Opaniuk ops, rollback_index_location, &stored_rollback_index);
713d8f9d2afSIgor Opaniuk if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
714d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
715d8f9d2afSIgor Opaniuk goto out;
716d8f9d2afSIgor Opaniuk } else if (io_ret != AVB_IO_RESULT_OK) {
717d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
718d8f9d2afSIgor Opaniuk ": Error getting rollback index for location.\n",
719d8f9d2afSIgor Opaniuk NULL);
720d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
721d8f9d2afSIgor Opaniuk goto out;
722d8f9d2afSIgor Opaniuk }
723d8f9d2afSIgor Opaniuk if (vbmeta_header.rollback_index < stored_rollback_index) {
724d8f9d2afSIgor Opaniuk avb_errorv(
725d8f9d2afSIgor Opaniuk full_partition_name,
726d8f9d2afSIgor Opaniuk ": Image rollback index is less than the stored rollback index.\n",
727d8f9d2afSIgor Opaniuk NULL);
728d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX;
729d8f9d2afSIgor Opaniuk if (!allow_verification_error) {
730d8f9d2afSIgor Opaniuk goto out;
731d8f9d2afSIgor Opaniuk }
732d8f9d2afSIgor Opaniuk }
733d8f9d2afSIgor Opaniuk
734d8f9d2afSIgor Opaniuk /* Copy vbmeta to vbmeta_images before recursing. */
735d8f9d2afSIgor Opaniuk if (is_main_vbmeta) {
736d8f9d2afSIgor Opaniuk avb_assert(slot_data->num_vbmeta_images == 0);
737d8f9d2afSIgor Opaniuk } else {
738d8f9d2afSIgor Opaniuk avb_assert(slot_data->num_vbmeta_images > 0);
739d8f9d2afSIgor Opaniuk }
740d8f9d2afSIgor Opaniuk if (slot_data->num_vbmeta_images == MAX_NUMBER_OF_VBMETA_IMAGES) {
741d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name, ": Too many vbmeta images.\n", NULL);
742d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
743d8f9d2afSIgor Opaniuk goto out;
744d8f9d2afSIgor Opaniuk }
745d8f9d2afSIgor Opaniuk vbmeta_image_data = &slot_data->vbmeta_images[slot_data->num_vbmeta_images++];
746d8f9d2afSIgor Opaniuk vbmeta_image_data->partition_name = avb_strdup(partition_name);
747d8f9d2afSIgor Opaniuk vbmeta_image_data->vbmeta_data = vbmeta_buf;
748d8f9d2afSIgor Opaniuk /* Note that |vbmeta_buf| is actually |vbmeta_num_read| bytes long
749d8f9d2afSIgor Opaniuk * and this includes data past the end of the image. Pass the
750d8f9d2afSIgor Opaniuk * actual size of the vbmeta image. Also, no need to use
751d8f9d2afSIgor Opaniuk * avb_safe_add() since the header has already been verified.
752d8f9d2afSIgor Opaniuk */
753d8f9d2afSIgor Opaniuk vbmeta_image_data->vbmeta_size =
754d8f9d2afSIgor Opaniuk sizeof(AvbVBMetaImageHeader) +
755d8f9d2afSIgor Opaniuk vbmeta_header.authentication_data_block_size +
756d8f9d2afSIgor Opaniuk vbmeta_header.auxiliary_data_block_size;
757d8f9d2afSIgor Opaniuk vbmeta_image_data->verify_result = vbmeta_ret;
758d8f9d2afSIgor Opaniuk
759d8f9d2afSIgor Opaniuk /* If verification has been disabled by setting a bit in the image,
760d8f9d2afSIgor Opaniuk * we're done... except that we need to load the entirety of the
761d8f9d2afSIgor Opaniuk * requested partitions.
762d8f9d2afSIgor Opaniuk */
763d8f9d2afSIgor Opaniuk if (vbmeta_header.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
764d8f9d2afSIgor Opaniuk AvbSlotVerifyResult sub_ret;
765d8f9d2afSIgor Opaniuk avb_debugv(
766d8f9d2afSIgor Opaniuk full_partition_name, ": VERIFICATION_DISABLED bit is set.\n", NULL);
767d8f9d2afSIgor Opaniuk /* If load_requested_partitions() fail it is always a fatal
768d8f9d2afSIgor Opaniuk * failure (e.g. ERROR_INVALID_ARGUMENT, ERROR_OOM, etc.) rather
769d8f9d2afSIgor Opaniuk * than recoverable (e.g. one where result_should_continue()
770d8f9d2afSIgor Opaniuk * returns true) and we want to convey that error.
771d8f9d2afSIgor Opaniuk */
772d8f9d2afSIgor Opaniuk sub_ret = load_requested_partitions(
773d8f9d2afSIgor Opaniuk ops, requested_partitions, ab_suffix, slot_data);
774d8f9d2afSIgor Opaniuk if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
775d8f9d2afSIgor Opaniuk ret = sub_ret;
776d8f9d2afSIgor Opaniuk }
777d8f9d2afSIgor Opaniuk goto out;
778d8f9d2afSIgor Opaniuk }
779d8f9d2afSIgor Opaniuk
780d8f9d2afSIgor Opaniuk /* Now go through all descriptors and take the appropriate action:
781d8f9d2afSIgor Opaniuk *
782d8f9d2afSIgor Opaniuk * - hash descriptor: Load data from partition, calculate hash, and
783d8f9d2afSIgor Opaniuk * checks that it matches what's in the hash descriptor.
784d8f9d2afSIgor Opaniuk *
785d8f9d2afSIgor Opaniuk * - hashtree descriptor: Do nothing since verification happens
786d8f9d2afSIgor Opaniuk * on-the-fly from within the OS. (Unless the descriptor uses a
787d8f9d2afSIgor Opaniuk * persistent digest, in which case we need to find it).
788d8f9d2afSIgor Opaniuk *
789d8f9d2afSIgor Opaniuk * - chained partition descriptor: Load the footer, load the vbmeta
790d8f9d2afSIgor Opaniuk * image, verify vbmeta image (includes rollback checks, hash
791d8f9d2afSIgor Opaniuk * checks, bail on chained partitions).
792d8f9d2afSIgor Opaniuk */
793d8f9d2afSIgor Opaniuk descriptors =
794d8f9d2afSIgor Opaniuk avb_descriptor_get_all(vbmeta_buf, vbmeta_num_read, &num_descriptors);
795d8f9d2afSIgor Opaniuk for (n = 0; n < num_descriptors; n++) {
796d8f9d2afSIgor Opaniuk AvbDescriptor desc;
797d8f9d2afSIgor Opaniuk
798d8f9d2afSIgor Opaniuk if (!avb_descriptor_validate_and_byteswap(descriptors[n], &desc)) {
799d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name, ": Descriptor is invalid.\n", NULL);
800d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
801d8f9d2afSIgor Opaniuk goto out;
802d8f9d2afSIgor Opaniuk }
803d8f9d2afSIgor Opaniuk
804d8f9d2afSIgor Opaniuk switch (desc.tag) {
805d8f9d2afSIgor Opaniuk case AVB_DESCRIPTOR_TAG_HASH: {
806d8f9d2afSIgor Opaniuk AvbSlotVerifyResult sub_ret;
807d8f9d2afSIgor Opaniuk sub_ret = load_and_verify_hash_partition(ops,
808d8f9d2afSIgor Opaniuk requested_partitions,
809d8f9d2afSIgor Opaniuk ab_suffix,
810d8f9d2afSIgor Opaniuk allow_verification_error,
811d8f9d2afSIgor Opaniuk descriptors[n],
812d8f9d2afSIgor Opaniuk slot_data);
813d8f9d2afSIgor Opaniuk if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
814d8f9d2afSIgor Opaniuk ret = sub_ret;
815d8f9d2afSIgor Opaniuk if (!allow_verification_error || !result_should_continue(ret)) {
816d8f9d2afSIgor Opaniuk goto out;
817d8f9d2afSIgor Opaniuk }
818d8f9d2afSIgor Opaniuk }
819d8f9d2afSIgor Opaniuk } break;
820d8f9d2afSIgor Opaniuk
821d8f9d2afSIgor Opaniuk case AVB_DESCRIPTOR_TAG_CHAIN_PARTITION: {
822d8f9d2afSIgor Opaniuk AvbSlotVerifyResult sub_ret;
823d8f9d2afSIgor Opaniuk AvbChainPartitionDescriptor chain_desc;
824d8f9d2afSIgor Opaniuk const uint8_t* chain_partition_name;
825d8f9d2afSIgor Opaniuk const uint8_t* chain_public_key;
826d8f9d2afSIgor Opaniuk
827d8f9d2afSIgor Opaniuk /* Only allow CHAIN_PARTITION descriptors in the main vbmeta image. */
828d8f9d2afSIgor Opaniuk if (!is_main_vbmeta) {
829d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
830d8f9d2afSIgor Opaniuk ": Encountered chain descriptor not in main image.\n",
831d8f9d2afSIgor Opaniuk NULL);
832d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
833d8f9d2afSIgor Opaniuk goto out;
834d8f9d2afSIgor Opaniuk }
835d8f9d2afSIgor Opaniuk
836d8f9d2afSIgor Opaniuk if (!avb_chain_partition_descriptor_validate_and_byteswap(
837d8f9d2afSIgor Opaniuk (AvbChainPartitionDescriptor*)descriptors[n], &chain_desc)) {
838d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
839d8f9d2afSIgor Opaniuk ": Chain partition descriptor is invalid.\n",
840d8f9d2afSIgor Opaniuk NULL);
841d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
842d8f9d2afSIgor Opaniuk goto out;
843d8f9d2afSIgor Opaniuk }
844d8f9d2afSIgor Opaniuk
845d8f9d2afSIgor Opaniuk if (chain_desc.rollback_index_location == 0) {
846d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
847d8f9d2afSIgor Opaniuk ": Chain partition has invalid "
848d8f9d2afSIgor Opaniuk "rollback_index_location field.\n",
849d8f9d2afSIgor Opaniuk NULL);
850d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
851d8f9d2afSIgor Opaniuk goto out;
852d8f9d2afSIgor Opaniuk }
853d8f9d2afSIgor Opaniuk
854d8f9d2afSIgor Opaniuk chain_partition_name = ((const uint8_t*)descriptors[n]) +
855d8f9d2afSIgor Opaniuk sizeof(AvbChainPartitionDescriptor);
856d8f9d2afSIgor Opaniuk chain_public_key = chain_partition_name + chain_desc.partition_name_len;
857d8f9d2afSIgor Opaniuk
858d8f9d2afSIgor Opaniuk sub_ret =
859d8f9d2afSIgor Opaniuk load_and_verify_vbmeta(ops,
860d8f9d2afSIgor Opaniuk requested_partitions,
861d8f9d2afSIgor Opaniuk ab_suffix,
862d8f9d2afSIgor Opaniuk allow_verification_error,
863d8f9d2afSIgor Opaniuk toplevel_vbmeta_flags,
864d8f9d2afSIgor Opaniuk chain_desc.rollback_index_location,
865d8f9d2afSIgor Opaniuk (const char*)chain_partition_name,
866d8f9d2afSIgor Opaniuk chain_desc.partition_name_len,
867d8f9d2afSIgor Opaniuk chain_public_key,
868d8f9d2afSIgor Opaniuk chain_desc.public_key_len,
869d8f9d2afSIgor Opaniuk slot_data,
870d8f9d2afSIgor Opaniuk NULL, /* out_algorithm_type */
871d8f9d2afSIgor Opaniuk NULL /* out_additional_cmdline_subst */);
872d8f9d2afSIgor Opaniuk if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
873d8f9d2afSIgor Opaniuk ret = sub_ret;
874d8f9d2afSIgor Opaniuk if (!result_should_continue(ret)) {
875d8f9d2afSIgor Opaniuk goto out;
876d8f9d2afSIgor Opaniuk }
877d8f9d2afSIgor Opaniuk }
878d8f9d2afSIgor Opaniuk } break;
879d8f9d2afSIgor Opaniuk
880d8f9d2afSIgor Opaniuk case AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE: {
881d8f9d2afSIgor Opaniuk const uint8_t* kernel_cmdline;
882d8f9d2afSIgor Opaniuk AvbKernelCmdlineDescriptor kernel_cmdline_desc;
883d8f9d2afSIgor Opaniuk bool apply_cmdline;
884d8f9d2afSIgor Opaniuk
885d8f9d2afSIgor Opaniuk if (!avb_kernel_cmdline_descriptor_validate_and_byteswap(
886d8f9d2afSIgor Opaniuk (AvbKernelCmdlineDescriptor*)descriptors[n],
887d8f9d2afSIgor Opaniuk &kernel_cmdline_desc)) {
888d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
889d8f9d2afSIgor Opaniuk ": Kernel cmdline descriptor is invalid.\n",
890d8f9d2afSIgor Opaniuk NULL);
891d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
892d8f9d2afSIgor Opaniuk goto out;
893d8f9d2afSIgor Opaniuk }
894d8f9d2afSIgor Opaniuk
895d8f9d2afSIgor Opaniuk kernel_cmdline = ((const uint8_t*)descriptors[n]) +
896d8f9d2afSIgor Opaniuk sizeof(AvbKernelCmdlineDescriptor);
897d8f9d2afSIgor Opaniuk
898d8f9d2afSIgor Opaniuk if (!avb_validate_utf8(kernel_cmdline,
899d8f9d2afSIgor Opaniuk kernel_cmdline_desc.kernel_cmdline_length)) {
900d8f9d2afSIgor Opaniuk avb_errorv(full_partition_name,
901d8f9d2afSIgor Opaniuk ": Kernel cmdline is not valid UTF-8.\n",
902d8f9d2afSIgor Opaniuk NULL);
903d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
904d8f9d2afSIgor Opaniuk goto out;
905d8f9d2afSIgor Opaniuk }
906d8f9d2afSIgor Opaniuk
907d8f9d2afSIgor Opaniuk /* Compare the flags for top-level VBMeta struct with flags in
908d8f9d2afSIgor Opaniuk * the command-line descriptor so command-line snippets only
909d8f9d2afSIgor Opaniuk * intended for a certain mode (dm-verity enabled/disabled)
910d8f9d2afSIgor Opaniuk * are skipped if applicable.
911d8f9d2afSIgor Opaniuk */
912d8f9d2afSIgor Opaniuk apply_cmdline = true;
913d8f9d2afSIgor Opaniuk if (toplevel_vbmeta_flags & AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED) {
914d8f9d2afSIgor Opaniuk if (kernel_cmdline_desc.flags &
915d8f9d2afSIgor Opaniuk AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_NOT_DISABLED) {
916d8f9d2afSIgor Opaniuk apply_cmdline = false;
917d8f9d2afSIgor Opaniuk }
918d8f9d2afSIgor Opaniuk } else {
919d8f9d2afSIgor Opaniuk if (kernel_cmdline_desc.flags &
920d8f9d2afSIgor Opaniuk AVB_KERNEL_CMDLINE_FLAGS_USE_ONLY_IF_HASHTREE_DISABLED) {
921d8f9d2afSIgor Opaniuk apply_cmdline = false;
922d8f9d2afSIgor Opaniuk }
923d8f9d2afSIgor Opaniuk }
924d8f9d2afSIgor Opaniuk
925d8f9d2afSIgor Opaniuk if (apply_cmdline) {
926d8f9d2afSIgor Opaniuk if (slot_data->cmdline == NULL) {
927d8f9d2afSIgor Opaniuk slot_data->cmdline =
928d8f9d2afSIgor Opaniuk avb_calloc(kernel_cmdline_desc.kernel_cmdline_length + 1);
929d8f9d2afSIgor Opaniuk if (slot_data->cmdline == NULL) {
930d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
931d8f9d2afSIgor Opaniuk goto out;
932d8f9d2afSIgor Opaniuk }
933d8f9d2afSIgor Opaniuk avb_memcpy(slot_data->cmdline,
934d8f9d2afSIgor Opaniuk kernel_cmdline,
935d8f9d2afSIgor Opaniuk kernel_cmdline_desc.kernel_cmdline_length);
936d8f9d2afSIgor Opaniuk } else {
937d8f9d2afSIgor Opaniuk /* new cmdline is: <existing_cmdline> + ' ' + <newcmdline> + '\0' */
938d8f9d2afSIgor Opaniuk size_t orig_size = avb_strlen(slot_data->cmdline);
939d8f9d2afSIgor Opaniuk size_t new_size =
940d8f9d2afSIgor Opaniuk orig_size + 1 + kernel_cmdline_desc.kernel_cmdline_length + 1;
941d8f9d2afSIgor Opaniuk char* new_cmdline = avb_calloc(new_size);
942d8f9d2afSIgor Opaniuk if (new_cmdline == NULL) {
943d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
944d8f9d2afSIgor Opaniuk goto out;
945d8f9d2afSIgor Opaniuk }
946d8f9d2afSIgor Opaniuk avb_memcpy(new_cmdline, slot_data->cmdline, orig_size);
947d8f9d2afSIgor Opaniuk new_cmdline[orig_size] = ' ';
948d8f9d2afSIgor Opaniuk avb_memcpy(new_cmdline + orig_size + 1,
949d8f9d2afSIgor Opaniuk kernel_cmdline,
950d8f9d2afSIgor Opaniuk kernel_cmdline_desc.kernel_cmdline_length);
951d8f9d2afSIgor Opaniuk avb_free(slot_data->cmdline);
952d8f9d2afSIgor Opaniuk slot_data->cmdline = new_cmdline;
953d8f9d2afSIgor Opaniuk }
954d8f9d2afSIgor Opaniuk }
955d8f9d2afSIgor Opaniuk } break;
956d8f9d2afSIgor Opaniuk
957d8f9d2afSIgor Opaniuk case AVB_DESCRIPTOR_TAG_HASHTREE: {
958d8f9d2afSIgor Opaniuk AvbHashtreeDescriptor hashtree_desc;
959d8f9d2afSIgor Opaniuk
960d8f9d2afSIgor Opaniuk if (!avb_hashtree_descriptor_validate_and_byteswap(
961d8f9d2afSIgor Opaniuk (AvbHashtreeDescriptor*)descriptors[n], &hashtree_desc)) {
962d8f9d2afSIgor Opaniuk avb_errorv(
963d8f9d2afSIgor Opaniuk full_partition_name, ": Hashtree descriptor is invalid.\n", NULL);
964d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
965d8f9d2afSIgor Opaniuk goto out;
966d8f9d2afSIgor Opaniuk }
967d8f9d2afSIgor Opaniuk
968d8f9d2afSIgor Opaniuk /* We only need to continue when there is no digest in the descriptor.
969d8f9d2afSIgor Opaniuk * This is because the only processing here is to find the digest and
970d8f9d2afSIgor Opaniuk * make it available on the kernel command line.
971d8f9d2afSIgor Opaniuk */
972d8f9d2afSIgor Opaniuk if (hashtree_desc.root_digest_len == 0) {
973d8f9d2afSIgor Opaniuk char part_name[AVB_PART_NAME_MAX_SIZE];
974d8f9d2afSIgor Opaniuk size_t digest_len = 0;
975d8f9d2afSIgor Opaniuk uint8_t digest_buf[AVB_SHA512_DIGEST_SIZE];
976d8f9d2afSIgor Opaniuk const uint8_t* desc_partition_name =
977d8f9d2afSIgor Opaniuk ((const uint8_t*)descriptors[n]) + sizeof(AvbHashtreeDescriptor);
978d8f9d2afSIgor Opaniuk
979d8f9d2afSIgor Opaniuk if (!avb_validate_utf8(desc_partition_name,
980d8f9d2afSIgor Opaniuk hashtree_desc.partition_name_len)) {
981d8f9d2afSIgor Opaniuk avb_error("Partition name is not valid UTF-8.\n");
982d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
983d8f9d2afSIgor Opaniuk goto out;
984d8f9d2afSIgor Opaniuk }
985d8f9d2afSIgor Opaniuk
986d8f9d2afSIgor Opaniuk /* No ab_suffix for partitions without a digest in the descriptor
987d8f9d2afSIgor Opaniuk * because these partitions hold data unique to this device and are
988d8f9d2afSIgor Opaniuk * not updated using an A/B scheme.
989d8f9d2afSIgor Opaniuk */
990d8f9d2afSIgor Opaniuk if ((hashtree_desc.flags &
991d8f9d2afSIgor Opaniuk AVB_HASHTREE_DESCRIPTOR_FLAGS_DO_NOT_USE_AB) == 0 &&
992d8f9d2afSIgor Opaniuk avb_strlen(ab_suffix) != 0) {
993d8f9d2afSIgor Opaniuk avb_error("Cannot use A/B with a persistent root digest.\n");
994d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
995d8f9d2afSIgor Opaniuk goto out;
996d8f9d2afSIgor Opaniuk }
997d8f9d2afSIgor Opaniuk if (hashtree_desc.partition_name_len >= AVB_PART_NAME_MAX_SIZE) {
998d8f9d2afSIgor Opaniuk avb_error("Partition name does not fit.\n");
999d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1000d8f9d2afSIgor Opaniuk goto out;
1001d8f9d2afSIgor Opaniuk }
1002d8f9d2afSIgor Opaniuk avb_memcpy(
1003d8f9d2afSIgor Opaniuk part_name, desc_partition_name, hashtree_desc.partition_name_len);
1004d8f9d2afSIgor Opaniuk part_name[hashtree_desc.partition_name_len] = '\0';
1005d8f9d2afSIgor Opaniuk
1006d8f9d2afSIgor Opaniuk /* Determine the expected digest size from the hash algorithm. */
1007d8f9d2afSIgor Opaniuk if (avb_strcmp((const char*)hashtree_desc.hash_algorithm, "sha1") ==
1008d8f9d2afSIgor Opaniuk 0) {
1009d8f9d2afSIgor Opaniuk digest_len = AVB_SHA1_DIGEST_SIZE;
1010d8f9d2afSIgor Opaniuk } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
1011d8f9d2afSIgor Opaniuk "sha256") == 0) {
1012d8f9d2afSIgor Opaniuk digest_len = AVB_SHA256_DIGEST_SIZE;
1013d8f9d2afSIgor Opaniuk } else if (avb_strcmp((const char*)hashtree_desc.hash_algorithm,
1014d8f9d2afSIgor Opaniuk "sha512") == 0) {
1015d8f9d2afSIgor Opaniuk digest_len = AVB_SHA512_DIGEST_SIZE;
1016d8f9d2afSIgor Opaniuk } else {
1017d8f9d2afSIgor Opaniuk avb_errorv(part_name, ": Unsupported hash algorithm.\n", NULL);
1018d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1019d8f9d2afSIgor Opaniuk goto out;
1020d8f9d2afSIgor Opaniuk }
1021d8f9d2afSIgor Opaniuk
1022d8f9d2afSIgor Opaniuk ret = read_persistent_digest(ops, part_name, digest_len, digest_buf);
1023d8f9d2afSIgor Opaniuk if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
1024d8f9d2afSIgor Opaniuk goto out;
1025d8f9d2afSIgor Opaniuk }
1026d8f9d2afSIgor Opaniuk
1027d8f9d2afSIgor Opaniuk if (out_additional_cmdline_subst) {
1028d8f9d2afSIgor Opaniuk ret =
1029d8f9d2afSIgor Opaniuk avb_add_root_digest_substitution(part_name,
1030d8f9d2afSIgor Opaniuk digest_buf,
1031d8f9d2afSIgor Opaniuk digest_len,
1032d8f9d2afSIgor Opaniuk out_additional_cmdline_subst);
1033d8f9d2afSIgor Opaniuk if (ret != AVB_SLOT_VERIFY_RESULT_OK) {
1034d8f9d2afSIgor Opaniuk goto out;
1035d8f9d2afSIgor Opaniuk }
1036d8f9d2afSIgor Opaniuk }
1037d8f9d2afSIgor Opaniuk }
1038d8f9d2afSIgor Opaniuk } break;
1039d8f9d2afSIgor Opaniuk
1040d8f9d2afSIgor Opaniuk case AVB_DESCRIPTOR_TAG_PROPERTY:
1041d8f9d2afSIgor Opaniuk /* Do nothing. */
1042d8f9d2afSIgor Opaniuk break;
1043d8f9d2afSIgor Opaniuk }
1044d8f9d2afSIgor Opaniuk }
1045d8f9d2afSIgor Opaniuk
1046d8f9d2afSIgor Opaniuk if (rollback_index_location >= AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS) {
1047d8f9d2afSIgor Opaniuk avb_errorv(
1048d8f9d2afSIgor Opaniuk full_partition_name, ": Invalid rollback_index_location.\n", NULL);
1049d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
1050d8f9d2afSIgor Opaniuk goto out;
1051d8f9d2afSIgor Opaniuk }
1052d8f9d2afSIgor Opaniuk
1053d8f9d2afSIgor Opaniuk slot_data->rollback_indexes[rollback_index_location] =
1054d8f9d2afSIgor Opaniuk vbmeta_header.rollback_index;
1055d8f9d2afSIgor Opaniuk
1056d8f9d2afSIgor Opaniuk if (out_algorithm_type != NULL) {
1057d8f9d2afSIgor Opaniuk *out_algorithm_type = (AvbAlgorithmType)vbmeta_header.algorithm_type;
1058d8f9d2afSIgor Opaniuk }
1059d8f9d2afSIgor Opaniuk
1060d8f9d2afSIgor Opaniuk out:
1061d8f9d2afSIgor Opaniuk /* If |vbmeta_image_data| isn't NULL it means that it adopted
1062d8f9d2afSIgor Opaniuk * |vbmeta_buf| so in that case don't free it here.
1063d8f9d2afSIgor Opaniuk */
1064d8f9d2afSIgor Opaniuk if (vbmeta_image_data == NULL) {
1065d8f9d2afSIgor Opaniuk if (vbmeta_buf != NULL) {
1066d8f9d2afSIgor Opaniuk avb_free(vbmeta_buf);
1067d8f9d2afSIgor Opaniuk }
1068d8f9d2afSIgor Opaniuk }
1069d8f9d2afSIgor Opaniuk if (descriptors != NULL) {
1070d8f9d2afSIgor Opaniuk avb_free(descriptors);
1071d8f9d2afSIgor Opaniuk }
1072d8f9d2afSIgor Opaniuk return ret;
1073d8f9d2afSIgor Opaniuk }
1074d8f9d2afSIgor Opaniuk
avb_slot_verify(AvbOps * ops,const char * const * requested_partitions,const char * ab_suffix,AvbSlotVerifyFlags flags,AvbHashtreeErrorMode hashtree_error_mode,AvbSlotVerifyData ** out_data)1075d8f9d2afSIgor Opaniuk AvbSlotVerifyResult avb_slot_verify(AvbOps* ops,
1076d8f9d2afSIgor Opaniuk const char* const* requested_partitions,
1077d8f9d2afSIgor Opaniuk const char* ab_suffix,
1078d8f9d2afSIgor Opaniuk AvbSlotVerifyFlags flags,
1079d8f9d2afSIgor Opaniuk AvbHashtreeErrorMode hashtree_error_mode,
1080d8f9d2afSIgor Opaniuk AvbSlotVerifyData** out_data) {
1081d8f9d2afSIgor Opaniuk AvbSlotVerifyResult ret;
1082d8f9d2afSIgor Opaniuk AvbSlotVerifyData* slot_data = NULL;
1083d8f9d2afSIgor Opaniuk AvbAlgorithmType algorithm_type = AVB_ALGORITHM_TYPE_NONE;
1084d8f9d2afSIgor Opaniuk bool using_boot_for_vbmeta = false;
1085d8f9d2afSIgor Opaniuk AvbVBMetaImageHeader toplevel_vbmeta;
1086d8f9d2afSIgor Opaniuk bool allow_verification_error =
1087d8f9d2afSIgor Opaniuk (flags & AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR);
1088d8f9d2afSIgor Opaniuk AvbCmdlineSubstList* additional_cmdline_subst = NULL;
1089d8f9d2afSIgor Opaniuk
1090d8f9d2afSIgor Opaniuk /* Fail early if we're missing the AvbOps needed for slot verification.
1091d8f9d2afSIgor Opaniuk *
1092d8f9d2afSIgor Opaniuk * For now, handle get_size_of_partition() not being implemented. In
1093d8f9d2afSIgor Opaniuk * a later release we may change that.
1094d8f9d2afSIgor Opaniuk */
1095d8f9d2afSIgor Opaniuk avb_assert(ops->read_is_device_unlocked != NULL);
1096d8f9d2afSIgor Opaniuk avb_assert(ops->read_from_partition != NULL);
1097d8f9d2afSIgor Opaniuk avb_assert(ops->validate_vbmeta_public_key != NULL);
1098d8f9d2afSIgor Opaniuk avb_assert(ops->read_rollback_index != NULL);
1099d8f9d2afSIgor Opaniuk avb_assert(ops->get_unique_guid_for_partition != NULL);
1100d8f9d2afSIgor Opaniuk
1101d8f9d2afSIgor Opaniuk if (out_data != NULL) {
1102d8f9d2afSIgor Opaniuk *out_data = NULL;
1103d8f9d2afSIgor Opaniuk }
1104d8f9d2afSIgor Opaniuk
1105d8f9d2afSIgor Opaniuk /* Allowing dm-verity errors defeats the purpose of verified boot so
1106d8f9d2afSIgor Opaniuk * only allow this if set up to allow verification errors
1107d8f9d2afSIgor Opaniuk * (e.g. typically only UNLOCKED mode).
1108d8f9d2afSIgor Opaniuk */
1109d8f9d2afSIgor Opaniuk if (hashtree_error_mode == AVB_HASHTREE_ERROR_MODE_LOGGING &&
1110d8f9d2afSIgor Opaniuk !allow_verification_error) {
1111d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT;
1112d8f9d2afSIgor Opaniuk goto fail;
1113d8f9d2afSIgor Opaniuk }
1114d8f9d2afSIgor Opaniuk
1115d8f9d2afSIgor Opaniuk slot_data = avb_calloc(sizeof(AvbSlotVerifyData));
1116d8f9d2afSIgor Opaniuk if (slot_data == NULL) {
1117d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1118d8f9d2afSIgor Opaniuk goto fail;
1119d8f9d2afSIgor Opaniuk }
1120d8f9d2afSIgor Opaniuk slot_data->vbmeta_images =
1121d8f9d2afSIgor Opaniuk avb_calloc(sizeof(AvbVBMetaData) * MAX_NUMBER_OF_VBMETA_IMAGES);
1122d8f9d2afSIgor Opaniuk if (slot_data->vbmeta_images == NULL) {
1123d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1124d8f9d2afSIgor Opaniuk goto fail;
1125d8f9d2afSIgor Opaniuk }
1126d8f9d2afSIgor Opaniuk slot_data->loaded_partitions =
1127d8f9d2afSIgor Opaniuk avb_calloc(sizeof(AvbPartitionData) * MAX_NUMBER_OF_LOADED_PARTITIONS);
1128d8f9d2afSIgor Opaniuk if (slot_data->loaded_partitions == NULL) {
1129d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1130d8f9d2afSIgor Opaniuk goto fail;
1131d8f9d2afSIgor Opaniuk }
1132d8f9d2afSIgor Opaniuk
1133d8f9d2afSIgor Opaniuk additional_cmdline_subst = avb_new_cmdline_subst_list();
1134d8f9d2afSIgor Opaniuk if (additional_cmdline_subst == NULL) {
1135d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1136d8f9d2afSIgor Opaniuk goto fail;
1137d8f9d2afSIgor Opaniuk }
1138d8f9d2afSIgor Opaniuk
1139d8f9d2afSIgor Opaniuk ret = load_and_verify_vbmeta(ops,
1140d8f9d2afSIgor Opaniuk requested_partitions,
1141d8f9d2afSIgor Opaniuk ab_suffix,
1142d8f9d2afSIgor Opaniuk allow_verification_error,
1143d8f9d2afSIgor Opaniuk 0 /* toplevel_vbmeta_flags */,
1144d8f9d2afSIgor Opaniuk 0 /* rollback_index_location */,
1145d8f9d2afSIgor Opaniuk "vbmeta",
1146d8f9d2afSIgor Opaniuk avb_strlen("vbmeta"),
1147d8f9d2afSIgor Opaniuk NULL /* expected_public_key */,
1148d8f9d2afSIgor Opaniuk 0 /* expected_public_key_length */,
1149d8f9d2afSIgor Opaniuk slot_data,
1150d8f9d2afSIgor Opaniuk &algorithm_type,
1151d8f9d2afSIgor Opaniuk additional_cmdline_subst);
1152d8f9d2afSIgor Opaniuk if (!allow_verification_error && ret != AVB_SLOT_VERIFY_RESULT_OK) {
1153d8f9d2afSIgor Opaniuk goto fail;
1154d8f9d2afSIgor Opaniuk }
1155d8f9d2afSIgor Opaniuk
1156d8f9d2afSIgor Opaniuk /* If things check out, mangle the kernel command-line as needed. */
1157d8f9d2afSIgor Opaniuk if (result_should_continue(ret)) {
1158d8f9d2afSIgor Opaniuk if (avb_strcmp(slot_data->vbmeta_images[0].partition_name, "vbmeta") != 0) {
1159d8f9d2afSIgor Opaniuk avb_assert(
1160d8f9d2afSIgor Opaniuk avb_strcmp(slot_data->vbmeta_images[0].partition_name, "boot") == 0);
1161d8f9d2afSIgor Opaniuk using_boot_for_vbmeta = true;
1162d8f9d2afSIgor Opaniuk }
1163d8f9d2afSIgor Opaniuk
1164d8f9d2afSIgor Opaniuk /* Byteswap top-level vbmeta header since we'll need it below. */
1165d8f9d2afSIgor Opaniuk avb_vbmeta_image_header_to_host_byte_order(
1166d8f9d2afSIgor Opaniuk (const AvbVBMetaImageHeader*)slot_data->vbmeta_images[0].vbmeta_data,
1167d8f9d2afSIgor Opaniuk &toplevel_vbmeta);
1168d8f9d2afSIgor Opaniuk
1169d8f9d2afSIgor Opaniuk /* Fill in |ab_suffix| field. */
1170d8f9d2afSIgor Opaniuk slot_data->ab_suffix = avb_strdup(ab_suffix);
1171d8f9d2afSIgor Opaniuk if (slot_data->ab_suffix == NULL) {
1172d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1173d8f9d2afSIgor Opaniuk goto fail;
1174d8f9d2afSIgor Opaniuk }
1175d8f9d2afSIgor Opaniuk
1176d8f9d2afSIgor Opaniuk /* If verification is disabled, we are done ... we specifically
1177d8f9d2afSIgor Opaniuk * don't want to add any androidboot.* options since verification
1178d8f9d2afSIgor Opaniuk * is disabled.
1179d8f9d2afSIgor Opaniuk */
1180d8f9d2afSIgor Opaniuk if (toplevel_vbmeta.flags & AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED) {
1181d8f9d2afSIgor Opaniuk /* Since verification is disabled we didn't process any
1182d8f9d2afSIgor Opaniuk * descriptors and thus there's no cmdline... so set root= such
1183d8f9d2afSIgor Opaniuk * that the system partition is mounted.
1184d8f9d2afSIgor Opaniuk */
1185d8f9d2afSIgor Opaniuk avb_assert(slot_data->cmdline == NULL);
1186d8f9d2afSIgor Opaniuk slot_data->cmdline =
1187d8f9d2afSIgor Opaniuk avb_strdup("root=PARTUUID=$(ANDROID_SYSTEM_PARTUUID)");
1188d8f9d2afSIgor Opaniuk if (slot_data->cmdline == NULL) {
1189d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1190d8f9d2afSIgor Opaniuk goto fail;
1191d8f9d2afSIgor Opaniuk }
1192d8f9d2afSIgor Opaniuk } else {
1193d8f9d2afSIgor Opaniuk /* Add options - any failure in avb_append_options() is either an
1194d8f9d2afSIgor Opaniuk * I/O or OOM error.
1195d8f9d2afSIgor Opaniuk */
1196d8f9d2afSIgor Opaniuk AvbSlotVerifyResult sub_ret = avb_append_options(ops,
1197d8f9d2afSIgor Opaniuk slot_data,
1198d8f9d2afSIgor Opaniuk &toplevel_vbmeta,
1199d8f9d2afSIgor Opaniuk algorithm_type,
1200d8f9d2afSIgor Opaniuk hashtree_error_mode);
1201d8f9d2afSIgor Opaniuk if (sub_ret != AVB_SLOT_VERIFY_RESULT_OK) {
1202d8f9d2afSIgor Opaniuk ret = sub_ret;
1203d8f9d2afSIgor Opaniuk goto fail;
1204d8f9d2afSIgor Opaniuk }
1205d8f9d2afSIgor Opaniuk }
1206d8f9d2afSIgor Opaniuk
1207d8f9d2afSIgor Opaniuk /* Substitute $(ANDROID_SYSTEM_PARTUUID) and friends. */
1208d8f9d2afSIgor Opaniuk if (slot_data->cmdline != NULL) {
1209d8f9d2afSIgor Opaniuk char* new_cmdline;
1210d8f9d2afSIgor Opaniuk new_cmdline = avb_sub_cmdline(ops,
1211d8f9d2afSIgor Opaniuk slot_data->cmdline,
1212d8f9d2afSIgor Opaniuk ab_suffix,
1213d8f9d2afSIgor Opaniuk using_boot_for_vbmeta,
1214d8f9d2afSIgor Opaniuk additional_cmdline_subst);
1215d8f9d2afSIgor Opaniuk if (new_cmdline != slot_data->cmdline) {
1216d8f9d2afSIgor Opaniuk if (new_cmdline == NULL) {
1217d8f9d2afSIgor Opaniuk ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
1218d8f9d2afSIgor Opaniuk goto fail;
1219d8f9d2afSIgor Opaniuk }
1220d8f9d2afSIgor Opaniuk avb_free(slot_data->cmdline);
1221d8f9d2afSIgor Opaniuk slot_data->cmdline = new_cmdline;
1222d8f9d2afSIgor Opaniuk }
1223d8f9d2afSIgor Opaniuk }
1224d8f9d2afSIgor Opaniuk
1225d8f9d2afSIgor Opaniuk if (out_data != NULL) {
1226d8f9d2afSIgor Opaniuk *out_data = slot_data;
1227d8f9d2afSIgor Opaniuk } else {
1228d8f9d2afSIgor Opaniuk avb_slot_verify_data_free(slot_data);
1229d8f9d2afSIgor Opaniuk }
1230d8f9d2afSIgor Opaniuk }
1231d8f9d2afSIgor Opaniuk
1232d8f9d2afSIgor Opaniuk avb_free_cmdline_subst_list(additional_cmdline_subst);
1233d8f9d2afSIgor Opaniuk additional_cmdline_subst = NULL;
1234d8f9d2afSIgor Opaniuk
1235d8f9d2afSIgor Opaniuk if (!allow_verification_error) {
1236d8f9d2afSIgor Opaniuk avb_assert(ret == AVB_SLOT_VERIFY_RESULT_OK);
1237d8f9d2afSIgor Opaniuk }
1238d8f9d2afSIgor Opaniuk
1239d8f9d2afSIgor Opaniuk return ret;
1240d8f9d2afSIgor Opaniuk
1241d8f9d2afSIgor Opaniuk fail:
1242d8f9d2afSIgor Opaniuk if (slot_data != NULL) {
1243d8f9d2afSIgor Opaniuk avb_slot_verify_data_free(slot_data);
1244d8f9d2afSIgor Opaniuk }
1245d8f9d2afSIgor Opaniuk if (additional_cmdline_subst != NULL) {
1246d8f9d2afSIgor Opaniuk avb_free_cmdline_subst_list(additional_cmdline_subst);
1247d8f9d2afSIgor Opaniuk }
1248d8f9d2afSIgor Opaniuk return ret;
1249d8f9d2afSIgor Opaniuk }
1250d8f9d2afSIgor Opaniuk
avb_slot_verify_data_free(AvbSlotVerifyData * data)1251d8f9d2afSIgor Opaniuk void avb_slot_verify_data_free(AvbSlotVerifyData* data) {
1252d8f9d2afSIgor Opaniuk if (data->ab_suffix != NULL) {
1253d8f9d2afSIgor Opaniuk avb_free(data->ab_suffix);
1254d8f9d2afSIgor Opaniuk }
1255d8f9d2afSIgor Opaniuk if (data->cmdline != NULL) {
1256d8f9d2afSIgor Opaniuk avb_free(data->cmdline);
1257d8f9d2afSIgor Opaniuk }
1258d8f9d2afSIgor Opaniuk if (data->vbmeta_images != NULL) {
1259d8f9d2afSIgor Opaniuk size_t n;
1260d8f9d2afSIgor Opaniuk for (n = 0; n < data->num_vbmeta_images; n++) {
1261d8f9d2afSIgor Opaniuk AvbVBMetaData* vbmeta_image = &data->vbmeta_images[n];
1262d8f9d2afSIgor Opaniuk if (vbmeta_image->partition_name != NULL) {
1263d8f9d2afSIgor Opaniuk avb_free(vbmeta_image->partition_name);
1264d8f9d2afSIgor Opaniuk }
1265d8f9d2afSIgor Opaniuk if (vbmeta_image->vbmeta_data != NULL) {
1266d8f9d2afSIgor Opaniuk avb_free(vbmeta_image->vbmeta_data);
1267d8f9d2afSIgor Opaniuk }
1268d8f9d2afSIgor Opaniuk }
1269d8f9d2afSIgor Opaniuk avb_free(data->vbmeta_images);
1270d8f9d2afSIgor Opaniuk }
1271d8f9d2afSIgor Opaniuk if (data->loaded_partitions != NULL) {
1272d8f9d2afSIgor Opaniuk size_t n;
1273d8f9d2afSIgor Opaniuk for (n = 0; n < data->num_loaded_partitions; n++) {
1274d8f9d2afSIgor Opaniuk AvbPartitionData* loaded_partition = &data->loaded_partitions[n];
1275d8f9d2afSIgor Opaniuk if (loaded_partition->partition_name != NULL) {
1276d8f9d2afSIgor Opaniuk avb_free(loaded_partition->partition_name);
1277d8f9d2afSIgor Opaniuk }
1278d8f9d2afSIgor Opaniuk if (loaded_partition->data != NULL && !loaded_partition->preloaded) {
1279d8f9d2afSIgor Opaniuk avb_free(loaded_partition->data);
1280d8f9d2afSIgor Opaniuk }
1281d8f9d2afSIgor Opaniuk }
1282d8f9d2afSIgor Opaniuk avb_free(data->loaded_partitions);
1283d8f9d2afSIgor Opaniuk }
1284d8f9d2afSIgor Opaniuk avb_free(data);
1285d8f9d2afSIgor Opaniuk }
1286d8f9d2afSIgor Opaniuk
avb_slot_verify_result_to_string(AvbSlotVerifyResult result)1287d8f9d2afSIgor Opaniuk const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result) {
1288d8f9d2afSIgor Opaniuk const char* ret = NULL;
1289d8f9d2afSIgor Opaniuk
1290d8f9d2afSIgor Opaniuk switch (result) {
1291d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_OK:
1292d8f9d2afSIgor Opaniuk ret = "OK";
1293d8f9d2afSIgor Opaniuk break;
1294d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
1295d8f9d2afSIgor Opaniuk ret = "ERROR_OOM";
1296d8f9d2afSIgor Opaniuk break;
1297d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
1298d8f9d2afSIgor Opaniuk ret = "ERROR_IO";
1299d8f9d2afSIgor Opaniuk break;
1300d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
1301d8f9d2afSIgor Opaniuk ret = "ERROR_VERIFICATION";
1302d8f9d2afSIgor Opaniuk break;
1303d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
1304d8f9d2afSIgor Opaniuk ret = "ERROR_ROLLBACK_INDEX";
1305d8f9d2afSIgor Opaniuk break;
1306d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
1307d8f9d2afSIgor Opaniuk ret = "ERROR_PUBLIC_KEY_REJECTED";
1308d8f9d2afSIgor Opaniuk break;
1309d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
1310d8f9d2afSIgor Opaniuk ret = "ERROR_INVALID_METADATA";
1311d8f9d2afSIgor Opaniuk break;
1312d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
1313d8f9d2afSIgor Opaniuk ret = "ERROR_UNSUPPORTED_VERSION";
1314d8f9d2afSIgor Opaniuk break;
1315d8f9d2afSIgor Opaniuk case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
1316d8f9d2afSIgor Opaniuk ret = "ERROR_INVALID_ARGUMENT";
1317d8f9d2afSIgor Opaniuk break;
1318d8f9d2afSIgor Opaniuk /* Do not add a 'default:' case here because of -Wswitch. */
1319d8f9d2afSIgor Opaniuk }
1320d8f9d2afSIgor Opaniuk
1321d8f9d2afSIgor Opaniuk if (ret == NULL) {
1322d8f9d2afSIgor Opaniuk avb_error("Unknown AvbSlotVerifyResult value.\n");
1323d8f9d2afSIgor Opaniuk ret = "(unknown)";
1324d8f9d2afSIgor Opaniuk }
1325d8f9d2afSIgor Opaniuk
1326d8f9d2afSIgor Opaniuk return ret;
1327d8f9d2afSIgor Opaniuk }
1328d8f9d2afSIgor Opaniuk
avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData * data,AvbDigestType digest_type,uint8_t * out_digest)1329d8f9d2afSIgor Opaniuk void avb_slot_verify_data_calculate_vbmeta_digest(AvbSlotVerifyData* data,
1330d8f9d2afSIgor Opaniuk AvbDigestType digest_type,
1331d8f9d2afSIgor Opaniuk uint8_t* out_digest) {
1332d8f9d2afSIgor Opaniuk bool ret = false;
1333d8f9d2afSIgor Opaniuk size_t n;
1334d8f9d2afSIgor Opaniuk
1335d8f9d2afSIgor Opaniuk switch (digest_type) {
1336d8f9d2afSIgor Opaniuk case AVB_DIGEST_TYPE_SHA256: {
1337d8f9d2afSIgor Opaniuk AvbSHA256Ctx ctx;
1338d8f9d2afSIgor Opaniuk avb_sha256_init(&ctx);
1339d8f9d2afSIgor Opaniuk for (n = 0; n < data->num_vbmeta_images; n++) {
1340d8f9d2afSIgor Opaniuk avb_sha256_update(&ctx,
1341d8f9d2afSIgor Opaniuk data->vbmeta_images[n].vbmeta_data,
1342d8f9d2afSIgor Opaniuk data->vbmeta_images[n].vbmeta_size);
1343d8f9d2afSIgor Opaniuk }
1344d8f9d2afSIgor Opaniuk avb_memcpy(out_digest, avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE);
1345d8f9d2afSIgor Opaniuk ret = true;
1346d8f9d2afSIgor Opaniuk } break;
1347d8f9d2afSIgor Opaniuk
1348d8f9d2afSIgor Opaniuk case AVB_DIGEST_TYPE_SHA512: {
1349d8f9d2afSIgor Opaniuk AvbSHA512Ctx ctx;
1350d8f9d2afSIgor Opaniuk avb_sha512_init(&ctx);
1351d8f9d2afSIgor Opaniuk for (n = 0; n < data->num_vbmeta_images; n++) {
1352d8f9d2afSIgor Opaniuk avb_sha512_update(&ctx,
1353d8f9d2afSIgor Opaniuk data->vbmeta_images[n].vbmeta_data,
1354d8f9d2afSIgor Opaniuk data->vbmeta_images[n].vbmeta_size);
1355d8f9d2afSIgor Opaniuk }
1356d8f9d2afSIgor Opaniuk avb_memcpy(out_digest, avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE);
1357d8f9d2afSIgor Opaniuk ret = true;
1358d8f9d2afSIgor Opaniuk } break;
1359d8f9d2afSIgor Opaniuk
1360d8f9d2afSIgor Opaniuk /* Do not add a 'default:' case here because of -Wswitch. */
1361d8f9d2afSIgor Opaniuk }
1362d8f9d2afSIgor Opaniuk
1363d8f9d2afSIgor Opaniuk if (!ret) {
1364d8f9d2afSIgor Opaniuk avb_fatal("Unknown digest type");
1365d8f9d2afSIgor Opaniuk }
1366d8f9d2afSIgor Opaniuk }
1367