Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 1 | // Copyright 2021, 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 | //! Routines for handling APEX payload |
| 16 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 17 | use std::fs::File; |
Andrew Scull | 3812725 | 2022-06-13 13:11:00 +0000 | [diff] [blame] | 18 | use std::io::{self, Read}; |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 19 | use thiserror::Error; |
Andrew Scull | 3812725 | 2022-06-13 13:11:00 +0000 | [diff] [blame] | 20 | use vbmeta::VbMetaImage; |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 21 | use zip::result::ZipError; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 22 | use zip::ZipArchive; |
| 23 | |
| 24 | const APEX_PUBKEY_ENTRY: &str = "apex_pubkey"; |
| 25 | const APEX_PAYLOAD_ENTRY: &str = "apex_payload.img"; |
| 26 | |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 27 | /// Errors from parsing an APEX. |
| 28 | #[derive(Debug, Error)] |
| 29 | pub enum ApexParseError { |
| 30 | /// There was an IO error. |
| 31 | #[error("IO error")] |
| 32 | Io(#[from] io::Error), |
| 33 | /// The Zip archive was invalid. |
| 34 | #[error("Cannot read zip archive")] |
| 35 | InvalidZip(&'static str), |
| 36 | /// The apex_pubkey file was missing from the APEX. |
| 37 | #[error("APEX doesn't contain apex_pubkey")] |
| 38 | PubkeyMissing, |
| 39 | /// The apex_payload.img file was missing from the APEX. |
| 40 | #[error("APEX doesn't contain apex_payload.img")] |
| 41 | PayloadMissing, |
Andrew Scull | 3812725 | 2022-06-13 13:11:00 +0000 | [diff] [blame] | 42 | /// There was no hashtree descriptor in the APEX payload's VBMeta image. |
| 43 | #[error("Non-hashtree descriptor found in payload's VBMeta image")] |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 44 | DescriptorNotHashtree, |
Andrew Scull | 3812725 | 2022-06-13 13:11:00 +0000 | [diff] [blame] | 45 | /// There was an error parsing the APEX payload's VBMeta image. |
| 46 | #[error("Could not parse payload's VBMeta image")] |
| 47 | PayloadVbmetaError(#[from] vbmeta::VbMetaImageParseError), |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /// Errors from verifying an APEX. |
| 51 | #[derive(Debug, Error)] |
| 52 | pub enum ApexVerificationError { |
| 53 | /// There was an error parsing the APEX. |
| 54 | #[error("Cannot parse APEX file")] |
| 55 | ParseError(#[from] ApexParseError), |
Andrew Scull | 3812725 | 2022-06-13 13:11:00 +0000 | [diff] [blame] | 56 | /// There was an error validating the APEX payload's VBMeta image. |
| 57 | #[error("Could not parse payload's VBMeta image")] |
| 58 | PayloadVbmetaError(#[from] vbmeta::VbMetaImageVerificationError), |
| 59 | /// The APEX payload was not verified with the apex_pubkey. |
| 60 | #[error("APEX pubkey mismatch")] |
| 61 | ApexPubkeyMistmatch, |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 64 | /// Verification result holds public key and root digest of apex_payload.img |
| 65 | pub struct ApexVerificationResult { |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 66 | /// The public key that verifies the payload signature. |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 67 | pub public_key: Vec<u8>, |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 68 | /// The root digest of the payload hashtree. |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 69 | pub root_digest: Vec<u8>, |
| 70 | } |
| 71 | |
| 72 | /// Verify APEX payload by AVB verification and return public key and root digest |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 73 | pub fn verify(path: &str) -> Result<ApexVerificationResult, ApexVerificationError> { |
| 74 | let apex_file = File::open(path).map_err(ApexParseError::Io)?; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 75 | let (public_key, image_offset, image_size) = get_public_key_and_image_info(&apex_file)?; |
Andrew Scull | 3812725 | 2022-06-13 13:11:00 +0000 | [diff] [blame] | 76 | let vbmeta = VbMetaImage::verify_reader_region(apex_file, image_offset, image_size)?; |
| 77 | let root_digest = find_root_digest(&vbmeta)?; |
| 78 | match vbmeta.public_key() { |
| 79 | Some(payload_public_key) if public_key == payload_public_key => { |
| 80 | Ok(ApexVerificationResult { public_key, root_digest }) |
| 81 | } |
| 82 | _ => Err(ApexVerificationError::ApexPubkeyMistmatch), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | fn find_root_digest(vbmeta: &VbMetaImage) -> Result<Vec<u8>, ApexParseError> { |
| 87 | // APEXs use the root digest from the first hashtree descriptor to describe the payload. |
| 88 | for descriptor in vbmeta.descriptors()?.iter() { |
| 89 | if let vbmeta::Descriptor::Hashtree(_) = descriptor { |
| 90 | return Ok(descriptor.to_hashtree()?.root_digest().to_vec()); |
| 91 | } |
| 92 | } |
| 93 | Err(ApexParseError::DescriptorNotHashtree) |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 94 | } |
| 95 | |
Alice Wang | 3356d6d | 2022-07-18 08:32:10 +0000 | [diff] [blame] | 96 | /// Gets the hash of the payload's verified VBMeta image data. |
| 97 | pub fn get_payload_vbmeta_image_hash(path: &str) -> Result<Vec<u8>, ApexVerificationError> { |
| 98 | let apex_file = File::open(path).map_err(ApexParseError::Io)?; |
| 99 | let (_, offset, size) = get_public_key_and_image_info(&apex_file)?; |
| 100 | let vbmeta = VbMetaImage::verify_reader_region(apex_file, offset, size)?; |
| 101 | Ok(vbmeta.hash().ok_or(ApexVerificationError::ApexPubkeyMistmatch)?.to_vec()) |
| 102 | } |
| 103 | |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 104 | fn get_public_key_and_image_info(apex_file: &File) -> Result<(Vec<u8>, u64, u64), ApexParseError> { |
| 105 | let mut z = ZipArchive::new(apex_file).map_err(|err| match err { |
| 106 | ZipError::Io(err) => ApexParseError::Io(err), |
| 107 | ZipError::InvalidArchive(s) | ZipError::UnsupportedArchive(s) => { |
| 108 | ApexParseError::InvalidZip(s) |
| 109 | } |
| 110 | ZipError::FileNotFound => unreachable!(), |
| 111 | })?; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 112 | |
| 113 | let mut public_key = Vec::new(); |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 114 | z.by_name(APEX_PUBKEY_ENTRY) |
| 115 | .map_err(|err| match err { |
| 116 | ZipError::Io(err) => ApexParseError::Io(err), |
| 117 | ZipError::FileNotFound => ApexParseError::PubkeyMissing, |
| 118 | ZipError::InvalidArchive(s) | ZipError::UnsupportedArchive(s) => { |
| 119 | ApexParseError::InvalidZip(s) |
| 120 | } |
| 121 | })? |
| 122 | .read_to_end(&mut public_key)?; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 123 | |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 124 | let (image_offset, image_size) = z |
| 125 | .by_name(APEX_PAYLOAD_ENTRY) |
| 126 | .map(|f| (f.data_start(), f.size())) |
| 127 | .map_err(|err| match err { |
| 128 | ZipError::Io(err) => ApexParseError::Io(err), |
| 129 | ZipError::FileNotFound => ApexParseError::PayloadMissing, |
| 130 | ZipError::InvalidArchive(s) | ZipError::UnsupportedArchive(s) => { |
| 131 | ApexParseError::InvalidZip(s) |
| 132 | } |
| 133 | })?; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 134 | |
| 135 | Ok((public_key, image_offset, image_size)) |
| 136 | } |
| 137 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 138 | #[cfg(test)] |
| 139 | mod tests { |
| 140 | use super::*; |
Alice Wang | 3356d6d | 2022-07-18 08:32:10 +0000 | [diff] [blame] | 141 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 142 | #[test] |
Alice Wang | f28a518 | 2022-09-26 09:02:03 +0000 | [diff] [blame] | 143 | fn apex_verification_returns_valid_result() { |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 144 | let res = verify("tests/data/test.apex").unwrap(); |
Alice Wang | f28a518 | 2022-09-26 09:02:03 +0000 | [diff] [blame] | 145 | // The expected hex is generated when we ran the method the first time. |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 146 | assert_eq!( |
Charisee | 96113f3 | 2023-01-26 09:00:42 +0000 | [diff] [blame] | 147 | hex::encode(res.root_digest), |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 148 | "fe11ab17da0a3a738b54bdc3a13f6139cbdf91ec32f001f8d4bbbf8938e04e39" |
| 149 | ); |
| 150 | } |
Alice Wang | 3356d6d | 2022-07-18 08:32:10 +0000 | [diff] [blame] | 151 | |
| 152 | #[test] |
Alice Wang | f28a518 | 2022-09-26 09:02:03 +0000 | [diff] [blame] | 153 | fn payload_vbmeta_has_valid_image_hash() { |
Alice Wang | 3356d6d | 2022-07-18 08:32:10 +0000 | [diff] [blame] | 154 | let result = get_payload_vbmeta_image_hash("tests/data/test.apex").unwrap(); |
| 155 | assert_eq!( |
Charisee | 96113f3 | 2023-01-26 09:00:42 +0000 | [diff] [blame] | 156 | hex::encode(result), |
Alice Wang | 3356d6d | 2022-07-18 08:32:10 +0000 | [diff] [blame] | 157 | "296e32a76544de9da01713e471403ab4667705ad527bb4f1fac0cf61e7ce122d" |
| 158 | ); |
| 159 | } |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 160 | } |