blob: 03d831579a02979317bb90f24cb5521d02667f92 [file] [log] [blame]
David Zeuthen21e95262016-07-27 17:58:40 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
David Zeuthenc612e2e2016-09-16 16:44:08 -04004 * 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 Zeuthen21e95262016-07-27 17:58:40 -040011 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040012 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
David Zeuthen21e95262016-07-27 17:58:40 -040014 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040015 * 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 Zeuthen21e95262016-07-27 17:58:40 -040023 */
24
25#include "avb_property_descriptor.h"
26#include "avb_util.h"
27
28bool avb_property_descriptor_validate_and_byteswap(
29 const AvbPropertyDescriptor* src, AvbPropertyDescriptor* dest) {
30 uint64_t expected_size;
31
32 avb_memcpy(dest, src, sizeof(AvbPropertyDescriptor));
33
34 if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src,
35 (AvbDescriptor*)dest))
36 return false;
37
38 if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_PROPERTY) {
39 avb_error("Invalid tag for property descriptor.\n");
40 return false;
41 }
42
43 dest->key_num_bytes = avb_be64toh(dest->key_num_bytes);
44 dest->value_num_bytes = avb_be64toh(dest->value_num_bytes);
45
46 /* Check that key and value are fully contained. */
47 expected_size = sizeof(AvbPropertyDescriptor) - sizeof(AvbDescriptor) + 2;
48 if (!avb_safe_add_to(&expected_size, dest->key_num_bytes) ||
49 !avb_safe_add_to(&expected_size, dest->value_num_bytes)) {
50 avb_error("Overflow while adding up sizes.\n");
51 return false;
52 }
53 if (expected_size > dest->parent_descriptor.num_bytes_following) {
54 avb_error("Descriptor payload size overflow.\n");
55 return false;
56 }
57
58 return true;
59}
60
61typedef struct {
62 const char* key;
63 size_t key_size;
64 const char* ret_value;
65 size_t ret_value_size;
66} PropertyIteratorData;
67
68static bool property_lookup_desc_foreach(const AvbDescriptor* header,
69 void* user_data) {
70 PropertyIteratorData* data = (PropertyIteratorData*)user_data;
71 AvbPropertyDescriptor prop_desc;
72 const uint8_t* p;
73 bool ret = true;
74
75 if (header->tag != AVB_DESCRIPTOR_TAG_PROPERTY) goto out;
76
77 if (!avb_property_descriptor_validate_and_byteswap(
78 (const AvbPropertyDescriptor*)header, &prop_desc))
79 goto out;
80
81 p = (const uint8_t*)header;
82 if (p[sizeof(AvbPropertyDescriptor) + prop_desc.key_num_bytes] != 0) {
83 avb_error("No terminating NUL byte in key.\n");
84 goto out;
85 }
86
87 if (data->key_size == prop_desc.key_num_bytes) {
88 if (avb_memcmp(p + sizeof(AvbPropertyDescriptor), data->key,
89 data->key_size) == 0) {
90 data->ret_value = (const char*)(p + sizeof(AvbPropertyDescriptor) +
91 prop_desc.key_num_bytes + 1);
92 data->ret_value_size = prop_desc.value_num_bytes;
93 /* Stop iterating. */
94 ret = false;
95 goto out;
96 }
97 }
98
99out:
100 return ret;
101}
102
103const char* avb_property_lookup(const uint8_t* image_data, size_t image_size,
104 const char* key, size_t key_size,
105 size_t* out_value_size) {
106 PropertyIteratorData data;
107
108 if (key_size == 0) key_size = avb_strlen(key);
109
110 data.key = key;
111 data.key_size = key_size;
112
113 if (avb_descriptor_foreach(image_data, image_size,
114 property_lookup_desc_foreach, &data) == 0) {
115 if (out_value_size != NULL) *out_value_size = data.ret_value_size;
116 return data.ret_value;
117 }
118
119 if (out_value_size != NULL) *out_value_size = 0;
120 return NULL;
121}
122
123bool avb_property_lookup_uint64(const uint8_t* image_data, size_t image_size,
124 const char* key, size_t key_size,
125 uint64_t* out_value) {
126 const char* value;
127 bool ret = false;
128 uint64_t parsed_val;
129 int base;
130 int n;
131
132 value = avb_property_lookup(image_data, image_size, key, key_size, NULL);
133 if (value == NULL) goto out;
134
135 base = 10;
136 if (avb_memcmp(value, "0x", 2) == 0) {
137 base = 16;
138 value += 2;
139 }
140
141 parsed_val = 0;
142 for (n = 0; value[n] != '\0'; n++) {
143 int c = value[n];
144 int digit;
145
146 parsed_val *= base;
147
148 if (c >= '0' && c <= '9') {
149 digit = c - '0';
150 } else if (base == 16 && c >= 'a' && c <= 'f') {
151 digit = c - 'a' + 10;
152 } else if (base == 16 && c >= 'A' && c <= 'F') {
153 digit = c - 'A' + 10;
154 } else {
155 avb_error("Invalid digit.\n");
156 goto out;
157 }
158
159 parsed_val += digit;
160 }
161
162 ret = true;
163 if (out_value != NULL) *out_value = parsed_val;
164
165out:
166 return ret;
167}