David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 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: |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 11 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 14 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 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. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 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_SLOT_VERIFY_H_ |
| 30 | #define AVB_SLOT_VERIFY_H_ |
| 31 | |
| 32 | #include "avb_ops.h" |
| 33 | #include "avb_vbmeta_image.h" |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | /* Return codes used in avb_slot_verify(), see that function for |
| 40 | * documentation for each field. |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 41 | * |
| 42 | * Use avb_slot_verify_result_to_string() to get a textual |
| 43 | * representation usable for error/debug output. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 44 | */ |
| 45 | typedef enum { |
| 46 | AVB_SLOT_VERIFY_RESULT_OK, |
| 47 | AVB_SLOT_VERIFY_RESULT_ERROR_OOM, |
| 48 | AVB_SLOT_VERIFY_RESULT_ERROR_IO, |
| 49 | AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, |
| 50 | AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX, |
| 51 | AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame] | 52 | AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA, |
| 53 | AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 54 | } AvbSlotVerifyResult; |
| 55 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 56 | /* Get a textual representation of |result|. */ |
| 57 | const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result); |
| 58 | |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 59 | /* Maximum number of rollback index locations supported. */ |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 60 | #define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS 32 |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 61 | |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 62 | /* AvbPartitionData contains data loaded from partitions when using |
| 63 | * avb_slot_verify(). The |partition_name| field contains the name of |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 64 | * the partition (without A/B suffix), |data| points to the loaded |
| 65 | * data which is |data_size| bytes long. |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 66 | * |
| 67 | * Note that this is strictly less than the partition size - it's only |
| 68 | * the image stored there, not the entire partition nor any of the |
| 69 | * metadata. |
| 70 | */ |
| 71 | typedef struct { |
| 72 | char* partition_name; |
| 73 | uint8_t* data; |
| 74 | size_t data_size; |
| 75 | } AvbPartitionData; |
| 76 | |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 77 | /* AvbVBMetaData contains a vbmeta struct loaded from a partition when |
| 78 | * using avb_slot_verify(). The |partition_name| field contains the |
| 79 | * name of the partition (without A/B suffix), |vbmeta_data| points to |
| 80 | * the loaded data which is |vbmeta_size| bytes long. |
| 81 | * |
| 82 | * The |verify_result| field contains the result of |
| 83 | * avb_vbmeta_image_verify() on the data. This is guaranteed to be |
| 84 | * AVB_VBMETA_VERIFY_RESULT_OK for all vbmeta images if |
| 85 | * avb_slot_verify() returns AVB_SLOT_VERIFY_RESULT_OK. |
| 86 | * |
| 87 | * You can use avb_descriptor_get_all(), avb_descriptor_foreach(), and |
| 88 | * avb_vbmeta_image_header_to_host_byte_order() with this data. |
| 89 | */ |
| 90 | typedef struct { |
| 91 | char* partition_name; |
| 92 | uint8_t* vbmeta_data; |
| 93 | size_t vbmeta_size; |
| 94 | AvbVBMetaVerifyResult verify_result; |
| 95 | } AvbVBMetaData; |
| 96 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 97 | /* AvbSlotVerifyData contains data needed to boot a particular slot |
| 98 | * and is returned by avb_slot_verify() if partitions in a slot are |
| 99 | * successfully verified. |
| 100 | * |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 101 | * All data pointed to by this struct - including data in each item in |
| 102 | * the |partitions| array - will be freed when the |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 103 | * avb_slot_verify_data_free() function is called. |
| 104 | * |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 105 | * The |ab_suffix| field is the copy of the of |ab_suffix| field |
| 106 | * passed to avb_slot_verify(). It is the A/B suffix of the slot. |
| 107 | * |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 108 | * The VBMeta images that were checked are available in the |
| 109 | * |vbmeta_images| field. The field |num_vbmeta_images| contains the |
| 110 | * number of elements in this array. The first element - |
David Zeuthen | a5fd3a4 | 2017-02-27 16:38:54 -0500 | [diff] [blame^] | 111 | * vbmeta_images[0] - is guaranteed to be from the partition with the |
| 112 | * top-level vbmeta struct. This is usually the "vbmeta" partition in |
| 113 | * the requested slot but if there is no "vbmeta" partition it can |
| 114 | * also be the "boot" partition. |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 115 | * |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 116 | * The partitions loaded and verified from from the slot are |
| 117 | * accessible in the |loaded_partitions| array. The field |
| 118 | * |num_loaded_partitions| contains the number of elements in this |
| 119 | * array. The order of partitions in this array may not necessarily be |
| 120 | * the same order as in the passed-in |requested_partitions| array. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 121 | * |
David Zeuthen | baf59e2 | 2016-11-14 15:39:43 -0500 | [diff] [blame] | 122 | * Rollback indexes for the verified slot are stored in the |
| 123 | * |rollback_indexes| field. Note that avb_slot_verify() will NEVER |
| 124 | * modify stored_rollback_index[n] locations e.g. it will never use |
| 125 | * the write_rollback_index() AvbOps operation. Instead it is the job |
| 126 | * of the caller of avb_slot_verify() to do this based on e.g. A/B |
| 127 | * policy and other factors. See libavb_ab/avb_ab_flow.c for an |
| 128 | * example of how to do this. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 129 | * |
| 130 | * The |cmdline| field is a NUL-terminated string in UTF-8 resulting |
| 131 | * from concatenating all |AvbKernelCmdlineDescriptor| and then |
| 132 | * performing proper substitution of the variables |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 133 | * $(ANDROID_SYSTEM_PARTUUID), $(ANDROID_BOOT_PARTUUID), and |
| 134 | * $(ANDROID_VBMETA_PARTUUID) using the |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 135 | * get_unique_guid_for_partition() operation in |AvbOps|. |
| 136 | * |
| 137 | * Additionally, the |cmdline| field will have the following kernel |
| 138 | * command-line options set: |
| 139 | * |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 140 | * androidboot.vbmeta.device_state: set to "locked" or "unlocked" |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 141 | * depending on the result of the result of AvbOps's |
| 142 | * read_is_unlocked() function. |
| 143 | * |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 144 | * androidboot.vbmeta.{hash_alg, size, digest}: Will be set to |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 145 | * the digest of all images in |vbmeta_images|. |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 146 | * |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 147 | * androidboot.vbmeta.device: This is set to the value |
| 148 | * PARTUUID=$(ANDROID_VBMETA_PARTUUID) before substitution so it |
| 149 | * will end up pointing to the vbmeta partition for the verified |
David Zeuthen | a5fd3a4 | 2017-02-27 16:38:54 -0500 | [diff] [blame^] | 150 | * slot. If there is no vbmeta partition it will point to the boot |
| 151 | * partition of the verified slot. |
David Zeuthen | 57ac06e | 2017-01-10 13:06:14 -0500 | [diff] [blame] | 152 | * |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame] | 153 | * androidboot.vbmeta.avb_version: This is set to the decimal value |
| 154 | * of AVB_VERSION_MAJOR followed by a dot followed by the decimal |
| 155 | * value of AVB_VERSION_MINOR, for example "1.0" or "1.4". This |
| 156 | * version number represents the vbmeta file format version |
| 157 | * supported by libavb copy used in the boot loader. This is not |
| 158 | * necessarily the same version number of the on-disk metadata for |
| 159 | * the slot that was verified. |
David Zeuthen | bc41cea | 2017-02-16 14:07:11 -0500 | [diff] [blame] | 160 | * |
David Zeuthen | 19c3843 | 2017-02-16 13:08:38 -0500 | [diff] [blame] | 161 | * Note that androidboot.slot_suffix is not set in |cmdline| - you |
David Zeuthen | bc41cea | 2017-02-16 14:07:11 -0500 | [diff] [blame] | 162 | * will have to pass this command-line option yourself. |
David Zeuthen | 19c3843 | 2017-02-16 13:08:38 -0500 | [diff] [blame] | 163 | * |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 164 | * This struct may grow in the future without it being considered an |
| 165 | * ABI break. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 166 | */ |
| 167 | typedef struct { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 168 | char* ab_suffix; |
David Zeuthen | 8681a33 | 2016-11-23 13:44:06 -0500 | [diff] [blame] | 169 | AvbVBMetaData* vbmeta_images; |
| 170 | size_t num_vbmeta_images; |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 171 | AvbPartitionData* loaded_partitions; |
| 172 | size_t num_loaded_partitions; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 173 | char* cmdline; |
David Zeuthen | 40ee1da | 2016-11-23 15:14:49 -0500 | [diff] [blame] | 174 | uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_LOCATIONS]; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 175 | } AvbSlotVerifyData; |
| 176 | |
| 177 | /* Frees a |AvbSlotVerifyData| including all data it points to. */ |
| 178 | void avb_slot_verify_data_free(AvbSlotVerifyData* data); |
| 179 | |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 180 | /* Performs a full verification of the slot identified by |ab_suffix| |
| 181 | * and load the contents of the partitions whose name is in the |
| 182 | * NULL-terminated string array |requested_partitions| (each partition |
| 183 | * must use hash verification). If not using A/B, pass an empty string |
| 184 | * (e.g. "", not NULL) for |ab_suffix|. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 185 | * |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 186 | * Typically the |requested_partitions| array only contains a single |
| 187 | * item for the boot partition, 'boot'. |
| 188 | * |
| 189 | * Verification includes loading data from the 'vbmeta', all hash |
| 190 | * partitions, and possibly other partitions (with |ab_suffix| |
| 191 | * appended), inspecting rollback indexes, and checking if the public |
| 192 | * key used to sign the data is acceptable. The functions in |ops| |
| 193 | * will be used to do this. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 194 | * |
| 195 | * If |out_data| is not NULL, it will be set to a newly allocated |
| 196 | * |AvbSlotVerifyData| struct containing all the data needed to |
| 197 | * actually boot the slot. This data structure should be freed with |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 198 | * avb_slot_verify_data_free() when you are done with it. See below |
| 199 | * for when this is returned. |
| 200 | * |
| 201 | * If |allow_verification_error| is false this function will bail out |
| 202 | * as soon as an error is encountered and |out_data| is set only if |
| 203 | * AVB_SLOT_VERIFY_RESULT_OK is returned. |
| 204 | * |
| 205 | * Otherwise if |allow_verification_error| is true the function will |
| 206 | * continue verification efforts and |out_data| is also set if |
| 207 | * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED, |
| 208 | * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION, or |
| 209 | * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned. It is |
| 210 | * undefined which error is returned if more than one distinct error |
| 211 | * is encountered. It is guaranteed that AVB_SLOT_VERIFY_RESULT_OK is |
| 212 | * returned if, and only if, there are no errors. This mode is needed |
| 213 | * to boot valid but unverified slots when the device is unlocked. |
| 214 | * |
| 215 | * Also note that |out_data| is never set if |
| 216 | * AVB_SLOT_VERIFY_RESULT_ERROR_OOM, AVB_SLOT_VERIFY_RESULT_ERROR_IO, |
| 217 | * or AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 218 | * |
| 219 | * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified |
| 220 | * correctly and all public keys are accepted. |
| 221 | * |
| 222 | * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if |
| 223 | * everything is verified correctly out but one or more public keys |
| 224 | * are not accepted. This includes the case where integrity data is |
| 225 | * not signed. |
| 226 | * |
| 227 | * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to |
| 228 | * allocate memory. |
| 229 | * |
| 230 | * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error |
| 231 | * occurred while trying to load data or get a rollback index. |
| 232 | * |
| 233 | * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data |
| 234 | * did not verify, e.g. the digest didn't match or signature checks |
| 235 | * failed. |
| 236 | * |
| 237 | * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a |
| 238 | * rollback index was less than its stored value. |
| 239 | * |
| 240 | * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some |
| 241 | * of the metadata is invalid or inconsistent. |
David Zeuthen | e3cadca | 2017-02-22 21:25:46 -0500 | [diff] [blame] | 242 | * |
| 243 | * AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION is returned if |
| 244 | * some of the metadata requires a newer version of libavb than what |
| 245 | * is in use. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 246 | */ |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame] | 247 | AvbSlotVerifyResult avb_slot_verify(AvbOps* ops, |
| 248 | const char* const* requested_partitions, |
| 249 | const char* ab_suffix, |
David Zeuthen | 0155e6b | 2016-11-16 17:58:13 -0500 | [diff] [blame] | 250 | bool allow_verification_error, |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 251 | AvbSlotVerifyData** out_data); |
| 252 | |
| 253 | #ifdef __cplusplus |
| 254 | } |
| 255 | #endif |
| 256 | |
| 257 | #endif /* AVB_SLOT_VERIFY_H_ */ |