avb_property_descriptor.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright (C) 2016 The Android Open Source Project
  4. */
  5. #include "avb_property_descriptor.h"
  6. #include "avb_util.h"
  7. bool avb_property_descriptor_validate_and_byteswap(
  8. const AvbPropertyDescriptor* src, AvbPropertyDescriptor* dest) {
  9. uint64_t expected_size;
  10. avb_memcpy(dest, src, sizeof(AvbPropertyDescriptor));
  11. if (!avb_descriptor_validate_and_byteswap((const AvbDescriptor*)src,
  12. (AvbDescriptor*)dest))
  13. return false;
  14. if (dest->parent_descriptor.tag != AVB_DESCRIPTOR_TAG_PROPERTY) {
  15. avb_error("Invalid tag for property descriptor.\n");
  16. return false;
  17. }
  18. dest->key_num_bytes = avb_be64toh(dest->key_num_bytes);
  19. dest->value_num_bytes = avb_be64toh(dest->value_num_bytes);
  20. /* Check that key and value are fully contained. */
  21. expected_size = sizeof(AvbPropertyDescriptor) - sizeof(AvbDescriptor) + 2;
  22. if (!avb_safe_add_to(&expected_size, dest->key_num_bytes) ||
  23. !avb_safe_add_to(&expected_size, dest->value_num_bytes)) {
  24. avb_error("Overflow while adding up sizes.\n");
  25. return false;
  26. }
  27. if (expected_size > dest->parent_descriptor.num_bytes_following) {
  28. avb_error("Descriptor payload size overflow.\n");
  29. return false;
  30. }
  31. return true;
  32. }
  33. typedef struct {
  34. const char* key;
  35. size_t key_size;
  36. const char* ret_value;
  37. size_t ret_value_size;
  38. } PropertyIteratorData;
  39. static bool property_lookup_desc_foreach(const AvbDescriptor* header,
  40. void* user_data) {
  41. PropertyIteratorData* data = (PropertyIteratorData*)user_data;
  42. AvbPropertyDescriptor prop_desc;
  43. const uint8_t* p;
  44. bool ret = true;
  45. if (header->tag != AVB_DESCRIPTOR_TAG_PROPERTY) {
  46. goto out;
  47. }
  48. if (!avb_property_descriptor_validate_and_byteswap(
  49. (const AvbPropertyDescriptor*)header, &prop_desc)) {
  50. goto out;
  51. }
  52. p = (const uint8_t*)header;
  53. if (p[sizeof(AvbPropertyDescriptor) + prop_desc.key_num_bytes] != 0) {
  54. avb_error("No terminating NUL byte in key.\n");
  55. goto out;
  56. }
  57. if (data->key_size == prop_desc.key_num_bytes) {
  58. if (avb_memcmp(p + sizeof(AvbPropertyDescriptor),
  59. data->key,
  60. data->key_size) == 0) {
  61. data->ret_value = (const char*)(p + sizeof(AvbPropertyDescriptor) +
  62. prop_desc.key_num_bytes + 1);
  63. data->ret_value_size = prop_desc.value_num_bytes;
  64. /* Stop iterating. */
  65. ret = false;
  66. goto out;
  67. }
  68. }
  69. out:
  70. return ret;
  71. }
  72. const char* avb_property_lookup(const uint8_t* image_data,
  73. size_t image_size,
  74. const char* key,
  75. size_t key_size,
  76. size_t* out_value_size) {
  77. PropertyIteratorData data;
  78. if (key_size == 0) {
  79. key_size = avb_strlen(key);
  80. }
  81. data.key = key;
  82. data.key_size = key_size;
  83. if (avb_descriptor_foreach(
  84. image_data, image_size, property_lookup_desc_foreach, &data) == 0) {
  85. if (out_value_size != NULL) {
  86. *out_value_size = data.ret_value_size;
  87. }
  88. return data.ret_value;
  89. }
  90. if (out_value_size != NULL) {
  91. *out_value_size = 0;
  92. }
  93. return NULL;
  94. }
  95. bool avb_property_lookup_uint64(const uint8_t* image_data,
  96. size_t image_size,
  97. const char* key,
  98. size_t key_size,
  99. uint64_t* out_value) {
  100. const char* value;
  101. bool ret = false;
  102. uint64_t parsed_val;
  103. int base;
  104. int n;
  105. value = avb_property_lookup(image_data, image_size, key, key_size, NULL);
  106. if (value == NULL) {
  107. goto out;
  108. }
  109. base = 10;
  110. if (avb_memcmp(value, "0x", 2) == 0) {
  111. base = 16;
  112. value += 2;
  113. }
  114. parsed_val = 0;
  115. for (n = 0; value[n] != '\0'; n++) {
  116. int c = value[n];
  117. int digit;
  118. parsed_val *= base;
  119. if (c >= '0' && c <= '9') {
  120. digit = c - '0';
  121. } else if (base == 16 && c >= 'a' && c <= 'f') {
  122. digit = c - 'a' + 10;
  123. } else if (base == 16 && c >= 'A' && c <= 'F') {
  124. digit = c - 'A' + 10;
  125. } else {
  126. avb_error("Invalid digit.\n");
  127. goto out;
  128. }
  129. parsed_val += digit;
  130. }
  131. ret = true;
  132. if (out_value != NULL) {
  133. *out_value = parsed_val;
  134. }
  135. out:
  136. return ret;
  137. }