Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //! Utilities for Signature Verification |
| 18 | |
| 19 | use anyhow::{anyhow, bail, Result}; |
| 20 | use byteorder::{LittleEndian, ReadBytesExt}; |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 21 | use bytes::{Buf, BufMut, Bytes, BytesMut}; |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 22 | use openssl::hash::{DigestBytes, Hasher, MessageDigest}; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 23 | use std::cmp::min; |
| 24 | use std::io::{Cursor, Read, Seek, SeekFrom, Take}; |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 25 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 26 | use crate::ziputil::{set_central_directory_offset, zip_sections}; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 27 | |
| 28 | const APK_SIG_BLOCK_MIN_SIZE: u32 = 32; |
| 29 | const APK_SIG_BLOCK_MAGIC: u128 = 0x3234206b636f6c4220676953204b5041; |
| 30 | |
| 31 | // TODO(jooyung): introduce type |
Jooyung Han | 5b4c70e | 2021-08-09 16:36:13 +0900 | [diff] [blame] | 32 | pub const SIGNATURE_RSA_PSS_WITH_SHA256: u32 = 0x0101; |
| 33 | pub const SIGNATURE_RSA_PSS_WITH_SHA512: u32 = 0x0102; |
| 34 | pub const SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA256: u32 = 0x0103; |
| 35 | pub const SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA512: u32 = 0x0104; |
| 36 | pub const SIGNATURE_ECDSA_WITH_SHA256: u32 = 0x0201; |
| 37 | pub const SIGNATURE_ECDSA_WITH_SHA512: u32 = 0x0202; |
| 38 | pub const SIGNATURE_DSA_WITH_SHA256: u32 = 0x0301; |
| 39 | pub const SIGNATURE_VERITY_RSA_PKCS1_V1_5_WITH_SHA256: u32 = 0x0421; |
| 40 | pub const SIGNATURE_VERITY_ECDSA_WITH_SHA256: u32 = 0x0423; |
| 41 | pub const SIGNATURE_VERITY_DSA_WITH_SHA256: u32 = 0x0425; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 42 | |
| 43 | // TODO(jooyung): introduce type |
| 44 | const CONTENT_DIGEST_CHUNKED_SHA256: u32 = 1; |
| 45 | const CONTENT_DIGEST_CHUNKED_SHA512: u32 = 2; |
| 46 | const CONTENT_DIGEST_VERITY_CHUNKED_SHA256: u32 = 3; |
| 47 | #[allow(unused)] |
| 48 | const CONTENT_DIGEST_SHA256: u32 = 4; |
| 49 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 50 | const CHUNK_SIZE_BYTES: u64 = 1024 * 1024; |
| 51 | |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame^] | 52 | /// The [APK structure] has four major sections: |
| 53 | /// |
| 54 | /// | Zip contents | APK Signing Block | Central directory | EOCD(End of Central Directory) | |
| 55 | /// |
| 56 | /// This structure contains the offset/size information of all the sections except the Zip contents. |
| 57 | /// |
| 58 | /// [APK structure]: https://source.android.com/docs/security/apksigning/v2#apk-signing-block |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 59 | pub struct ApkSections<R> { |
| 60 | inner: R, |
| 61 | signing_block_offset: u32, |
| 62 | signing_block_size: u32, |
| 63 | central_directory_offset: u32, |
| 64 | central_directory_size: u32, |
| 65 | eocd_offset: u32, |
| 66 | eocd_size: u32, |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 69 | impl<R: Read + Seek> ApkSections<R> { |
| 70 | pub fn new(reader: R) -> Result<ApkSections<R>> { |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 71 | let (mut reader, zip_sections) = zip_sections(reader)?; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 72 | let (signing_block_offset, signing_block_size) = |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 73 | find_signing_block(&mut reader, zip_sections.central_directory_offset)?; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 74 | Ok(ApkSections { |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 75 | inner: reader, |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 76 | signing_block_offset, |
| 77 | signing_block_size, |
| 78 | central_directory_offset: zip_sections.central_directory_offset, |
| 79 | central_directory_size: zip_sections.central_directory_size, |
| 80 | eocd_offset: zip_sections.eocd_offset, |
| 81 | eocd_size: zip_sections.eocd_size, |
| 82 | }) |
| 83 | } |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 84 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 85 | /// Returns the APK Signature Scheme block contained in the provided file for the given ID |
| 86 | /// and the additional information relevant for verifying the block against the file. |
| 87 | pub fn find_signature(&mut self, block_id: u32) -> Result<Bytes> { |
| 88 | let signing_block = self.bytes(self.signing_block_offset, self.signing_block_size)?; |
| 89 | // TODO(jooyung): propagate NotFound error so that verification can fallback to V2 |
| 90 | find_signature_scheme_block(Bytes::from(signing_block), block_id) |
| 91 | } |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 92 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 93 | /// Computes digest with "signature algorithm" over APK contents, central directory, and EOCD. |
| 94 | /// 1. The digest of each chunk is computed over the concatenation of byte 0xa5, the chunk’s |
| 95 | /// length in bytes (little-endian uint32), and the chunk’s contents. |
| 96 | /// 2. The top-level digest is computed over the concatenation of byte 0x5a, the number of |
| 97 | /// chunks (little-endian uint32), and the concatenation of digests of the chunks in the |
| 98 | /// order the chunks appear in the APK. |
| 99 | /// (see https://source.android.com/security/apksigning/v2#integrity-protected-contents) |
| 100 | pub fn compute_digest(&mut self, signature_algorithm_id: u32) -> Result<Vec<u8>> { |
| 101 | let digester = Digester::new(signature_algorithm_id)?; |
| 102 | |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 103 | let mut digests_of_chunks = BytesMut::new(); |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 104 | let mut chunk_count = 0u32; |
| 105 | let mut chunk = vec![0u8; CHUNK_SIZE_BYTES as usize]; |
| 106 | for data in &[ |
| 107 | ApkSections::zip_entries, |
| 108 | ApkSections::central_directory, |
| 109 | ApkSections::eocd_for_verification, |
| 110 | ] { |
| 111 | let mut data = data(self)?; |
| 112 | while data.limit() > 0 { |
| 113 | let chunk_size = min(CHUNK_SIZE_BYTES, data.limit()); |
Chris Wailes | 641fc4a | 2021-12-01 15:03:21 -0800 | [diff] [blame] | 114 | let slice = &mut chunk[..(chunk_size as usize)]; |
| 115 | data.read_exact(slice)?; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 116 | digests_of_chunks.put_slice( |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 117 | digester.digest(slice, CHUNK_HEADER_MID, chunk_size as u32)?.as_ref(), |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 118 | ); |
| 119 | chunk_count += 1; |
| 120 | } |
| 121 | } |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 122 | Ok(digester.digest(&digests_of_chunks, CHUNK_HEADER_TOP, chunk_count)?.as_ref().into()) |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | fn zip_entries(&mut self) -> Result<Take<Box<dyn Read + '_>>> { |
| 126 | scoped_read(&mut self.inner, 0, self.signing_block_offset as u64) |
| 127 | } |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 128 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 129 | fn central_directory(&mut self) -> Result<Take<Box<dyn Read + '_>>> { |
| 130 | scoped_read( |
| 131 | &mut self.inner, |
| 132 | self.central_directory_offset as u64, |
| 133 | self.central_directory_size as u64, |
| 134 | ) |
| 135 | } |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 136 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 137 | fn eocd_for_verification(&mut self) -> Result<Take<Box<dyn Read + '_>>> { |
| 138 | let mut eocd = self.bytes(self.eocd_offset, self.eocd_size)?; |
| 139 | // Protection of section 4 (ZIP End of Central Directory) is complicated by the section |
| 140 | // containing the offset of ZIP Central Directory. The offset changes when the size of the |
| 141 | // APK Signing Block changes, for instance, when a new signature is added. Thus, when |
| 142 | // computing digest over the ZIP End of Central Directory, the field containing the offset |
| 143 | // of ZIP Central Directory must be treated as containing the offset of the APK Signing |
| 144 | // Block. |
| 145 | set_central_directory_offset(&mut eocd, self.signing_block_offset)?; |
| 146 | Ok(Read::take(Box::new(Cursor::new(eocd)), self.eocd_size as u64)) |
| 147 | } |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 148 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 149 | fn bytes(&mut self, offset: u32, size: u32) -> Result<Vec<u8>> { |
| 150 | self.inner.seek(SeekFrom::Start(offset as u64))?; |
| 151 | let mut buf = vec![0u8; size as usize]; |
| 152 | self.inner.read_exact(&mut buf)?; |
| 153 | Ok(buf) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn scoped_read<'a, R: Read + Seek>( |
| 158 | src: &'a mut R, |
| 159 | offset: u64, |
| 160 | size: u64, |
| 161 | ) -> Result<Take<Box<dyn Read + 'a>>> { |
| 162 | src.seek(SeekFrom::Start(offset))?; |
| 163 | Ok(Read::take(Box::new(src), size)) |
| 164 | } |
| 165 | |
| 166 | struct Digester { |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 167 | algorithm: MessageDigest, |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | const CHUNK_HEADER_TOP: &[u8] = &[0x5a]; |
| 171 | const CHUNK_HEADER_MID: &[u8] = &[0xa5]; |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 172 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 173 | impl Digester { |
| 174 | fn new(signature_algorithm_id: u32) -> Result<Digester> { |
| 175 | let digest_algorithm_id = to_content_digest_algorithm(signature_algorithm_id)?; |
| 176 | let algorithm = match digest_algorithm_id { |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 177 | CONTENT_DIGEST_CHUNKED_SHA256 => MessageDigest::sha256(), |
| 178 | CONTENT_DIGEST_CHUNKED_SHA512 => MessageDigest::sha512(), |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 179 | // TODO(jooyung): implement |
| 180 | CONTENT_DIGEST_VERITY_CHUNKED_SHA256 => { |
| 181 | bail!("TODO(b/190343842): CONTENT_DIGEST_VERITY_CHUNKED_SHA256: not implemented") |
| 182 | } |
| 183 | _ => bail!("Unknown digest algorithm: {}", digest_algorithm_id), |
| 184 | }; |
| 185 | Ok(Digester { algorithm }) |
| 186 | } |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 187 | |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 188 | // v2/v3 digests are computed after prepending "header" byte and "size" info. |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 189 | fn digest(&self, data: &[u8], header: &[u8], size: u32) -> Result<DigestBytes> { |
| 190 | let mut ctx = Hasher::new(self.algorithm)?; |
| 191 | ctx.update(header)?; |
| 192 | ctx.update(&size.to_le_bytes())?; |
| 193 | ctx.update(data)?; |
| 194 | Ok(ctx.finish()?) |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 195 | } |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 196 | } |
| 197 | |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 198 | fn find_signing_block<T: Read + Seek>( |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 199 | reader: &mut T, |
| 200 | central_directory_offset: u32, |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 201 | ) -> Result<(u32, u32)> { |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 202 | // FORMAT: |
| 203 | // OFFSET DATA TYPE DESCRIPTION |
| 204 | // * @+0 bytes uint64: size in bytes (excluding this field) |
| 205 | // * @+8 bytes payload |
| 206 | // * @-24 bytes uint64: size in bytes (same as the one above) |
| 207 | // * @-16 bytes uint128: magic |
| 208 | if central_directory_offset < APK_SIG_BLOCK_MIN_SIZE { |
| 209 | bail!( |
| 210 | "APK too small for APK Signing Block. ZIP Central Directory offset: {}", |
| 211 | central_directory_offset |
| 212 | ); |
| 213 | } |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 214 | reader.seek(SeekFrom::Start((central_directory_offset - 24) as u64))?; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 215 | let size_in_footer = reader.read_u64::<LittleEndian>()? as u32; |
| 216 | if reader.read_u128::<LittleEndian>()? != APK_SIG_BLOCK_MAGIC { |
| 217 | bail!("No APK Signing Block before ZIP Central Directory") |
| 218 | } |
| 219 | let total_size = size_in_footer + 8; |
| 220 | let signing_block_offset = central_directory_offset |
| 221 | .checked_sub(total_size) |
| 222 | .ok_or_else(|| anyhow!("APK Signing Block size out of range: {}", size_in_footer))?; |
Jooyung Han | 5d94bfc | 2021-08-06 14:07:49 +0900 | [diff] [blame] | 223 | reader.seek(SeekFrom::Start(signing_block_offset as u64))?; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 224 | let size_in_header = reader.read_u64::<LittleEndian>()? as u32; |
| 225 | if size_in_header != size_in_footer { |
| 226 | bail!( |
| 227 | "APK Signing Block sizes in header and footer do not match: {} vs {}", |
| 228 | size_in_header, |
| 229 | size_in_footer |
| 230 | ); |
| 231 | } |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 232 | Ok((signing_block_offset, total_size)) |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | fn find_signature_scheme_block(buf: Bytes, block_id: u32) -> Result<Bytes> { |
| 236 | // FORMAT: |
| 237 | // OFFSET DATA TYPE DESCRIPTION |
| 238 | // * @+0 bytes uint64: size in bytes (excluding this field) |
| 239 | // * @+8 bytes pairs |
| 240 | // * @-24 bytes uint64: size in bytes (same as the one above) |
| 241 | // * @-16 bytes uint128: magic |
| 242 | let mut pairs = buf.slice(8..(buf.len() - 24)); |
| 243 | let mut entry_count = 0; |
| 244 | while pairs.has_remaining() { |
| 245 | entry_count += 1; |
| 246 | if pairs.remaining() < 8 { |
| 247 | bail!("Insufficient data to read size of APK Signing Block entry #{}", entry_count); |
| 248 | } |
| 249 | let length = pairs.get_u64_le(); |
| 250 | let mut pair = pairs.split_to(length as usize); |
| 251 | let id = pair.get_u32_le(); |
| 252 | if id == block_id { |
| 253 | return Ok(pair); |
| 254 | } |
| 255 | } |
| 256 | // TODO(jooyung): return NotFound error |
| 257 | bail!("No APK Signature Scheme block in APK Signing Block with ID: {}", block_id) |
| 258 | } |
| 259 | |
| 260 | pub fn is_supported_signature_algorithm(algorithm_id: u32) -> bool { |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 261 | matches!( |
| 262 | algorithm_id, |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 263 | SIGNATURE_RSA_PSS_WITH_SHA256 |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 264 | | SIGNATURE_RSA_PSS_WITH_SHA512 |
| 265 | | SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA256 |
| 266 | | SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA512 |
| 267 | | SIGNATURE_ECDSA_WITH_SHA256 |
| 268 | | SIGNATURE_ECDSA_WITH_SHA512 |
| 269 | | SIGNATURE_DSA_WITH_SHA256 |
| 270 | | SIGNATURE_VERITY_RSA_PKCS1_V1_5_WITH_SHA256 |
| 271 | | SIGNATURE_VERITY_ECDSA_WITH_SHA256 |
| 272 | | SIGNATURE_VERITY_DSA_WITH_SHA256 |
| 273 | ) |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | fn to_content_digest_algorithm(algorithm_id: u32) -> Result<u32> { |
| 277 | match algorithm_id { |
| 278 | SIGNATURE_RSA_PSS_WITH_SHA256 |
| 279 | | SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA256 |
| 280 | | SIGNATURE_ECDSA_WITH_SHA256 |
| 281 | | SIGNATURE_DSA_WITH_SHA256 => Ok(CONTENT_DIGEST_CHUNKED_SHA256), |
| 282 | SIGNATURE_RSA_PSS_WITH_SHA512 |
| 283 | | SIGNATURE_RSA_PKCS1_V1_5_WITH_SHA512 |
| 284 | | SIGNATURE_ECDSA_WITH_SHA512 => Ok(CONTENT_DIGEST_CHUNKED_SHA512), |
| 285 | SIGNATURE_VERITY_RSA_PKCS1_V1_5_WITH_SHA256 |
| 286 | | SIGNATURE_VERITY_ECDSA_WITH_SHA256 |
| 287 | | SIGNATURE_VERITY_DSA_WITH_SHA256 => Ok(CONTENT_DIGEST_VERITY_CHUNKED_SHA256), |
| 288 | _ => bail!("Unknown signature algorithm: {}", algorithm_id), |
| 289 | } |
| 290 | } |
| 291 | |
Andrew Scull | 9173eb8 | 2022-06-01 09:17:14 +0000 | [diff] [blame] | 292 | /// Rank the signature algorithm according to the preferences of the v4 signing scheme. |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 293 | pub fn rank_signature_algorithm(algo: u32) -> Result<u32> { |
| 294 | rank_content_digest_algorithm(to_content_digest_algorithm(algo)?) |
| 295 | } |
| 296 | |
| 297 | fn rank_content_digest_algorithm(id: u32) -> Result<u32> { |
| 298 | match id { |
| 299 | CONTENT_DIGEST_CHUNKED_SHA256 => Ok(0), |
| 300 | CONTENT_DIGEST_VERITY_CHUNKED_SHA256 => Ok(1), |
| 301 | CONTENT_DIGEST_CHUNKED_SHA512 => Ok(2), |
| 302 | _ => bail!("Unknown digest algorithm: {}", id), |
| 303 | } |
| 304 | } |
Alice Wang | ed79eab | 2022-09-08 11:16:31 +0000 | [diff] [blame^] | 305 | |
| 306 | #[cfg(test)] |
| 307 | mod tests { |
| 308 | use super::*; |
| 309 | use byteorder::LittleEndian; |
| 310 | use std::fs::File; |
| 311 | use std::mem::size_of_val; |
| 312 | |
| 313 | const CENTRAL_DIRECTORY_HEADER_SIGNATURE: u32 = 0x02014b50; |
| 314 | |
| 315 | #[test] |
| 316 | fn test_apk_sections() { |
| 317 | let apk_file = File::open("tests/data/v3-only-with-ecdsa-sha512-p521.apk").unwrap(); |
| 318 | let apk_sections = ApkSections::new(apk_file).unwrap(); |
| 319 | let mut reader = &apk_sections.inner; |
| 320 | |
| 321 | // Checks APK Signing Block. |
| 322 | assert_eq!( |
| 323 | apk_sections.signing_block_offset + apk_sections.signing_block_size, |
| 324 | apk_sections.central_directory_offset |
| 325 | ); |
| 326 | let apk_signature_offset = SeekFrom::Start( |
| 327 | apk_sections.central_directory_offset as u64 - size_of_val(&APK_SIG_BLOCK_MAGIC) as u64, |
| 328 | ); |
| 329 | reader.seek(apk_signature_offset).unwrap(); |
| 330 | assert_eq!(reader.read_u128::<LittleEndian>().unwrap(), APK_SIG_BLOCK_MAGIC); |
| 331 | |
| 332 | // Checks Central directory. |
| 333 | assert_eq!(reader.read_u32::<LittleEndian>().unwrap(), CENTRAL_DIRECTORY_HEADER_SIGNATURE); |
| 334 | assert_eq!( |
| 335 | apk_sections.central_directory_offset + apk_sections.central_directory_size, |
| 336 | apk_sections.eocd_offset |
| 337 | ); |
| 338 | |
| 339 | // Checks EOCD. |
| 340 | assert_eq!( |
| 341 | reader.metadata().unwrap().len(), |
| 342 | (apk_sections.eocd_offset + apk_sections.eocd_size) as u64 |
| 343 | ); |
| 344 | } |
| 345 | } |