avb_vbmeta_image.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  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:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  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.
  23. */
  24. #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
  25. #error "Never include this file directly, include libavb.h instead."
  26. #endif
  27. #ifndef AVB_VBMETA_IMAGE_H_
  28. #define AVB_VBMETA_IMAGE_H_
  29. #include "avb_sysdeps.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include "avb_crypto.h"
  34. #include "avb_descriptor.h"
  35. /* Size of the vbmeta image header. */
  36. #define AVB_VBMETA_IMAGE_HEADER_SIZE 256
  37. /* Magic for the vbmeta image header. */
  38. #define AVB_MAGIC "AVB0"
  39. #define AVB_MAGIC_LEN 4
  40. /* Maximum size of the release string including the terminating NUL byte. */
  41. #define AVB_RELEASE_STRING_SIZE 48
  42. /* Flags for the vbmeta image.
  43. *
  44. * AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED: If this flag is set,
  45. * hashtree image verification will be disabled.
  46. *
  47. * AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED: If this flag is set,
  48. * verification will be disabled and descriptors will not be parsed.
  49. */
  50. typedef enum {
  51. AVB_VBMETA_IMAGE_FLAGS_HASHTREE_DISABLED = (1 << 0),
  52. AVB_VBMETA_IMAGE_FLAGS_VERIFICATION_DISABLED = (1 << 1)
  53. } AvbVBMetaImageFlags;
  54. /* Binary format for header of the vbmeta image.
  55. *
  56. * The vbmeta image consists of three blocks:
  57. *
  58. * +-----------------------------------------+
  59. * | Header data - fixed size |
  60. * +-----------------------------------------+
  61. * | Authentication data - variable size |
  62. * +-----------------------------------------+
  63. * | Auxiliary data - variable size |
  64. * +-----------------------------------------+
  65. *
  66. * The "Header data" block is described by this struct and is always
  67. * |AVB_VBMETA_IMAGE_HEADER_SIZE| bytes long.
  68. *
  69. * The "Authentication data" block is |authentication_data_block_size|
  70. * bytes long and contains the hash and signature used to authenticate
  71. * the vbmeta image. The type of the hash and signature is defined by
  72. * the |algorithm_type| field.
  73. *
  74. * The "Auxiliary data" is |auxiliary_data_block_size| bytes long and
  75. * contains the auxiliary data including the public key used to make
  76. * the signature and descriptors.
  77. *
  78. * The public key is at offset |public_key_offset| with size
  79. * |public_key_size| in this block. The size of the public key data is
  80. * defined by the |algorithm_type| field. The format of the public key
  81. * data is described in the |AvbRSAPublicKeyHeader| struct.
  82. *
  83. * The descriptors starts at |descriptors_offset| from the beginning
  84. * of the "Auxiliary Data" block and take up |descriptors_size|
  85. * bytes. Each descriptor is stored as a |AvbDescriptor| with tag and
  86. * number of bytes following. The number of descriptors can be
  87. * determined by walking this data until |descriptors_size| is
  88. * exhausted.
  89. *
  90. * The size of each of the "Authentication data" and "Auxiliary data"
  91. * blocks must be divisible by 64. This is to ensure proper alignment.
  92. *
  93. * Descriptors are free-form blocks stored in a part of the vbmeta
  94. * image subject to the same integrity checks as the rest of the
  95. * image. See the documentation for |AvbDescriptor| for well-known
  96. * descriptors. See avb_descriptor_foreach() for a convenience
  97. * function to iterate over descriptors.
  98. *
  99. * This struct is versioned, see the |required_libavb_version_major|
  100. * and |required_libavb_version_minor| fields. This represents the
  101. * minimum version of libavb required to verify the header and depends
  102. * on the features (e.g. algorithms, descriptors) used. Note that this
  103. * may be 1.0 even if generated by an avbtool from 1.4 but where no
  104. * features introduced after 1.0 has been used. See the "Versioning
  105. * and compatibility" section in the README.md file for more details.
  106. *
  107. * All fields are stored in network byte order when serialized. To
  108. * generate a copy with fields swapped to native byte order, use the
  109. * function avb_vbmeta_image_header_to_host_byte_order().
  110. *
  111. * Before reading and/or using any of this data, you MUST verify it
  112. * using avb_vbmeta_image_verify() and reject it unless it's signed by
  113. * a known good public key.
  114. */
  115. typedef struct AvbVBMetaImageHeader {
  116. /* 0: Four bytes equal to "AVB0" (AVB_MAGIC). */
  117. uint8_t magic[AVB_MAGIC_LEN];
  118. /* 4: The major version of libavb required for this header. */
  119. uint32_t required_libavb_version_major;
  120. /* 8: The minor version of libavb required for this header. */
  121. uint32_t required_libavb_version_minor;
  122. /* 12: The size of the signature block. */
  123. uint64_t authentication_data_block_size;
  124. /* 20: The size of the auxiliary data block. */
  125. uint64_t auxiliary_data_block_size;
  126. /* 28: The verification algorithm used, see |AvbAlgorithmType| enum. */
  127. uint32_t algorithm_type;
  128. /* 32: Offset into the "Authentication data" block of hash data. */
  129. uint64_t hash_offset;
  130. /* 40: Length of the hash data. */
  131. uint64_t hash_size;
  132. /* 48: Offset into the "Authentication data" block of signature data. */
  133. uint64_t signature_offset;
  134. /* 56: Length of the signature data. */
  135. uint64_t signature_size;
  136. /* 64: Offset into the "Auxiliary data" block of public key data. */
  137. uint64_t public_key_offset;
  138. /* 72: Length of the public key data. */
  139. uint64_t public_key_size;
  140. /* 80: Offset into the "Auxiliary data" block of public key metadata. */
  141. uint64_t public_key_metadata_offset;
  142. /* 88: Length of the public key metadata. Must be set to zero if there
  143. * is no public key metadata.
  144. */
  145. uint64_t public_key_metadata_size;
  146. /* 96: Offset into the "Auxiliary data" block of descriptor data. */
  147. uint64_t descriptors_offset;
  148. /* 104: Length of descriptor data. */
  149. uint64_t descriptors_size;
  150. /* 112: The rollback index which can be used to prevent rollback to
  151. * older versions.
  152. */
  153. uint64_t rollback_index;
  154. /* 120: Flags from the AvbVBMetaImageFlags enumeration. This must be
  155. * set to zero if the vbmeta image is not a top-level image.
  156. */
  157. uint32_t flags;
  158. /* 124: Reserved to ensure |release_string| start on a 16-byte
  159. * boundary. Must be set to zeroes.
  160. */
  161. uint8_t reserved0[4];
  162. /* 128: The release string from avbtool, e.g. "avbtool 1.0.0" or
  163. * "avbtool 1.0.0 xyz_board Git-234abde89". Is guaranteed to be NUL
  164. * terminated. Applications must not make assumptions about how this
  165. * string is formatted.
  166. */
  167. uint8_t release_string[AVB_RELEASE_STRING_SIZE];
  168. /* 176: Padding to ensure struct is size AVB_VBMETA_IMAGE_HEADER_SIZE
  169. * bytes. This must be set to zeroes.
  170. */
  171. uint8_t reserved[80];
  172. } AVB_ATTR_PACKED AvbVBMetaImageHeader;
  173. /* Copies |src| to |dest|, byte-swapping fields in the process.
  174. *
  175. * Make sure you've verified |src| using avb_vbmeta_image_verify()
  176. * before accessing the data and/or using this function.
  177. */
  178. void avb_vbmeta_image_header_to_host_byte_order(const AvbVBMetaImageHeader* src,
  179. AvbVBMetaImageHeader* dest);
  180. /* Return codes used in avb_vbmeta_image_verify().
  181. *
  182. * AVB_VBMETA_VERIFY_RESULT_OK is returned if the vbmeta image header
  183. * is valid, the hash is correct and the signature is correct. Keep in
  184. * mind that you still need to check that you know the public key used
  185. * to sign the image, see avb_vbmeta_image_verify() for details.
  186. *
  187. * AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED is returned if the vbmeta
  188. * image header is valid but there is no signature or hash.
  189. *
  190. * AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER is returned if the
  191. * header of the vbmeta image is invalid, for example, invalid magic
  192. * or inconsistent data.
  193. *
  194. * AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION is returned if a) the
  195. * vbmeta image requires a minimum version of libavb which exceeds the
  196. * version of libavb used; or b) the vbmeta image major version
  197. * differs from the major version of libavb in use.
  198. *
  199. * AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH is returned if the hash
  200. * stored in the "Authentication data" block does not match the
  201. * calculated hash.
  202. *
  203. * AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH is returned if the
  204. * signature stored in the "Authentication data" block is invalid or
  205. * doesn't match the public key stored in the vbmeta image.
  206. */
  207. typedef enum {
  208. AVB_VBMETA_VERIFY_RESULT_OK,
  209. AVB_VBMETA_VERIFY_RESULT_OK_NOT_SIGNED,
  210. AVB_VBMETA_VERIFY_RESULT_INVALID_VBMETA_HEADER,
  211. AVB_VBMETA_VERIFY_RESULT_UNSUPPORTED_VERSION,
  212. AVB_VBMETA_VERIFY_RESULT_HASH_MISMATCH,
  213. AVB_VBMETA_VERIFY_RESULT_SIGNATURE_MISMATCH,
  214. } AvbVBMetaVerifyResult;
  215. /* Get a textual representation of |result|. */
  216. const char* avb_vbmeta_verify_result_to_string(AvbVBMetaVerifyResult result);
  217. /* Checks that vbmeta image at |data| of size |length| is a valid
  218. * vbmeta image. The complete contents of the vbmeta image must be
  219. * passed in. It's fine if |length| is bigger than the actual image,
  220. * typically callers of this function will load the entire contents of
  221. * the 'vbmeta_a' or 'vbmeta_b' partition and pass in its length (for
  222. * example, 1 MiB).
  223. *
  224. * See the |AvbVBMetaImageHeader| struct for information about the
  225. * three blocks (header, authentication, auxiliary) that make up a
  226. * vbmeta image.
  227. *
  228. * If the function returns |AVB_VBMETA_VERIFY_RESULT_OK| and
  229. * |out_public_key_data| is non-NULL, it will be set to point inside
  230. * |data| for where the serialized public key data is stored and
  231. * |out_public_key_length|, if non-NULL, will be set to the length of
  232. * the public key data. If there is no public key in the metadata then
  233. * |out_public_key_data| is set to NULL.
  234. *
  235. * See the |AvbVBMetaVerifyResult| enum for possible return values.
  236. *
  237. * VERY IMPORTANT:
  238. *
  239. * 1. Even if |AVB_VBMETA_VERIFY_RESULT_OK| is returned, you still
  240. * need to check that the public key embedded in the image
  241. * matches a known key! You can use 'avbtool extract_public_key'
  242. * to extract the key (at build time, then store it along your
  243. * code) and compare it to what is returned in
  244. * |out_public_key_data|.
  245. *
  246. * 2. You need to check the |rollback_index| field against a stored
  247. * value in NVRAM and reject the vbmeta image if the value in
  248. * NVRAM is bigger than |rollback_index|. You must also update
  249. * the value stored in NVRAM to the smallest value of
  250. * |rollback_index| field from boot images in all bootable and
  251. * authentic slots marked as GOOD.
  252. *
  253. * This is a low-level function to only verify the vbmeta data - you
  254. * are likely looking for avb_slot_verify() instead for verifying
  255. * integrity data for a whole set of partitions.
  256. */
  257. AvbVBMetaVerifyResult avb_vbmeta_image_verify(
  258. const uint8_t* data,
  259. size_t length,
  260. const uint8_t** out_public_key_data,
  261. size_t* out_public_key_length) AVB_ATTR_WARN_UNUSED_RESULT;
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265. #endif /* AVB_VBMETA_IMAGE_H_ */