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 | |
| 104 | /// Converts the buffer to a Hex String |
| 105 | pub fn to_hex_string(buf: &[u8]) -> String { |
| 106 | buf.iter().map(|b| format!("{:02x}", b)).collect() |
| 107 | } |
| 108 | |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 109 | fn get_public_key_and_image_info(apex_file: &File) -> Result<(Vec<u8>, u64, u64), ApexParseError> { |
| 110 | let mut z = ZipArchive::new(apex_file).map_err(|err| match err { |
| 111 | ZipError::Io(err) => ApexParseError::Io(err), |
| 112 | ZipError::InvalidArchive(s) | ZipError::UnsupportedArchive(s) => { |
| 113 | ApexParseError::InvalidZip(s) |
| 114 | } |
| 115 | ZipError::FileNotFound => unreachable!(), |
| 116 | })?; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 117 | |
| 118 | let mut public_key = Vec::new(); |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 119 | z.by_name(APEX_PUBKEY_ENTRY) |
| 120 | .map_err(|err| match err { |
| 121 | ZipError::Io(err) => ApexParseError::Io(err), |
| 122 | ZipError::FileNotFound => ApexParseError::PubkeyMissing, |
| 123 | ZipError::InvalidArchive(s) | ZipError::UnsupportedArchive(s) => { |
| 124 | ApexParseError::InvalidZip(s) |
| 125 | } |
| 126 | })? |
| 127 | .read_to_end(&mut public_key)?; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 128 | |
Andrew Scull | 9cbf2e0 | 2022-04-25 18:12:27 +0000 | [diff] [blame] | 129 | let (image_offset, image_size) = z |
| 130 | .by_name(APEX_PAYLOAD_ENTRY) |
| 131 | .map(|f| (f.data_start(), f.size())) |
| 132 | .map_err(|err| match err { |
| 133 | ZipError::Io(err) => ApexParseError::Io(err), |
| 134 | ZipError::FileNotFound => ApexParseError::PayloadMissing, |
| 135 | ZipError::InvalidArchive(s) | ZipError::UnsupportedArchive(s) => { |
| 136 | ApexParseError::InvalidZip(s) |
| 137 | } |
| 138 | })?; |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 139 | |
| 140 | Ok((public_key, image_offset, image_size)) |
| 141 | } |
| 142 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 143 | #[cfg(test)] |
| 144 | mod tests { |
| 145 | use super::*; |
Alice Wang | 3356d6d | 2022-07-18 08:32:10 +0000 | [diff] [blame^] | 146 | |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 147 | #[test] |
| 148 | fn test_open_apex() { |
| 149 | let res = verify("tests/data/test.apex").unwrap(); |
| 150 | assert_eq!( |
| 151 | to_hex_string(&res.root_digest), |
| 152 | "fe11ab17da0a3a738b54bdc3a13f6139cbdf91ec32f001f8d4bbbf8938e04e39" |
| 153 | ); |
| 154 | } |
Alice Wang | 3356d6d | 2022-07-18 08:32:10 +0000 | [diff] [blame^] | 155 | |
| 156 | #[test] |
| 157 | fn test_payload_vbmeta_image_hash() { |
| 158 | let result = get_payload_vbmeta_image_hash("tests/data/test.apex").unwrap(); |
| 159 | assert_eq!( |
| 160 | to_hex_string(&result), |
| 161 | "296e32a76544de9da01713e471403ab4667705ad527bb4f1fac0cf61e7ce122d" |
| 162 | ); |
| 163 | } |
Jooyung Han | c8deb47 | 2021-09-13 13:48:25 +0900 | [diff] [blame] | 164 | } |