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