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