David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "avb_property_descriptor.h" |
| 18 | #include "avb_util.h" |
| 19 | |
| 20 | bool avb_property_descriptor_validate_and_byteswap( |
| 21 | const AvbPropertyDescriptor* src, AvbPropertyDescriptor* dest) { |
| 22 | uint64_t expected_size; |
| 23 | |
| 24 | avb_memcpy(dest, src, sizeof(AvbPropertyDescriptor)); |
| 25 | |
| 26 | if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src, |
| 27 | (AvbDescriptor*)dest)) |
| 28 | return false; |
| 29 | |
| 30 | if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_PROPERTY) { |
| 31 | avb_error("Invalid tag for property descriptor.\n"); |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | dest->key_num_bytes = avb_be64toh(dest->key_num_bytes); |
| 36 | dest->value_num_bytes = avb_be64toh(dest->value_num_bytes); |
| 37 | |
| 38 | /* Check that key and value are fully contained. */ |
| 39 | expected_size = sizeof(AvbPropertyDescriptor) - sizeof(AvbDescriptor) + 2; |
| 40 | if (!avb_safe_add_to(&expected_size, dest->key_num_bytes) || |
| 41 | !avb_safe_add_to(&expected_size, dest->value_num_bytes)) { |
| 42 | avb_error("Overflow while adding up sizes.\n"); |
| 43 | return false; |
| 44 | } |
| 45 | if (expected_size > dest->parent_descriptor.num_bytes_following) { |
| 46 | avb_error("Descriptor payload size overflow.\n"); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | typedef struct { |
| 54 | const char* key; |
| 55 | size_t key_size; |
| 56 | const char* ret_value; |
| 57 | size_t ret_value_size; |
| 58 | } PropertyIteratorData; |
| 59 | |
| 60 | static bool property_lookup_desc_foreach(const AvbDescriptor* header, |
| 61 | void* user_data) { |
| 62 | PropertyIteratorData* data = (PropertyIteratorData*)user_data; |
| 63 | AvbPropertyDescriptor prop_desc; |
| 64 | const uint8_t* p; |
| 65 | bool ret = true; |
| 66 | |
| 67 | if (header->tag != AVB_DESCRIPTOR_TAG_PROPERTY) goto out; |
| 68 | |
| 69 | if (!avb_property_descriptor_validate_and_byteswap( |
| 70 | (const AvbPropertyDescriptor*)header, &prop_desc)) |
| 71 | goto out; |
| 72 | |
| 73 | p = (const uint8_t*)header; |
| 74 | if (p[sizeof(AvbPropertyDescriptor) + prop_desc.key_num_bytes] != 0) { |
| 75 | avb_error("No terminating NUL byte in key.\n"); |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | if (data->key_size == prop_desc.key_num_bytes) { |
| 80 | if (avb_memcmp(p + sizeof(AvbPropertyDescriptor), data->key, |
| 81 | data->key_size) == 0) { |
| 82 | data->ret_value = (const char*)(p + sizeof(AvbPropertyDescriptor) + |
| 83 | prop_desc.key_num_bytes + 1); |
| 84 | data->ret_value_size = prop_desc.value_num_bytes; |
| 85 | /* Stop iterating. */ |
| 86 | ret = false; |
| 87 | goto out; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | out: |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | const char* avb_property_lookup(const uint8_t* image_data, size_t image_size, |
| 96 | const char* key, size_t key_size, |
| 97 | size_t* out_value_size) { |
| 98 | PropertyIteratorData data; |
| 99 | |
| 100 | if (key_size == 0) key_size = avb_strlen(key); |
| 101 | |
| 102 | data.key = key; |
| 103 | data.key_size = key_size; |
| 104 | |
| 105 | if (avb_descriptor_foreach(image_data, image_size, |
| 106 | property_lookup_desc_foreach, &data) == 0) { |
| 107 | if (out_value_size != NULL) *out_value_size = data.ret_value_size; |
| 108 | return data.ret_value; |
| 109 | } |
| 110 | |
| 111 | if (out_value_size != NULL) *out_value_size = 0; |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | bool avb_property_lookup_uint64(const uint8_t* image_data, size_t image_size, |
| 116 | const char* key, size_t key_size, |
| 117 | uint64_t* out_value) { |
| 118 | const char* value; |
| 119 | bool ret = false; |
| 120 | uint64_t parsed_val; |
| 121 | int base; |
| 122 | int n; |
| 123 | |
| 124 | value = avb_property_lookup(image_data, image_size, key, key_size, NULL); |
| 125 | if (value == NULL) goto out; |
| 126 | |
| 127 | base = 10; |
| 128 | if (avb_memcmp(value, "0x", 2) == 0) { |
| 129 | base = 16; |
| 130 | value += 2; |
| 131 | } |
| 132 | |
| 133 | parsed_val = 0; |
| 134 | for (n = 0; value[n] != '\0'; n++) { |
| 135 | int c = value[n]; |
| 136 | int digit; |
| 137 | |
| 138 | parsed_val *= base; |
| 139 | |
| 140 | if (c >= '0' && c <= '9') { |
| 141 | digit = c - '0'; |
| 142 | } else if (base == 16 && c >= 'a' && c <= 'f') { |
| 143 | digit = c - 'a' + 10; |
| 144 | } else if (base == 16 && c >= 'A' && c <= 'F') { |
| 145 | digit = c - 'A' + 10; |
| 146 | } else { |
| 147 | avb_error("Invalid digit.\n"); |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | parsed_val += digit; |
| 152 | } |
| 153 | |
| 154 | ret = true; |
| 155 | if (out_value != NULL) *out_value = parsed_val; |
| 156 | |
| 157 | out: |
| 158 | return ret; |
| 159 | } |