Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! This module regroups methods related to AvbOps. |
| 16 | |
| 17 | #![warn(unsafe_op_in_unsafe_fn)] |
| 18 | // TODO(b/256148034): Remove this when the feature is code complete. |
| 19 | #![allow(dead_code)] |
| 20 | #![allow(unused_imports)] |
| 21 | |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 22 | use alloc::ffi::CString; |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 23 | use avb_bindgen::{avb_slot_verify, AvbHashtreeErrorMode, AvbSlotVerifyFlags, AvbSlotVerifyResult}; |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 24 | use core::fmt; |
| 25 | use log::debug; |
| 26 | |
| 27 | /// Error code from AVB image verification. |
| 28 | #[derive(Clone, Copy, Debug)] |
| 29 | pub enum AvbImageVerifyError { |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 30 | /// AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 31 | InvalidArgument, |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 32 | /// AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 33 | InvalidMetadata, |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 34 | /// AVB_SLOT_VERIFY_RESULT_ERROR_IO |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 35 | Io, |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 36 | /// AVB_SLOT_VERIFY_RESULT_ERROR_OOM |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 37 | Oom, |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 38 | /// AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 39 | PublicKeyRejected, |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 40 | /// AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 41 | RollbackIndex, |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 42 | /// AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 43 | UnsupportedVersion, |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 44 | /// AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 45 | Verification, |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 48 | fn to_avb_verify_result(result: AvbSlotVerifyResult) -> Result<(), AvbImageVerifyError> { |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 49 | match result { |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 50 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_OK => Ok(()), |
| 51 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT => { |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 52 | Err(AvbImageVerifyError::InvalidArgument) |
| 53 | } |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 54 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA => { |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 55 | Err(AvbImageVerifyError::InvalidMetadata) |
| 56 | } |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 57 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_IO => Err(AvbImageVerifyError::Io), |
| 58 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_OOM => Err(AvbImageVerifyError::Oom), |
| 59 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED => { |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 60 | Err(AvbImageVerifyError::PublicKeyRejected) |
| 61 | } |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 62 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX => { |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 63 | Err(AvbImageVerifyError::RollbackIndex) |
| 64 | } |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 65 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION => { |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 66 | Err(AvbImageVerifyError::UnsupportedVersion) |
| 67 | } |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 68 | AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION => { |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 69 | Err(AvbImageVerifyError::Verification) |
| 70 | } |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | impl fmt::Display for AvbImageVerifyError { |
| 75 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 76 | match self { |
| 77 | Self::InvalidArgument => write!(f, "Invalid parameters."), |
| 78 | Self::InvalidMetadata => write!(f, "Invalid metadata."), |
| 79 | Self::Io => write!(f, "I/O error while trying to load data or get a rollback index."), |
| 80 | Self::Oom => write!(f, "Unable to allocate memory."), |
| 81 | Self::PublicKeyRejected => write!( |
| 82 | f, |
| 83 | "Everything is verified correctly out but the public key is not accepted. \ |
| 84 | This includes the case where integrity data is not signed." |
| 85 | ), |
| 86 | Self::RollbackIndex => write!(f, "Rollback index is less than its stored value."), |
| 87 | Self::UnsupportedVersion => write!( |
| 88 | f, |
| 89 | "Some of the metadata requires a newer version of libavb than what is in use." |
| 90 | ), |
| 91 | Self::Verification => write!(f, "Data does not verify."), |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// Verifies that for the given image: |
| 97 | /// - The given public key is acceptable. |
| 98 | /// - The VBMeta struct is valid. |
| 99 | /// - The partitions of the image match the descriptors of the verified VBMeta struct. |
| 100 | /// Returns Ok if everything is verified correctly and the public key is accepted. |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 101 | pub fn verify_image(_image: &[u8], _public_key: &[u8]) -> Result<(), AvbImageVerifyError> { |
| 102 | // TODO(b/256148034): Call verify_slot() from pvmfw. |
| 103 | AvbOps::new().verify_slot() |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /// TODO(b/256148034): Make AvbOps a rust wrapper of avb_bindgen::AvbOps using foreign_types. |
| 107 | struct AvbOps {} |
| 108 | |
| 109 | impl AvbOps { |
| 110 | fn new() -> Self { |
| 111 | AvbOps {} |
| 112 | } |
| 113 | |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 114 | fn verify_slot(&mut self) -> Result<(), AvbImageVerifyError> { |
| 115 | let flags = AvbSlotVerifyFlags::AVB_SLOT_VERIFY_FLAGS_NO_VBMETA_PARTITION; |
| 116 | let hashtree_error_mode = AvbHashtreeErrorMode::AVB_HASHTREE_ERROR_MODE_EIO; |
| 117 | debug!("flags: {:?}", flags); |
| 118 | debug!("hashtree_error_mode: {:?}", hashtree_error_mode); |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 119 | // TODO(b/256148034): Verify the kernel image with avb_slot_verify() |
| 120 | // let result = unsafe { |
| 121 | // avb_slot_verify( |
| 122 | // self.as_ptr(), |
| 123 | // requested_partitions.as_ptr(), |
| 124 | // ab_suffix.as_ptr(), |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 125 | // flags, |
| 126 | // hashtree_error_mode, |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 127 | // &image.as_ptr(), |
| 128 | // ) |
| 129 | // }; |
Alice Wang | dc63fe0 | 2022-12-15 08:49:57 +0000 | [diff] [blame^] | 130 | let result = AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_OK; |
Alice Wang | 28cbcf1 | 2022-12-01 07:58:28 +0000 | [diff] [blame] | 131 | to_avb_verify_result(result) |
| 132 | } |
| 133 | } |