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, |
| 52 | AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA |
| 53 | } AvbSlotVerifyResult; |
| 54 | |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 55 | /* Get a textual representation of |result|. */ |
| 56 | const char* avb_slot_verify_result_to_string(AvbSlotVerifyResult result); |
| 57 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 58 | /* Maximum number of rollback index slots number supported. */ |
| 59 | #define AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_SLOTS 32 |
| 60 | |
| 61 | /* AvbSlotVerifyData contains data needed to boot a particular slot |
| 62 | * and is returned by avb_slot_verify() if partitions in a slot are |
| 63 | * successfully verified. |
| 64 | * |
| 65 | * All data pointed to by this struct will be freed when the |
| 66 | * avb_slot_verify_data_free() function is called. |
| 67 | * |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 68 | * The |ab_suffix| field is the copy of the of |ab_suffix| field |
| 69 | * passed to avb_slot_verify(). It is the A/B suffix of the slot. |
| 70 | * |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 71 | * The image loaded and verified from the boot partition of the slot |
| 72 | * is accessible via the |boot_data| and is of length |boot_size| |
| 73 | * bytes. Note that this is strictly less than the partition size - |
| 74 | * it's only the image stored there, not the entire partition nor any |
| 75 | * of the metadata. |
| 76 | * |
| 77 | * The verified vbmeta image in the 'vbmeta' partition of the slot is |
| 78 | * accessible from the |vbmeta_data| field and is of length |
| 79 | * |vbmeta_size| bytes. You can use this data with |
| 80 | * e.g. avb_descriptor_get_all(). |
| 81 | * |
| 82 | * Rollback indexes for the slot are stored in the |rollback_indexes| |
| 83 | * field. |
| 84 | * |
| 85 | * The |cmdline| field is a NUL-terminated string in UTF-8 resulting |
| 86 | * from concatenating all |AvbKernelCmdlineDescriptor| and then |
| 87 | * performing proper substitution of the variables |
| 88 | * $(ANDROID_SYSTEM_PARTUUID) and $(ANDROID_BOOT_PARTUUID) using the |
| 89 | * get_unique_guid_for_partition() operation in |AvbOps|. |
| 90 | * |
| 91 | * Additionally, the |cmdline| field will have the following kernel |
| 92 | * command-line options set: |
| 93 | * |
| 94 | * androidboot.avb.device_state: set to "locked" or "unlocked" |
| 95 | * depending on the result of the result of AvbOps's |
| 96 | * read_is_unlocked() function. |
| 97 | * |
| 98 | * androidboot.slot_suffix: If |ab_suffix| as passed into |
| 99 | * avb_slot_verify() is non-empty, this variable will be set to its |
| 100 | * value. |
| 101 | * |
| 102 | * androidboot.vbmeta.{hash_alg, size, digest}: Will be set to |
| 103 | * the digest of the vbmeta image. |
| 104 | */ |
| 105 | typedef struct { |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 106 | char* ab_suffix; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 107 | uint8_t* boot_data; |
| 108 | size_t boot_size; |
| 109 | uint8_t* vbmeta_data; |
| 110 | size_t vbmeta_size; |
| 111 | char* cmdline; |
| 112 | uint64_t rollback_indexes[AVB_MAX_NUMBER_OF_ROLLBACK_INDEX_SLOTS]; |
| 113 | } AvbSlotVerifyData; |
| 114 | |
| 115 | /* Frees a |AvbSlotVerifyData| including all data it points to. */ |
| 116 | void avb_slot_verify_data_free(AvbSlotVerifyData* data); |
| 117 | |
| 118 | /* Performs a full verification of the slot identified by |
| 119 | * |ab_suffix|. If not using A/B, pass an empty string (e.g. "", not |
| 120 | * NULL) for |ab_suffix|. |
| 121 | * |
| 122 | * This includes loading data from the 'vbmeta', 'boot', and possibly |
| 123 | * other partitions (with |ab_suffix| appended), inspecting rollback |
| 124 | * indexes, and checking if the public key used to sign the data is |
| 125 | * acceptable. The functions in |ops| will be used to do this. |
| 126 | * |
| 127 | * If |out_data| is not NULL, it will be set to a newly allocated |
| 128 | * |AvbSlotVerifyData| struct containing all the data needed to |
| 129 | * actually boot the slot. This data structure should be freed with |
| 130 | * avb_slot_verify_data_free() when you are done with it. |
| 131 | * |
| 132 | * AVB_SLOT_VERIFY_RESULT_OK is returned if everything is verified |
| 133 | * correctly and all public keys are accepted. |
| 134 | * |
| 135 | * AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED is returned if |
| 136 | * everything is verified correctly out but one or more public keys |
| 137 | * are not accepted. This includes the case where integrity data is |
| 138 | * not signed. |
| 139 | * |
| 140 | * AVB_SLOT_VERIFY_RESULT_ERROR_OOM is returned if unable to |
| 141 | * allocate memory. |
| 142 | * |
| 143 | * AVB_SLOT_VERIFY_RESULT_ERROR_IO is returned if an I/O error |
| 144 | * occurred while trying to load data or get a rollback index. |
| 145 | * |
| 146 | * AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION is returned if the data |
| 147 | * did not verify, e.g. the digest didn't match or signature checks |
| 148 | * failed. |
| 149 | * |
| 150 | * AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX is returned if a |
| 151 | * rollback index was less than its stored value. |
| 152 | * |
| 153 | * AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA is returned if some |
| 154 | * of the metadata is invalid or inconsistent. |
| 155 | */ |
| 156 | AvbSlotVerifyResult avb_slot_verify(AvbOps* ops, const char* ab_suffix, |
| 157 | AvbSlotVerifyData** out_data); |
| 158 | |
| 159 | #ifdef __cplusplus |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | #endif /* AVB_SLOT_VERIFY_H_ */ |