Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +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 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 17 | use anyhow::{anyhow, bail, Context, Result}; |
Andrew Scull | 11d53ee | 2022-06-01 13:38:15 +0000 | [diff] [blame^] | 18 | use apkverify::pick_v4_apk_digest; |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 19 | use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; |
| 20 | use num_derive::{FromPrimitive, ToPrimitive}; |
| 21 | use num_traits::{FromPrimitive, ToPrimitive}; |
| 22 | use std::io::{copy, Cursor, Read, Seek, SeekFrom, Write}; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 23 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 24 | use crate::hashtree::*; |
| 25 | |
| 26 | // `apksigv4` module provides routines to decode and encode the idsig file as defined in [APK |
| 27 | // signature scheme v4] (https://source.android.com/security/apksigning/v4). |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 28 | |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 29 | /// `V4Signature` provides access to the various fields in an idsig file. |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 30 | #[derive(Default)] |
| 31 | pub struct V4Signature<R: Read + Seek> { |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 32 | /// Version of the header. Should be 2. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 33 | pub version: Version, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 34 | /// Provides access to the information about how the APK is hashed. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 35 | pub hashing_info: HashingInfo, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 36 | /// Provides access to the information that can be used to verify this file |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 37 | pub signing_info: SigningInfo, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 38 | /// Total size of the merkle tree |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 39 | pub merkle_tree_size: u32, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 40 | /// Offset of the merkle tree in the idsig file |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 41 | pub merkle_tree_offset: u64, |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 42 | |
| 43 | // Provides access to the underlying data |
| 44 | data: R, |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 45 | } |
| 46 | |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 47 | /// `HashingInfo` provides information about how the APK is hashed. |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 48 | #[derive(Default)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 49 | pub struct HashingInfo { |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 50 | /// Hash algorithm used when creating the merkle tree for the APK. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 51 | pub hash_algorithm: HashAlgorithm, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 52 | /// The log size of a block used when creating the merkle tree. 12 if 4k block was used. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 53 | pub log2_blocksize: u8, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 54 | /// The salt used when creating the merkle tree. 32 bytes max. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 55 | pub salt: Box<[u8]>, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 56 | /// The root hash of the merkle tree created. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 57 | pub raw_root_hash: Box<[u8]>, |
| 58 | } |
| 59 | |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 60 | /// `SigningInfo` provides information that can be used to verify the idsig file. |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 61 | #[derive(Default)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 62 | pub struct SigningInfo { |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 63 | /// Digest of the APK that this idsig file is for. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 64 | pub apk_digest: Box<[u8]>, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 65 | /// Certificate of the signer that signed this idsig file. ASN.1 DER form. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 66 | pub x509_certificate: Box<[u8]>, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 67 | /// A free-form binary data |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 68 | pub additional_data: Box<[u8]>, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 69 | /// Public key of the signer in ASN.1 DER form. This must match the `x509_certificate` field. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 70 | pub public_key: Box<[u8]>, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 71 | /// Signature algorithm used to sign this file. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 72 | pub signature_algorithm_id: SignatureAlgorithmId, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 73 | /// The signature of this file. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 74 | pub signature: Box<[u8]>, |
| 75 | } |
| 76 | |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 77 | /// Version of the idsig file format |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 78 | #[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 79 | #[repr(u32)] |
| 80 | pub enum Version { |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 81 | /// Version 2, the only supported version. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 82 | V2 = 2, |
| 83 | } |
| 84 | |
| 85 | impl Version { |
| 86 | fn from(val: u32) -> Result<Version> { |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 87 | Self::from_u32(val).ok_or_else(|| anyhow!("{} is an unsupported version", val)) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 91 | impl Default for Version { |
| 92 | fn default() -> Self { |
| 93 | Version::V2 |
| 94 | } |
| 95 | } |
| 96 | |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 97 | /// Hash algorithm that can be used for idsig file. |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 98 | #[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 99 | #[repr(u32)] |
| 100 | pub enum HashAlgorithm { |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 101 | /// SHA2-256 |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 102 | SHA256 = 1, |
| 103 | } |
| 104 | |
| 105 | impl HashAlgorithm { |
| 106 | fn from(val: u32) -> Result<HashAlgorithm> { |
Jiyong Park | 99a35b8 | 2021-06-07 10:13:44 +0900 | [diff] [blame] | 107 | Self::from_u32(val).ok_or_else(|| anyhow!("{} is an unsupported hash algorithm", val)) |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 111 | impl Default for HashAlgorithm { |
| 112 | fn default() -> Self { |
| 113 | HashAlgorithm::SHA256 |
| 114 | } |
| 115 | } |
| 116 | |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 117 | /// Signature algorithm that can be used for idsig file |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 118 | #[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)] |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 119 | #[allow(non_camel_case_types)] |
| 120 | #[repr(u32)] |
| 121 | pub enum SignatureAlgorithmId { |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 122 | /// RSASSA-PSS with SHA2-256 digest, SHA2-256 MGF1, 32 bytes of salt, trailer: 0xbc |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 123 | RSASSA_PSS_SHA2_256 = 0x0101, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 124 | /// RSASSA-PSS with SHA2-512 digest, SHA2-512 MGF1, 64 bytes of salt, trailer: 0xbc |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 125 | RSASSA_PSS_SHA2_512 = 0x0102, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 126 | /// RSASSA-PKCS1-v1_5 with SHA2-256 digest. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 127 | RSASSA_PKCS1_SHA2_256 = 0x0103, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 128 | /// RSASSA-PKCS1-v1_5 with SHA2-512 digest. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 129 | RSASSA_PKCS1_SHA2_512 = 0x0104, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 130 | /// ECDSA with SHA2-256 digest. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 131 | ECDSA_SHA2_256 = 0x0201, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 132 | /// ECDSA with SHA2-512 digest. |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 133 | ECDSA_SHA2_512 = 0x0202, |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 134 | /// DSA with SHA2-256 digest |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 135 | DSA_SHA2_256 = 0x0301, |
| 136 | } |
| 137 | |
| 138 | impl SignatureAlgorithmId { |
| 139 | fn from(val: u32) -> Result<SignatureAlgorithmId> { |
| 140 | Self::from_u32(val) |
| 141 | .with_context(|| format!("{:#06x} is an unsupported signature algorithm", val)) |
| 142 | } |
| 143 | } |
| 144 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 145 | impl Default for SignatureAlgorithmId { |
| 146 | fn default() -> Self { |
| 147 | SignatureAlgorithmId::DSA_SHA2_256 |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | impl<R: Read + Seek> V4Signature<R> { |
| 152 | /// Consumes a stream for an idsig file into a `V4Signature` struct. |
| 153 | pub fn from(mut r: R) -> Result<V4Signature<R>> { |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 154 | Ok(V4Signature { |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 155 | version: Version::from(r.read_u32::<LittleEndian>()?)?, |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 156 | hashing_info: HashingInfo::from(&mut r)?, |
| 157 | signing_info: SigningInfo::from(&mut r)?, |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 158 | merkle_tree_size: r.read_u32::<LittleEndian>()?, |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 159 | merkle_tree_offset: r.stream_position()?, |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 160 | data: r, |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 161 | }) |
| 162 | } |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 163 | |
| 164 | /// Read a stream for an APK file and creates a corresponding `V4Signature` struct that digests |
| 165 | /// the APK file. Note that the signing is not done. |
| 166 | pub fn create( |
| 167 | mut apk: &mut R, |
| 168 | block_size: usize, |
| 169 | salt: &[u8], |
| 170 | algorithm: HashAlgorithm, |
| 171 | ) -> Result<V4Signature<Cursor<Vec<u8>>>> { |
| 172 | // Determine the size of the apk |
| 173 | let start = apk.stream_position()?; |
| 174 | let size = apk.seek(SeekFrom::End(0))? as usize; |
| 175 | apk.seek(SeekFrom::Start(start))?; |
| 176 | |
| 177 | // Create hash tree (and root hash) |
| 178 | let algorithm = match algorithm { |
Andrew Scull | 462569d | 2022-05-24 10:29:19 +0000 | [diff] [blame] | 179 | HashAlgorithm::SHA256 => openssl::hash::MessageDigest::sha256(), |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 180 | }; |
| 181 | let hash_tree = HashTree::from(&mut apk, size, salt, block_size, algorithm)?; |
| 182 | |
| 183 | let mut ret = V4Signature { |
| 184 | version: Version::default(), |
| 185 | hashing_info: HashingInfo::default(), |
| 186 | signing_info: SigningInfo::default(), |
| 187 | merkle_tree_size: hash_tree.tree.len() as u32, |
| 188 | merkle_tree_offset: 0, // merkle tree starts from the beginning of `data` |
| 189 | data: Cursor::new(hash_tree.tree), |
| 190 | }; |
| 191 | ret.hashing_info.raw_root_hash = hash_tree.root_hash.into_boxed_slice(); |
| 192 | ret.hashing_info.log2_blocksize = log2(block_size); |
| 193 | |
Andrew Scull | 11d53ee | 2022-06-01 13:38:15 +0000 | [diff] [blame^] | 194 | apk.seek(SeekFrom::Start(start))?; |
| 195 | let (signature_algorithm_id, apk_digest) = pick_v4_apk_digest(apk)?; |
| 196 | ret.signing_info.signature_algorithm_id = |
| 197 | SignatureAlgorithmId::from(signature_algorithm_id)?; |
| 198 | ret.signing_info.apk_digest = apk_digest; |
| 199 | // TODO(jiyong): add a signature to the signing_info struct |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 200 | |
| 201 | Ok(ret) |
| 202 | } |
| 203 | |
| 204 | /// Writes the data into a writer |
| 205 | pub fn write_into<W: Write + Seek>(&mut self, mut w: &mut W) -> Result<()> { |
| 206 | // Writes the header part |
| 207 | w.write_u32::<LittleEndian>(self.version.to_u32().unwrap())?; |
| 208 | self.hashing_info.write_into(&mut w)?; |
| 209 | self.signing_info.write_into(&mut w)?; |
| 210 | w.write_u32::<LittleEndian>(self.merkle_tree_size)?; |
| 211 | |
| 212 | // Writes the merkle tree |
| 213 | self.data.seek(SeekFrom::Start(self.merkle_tree_offset))?; |
| 214 | let copied_size = copy(&mut self.data, &mut w)?; |
| 215 | if copied_size != self.merkle_tree_size as u64 { |
| 216 | bail!( |
| 217 | "merkle tree is {} bytes, but only {} bytes are written.", |
| 218 | self.merkle_tree_size, |
| 219 | copied_size |
| 220 | ); |
| 221 | } |
| 222 | Ok(()) |
| 223 | } |
| 224 | |
| 225 | /// Returns the bytes that represents the merkle tree |
| 226 | pub fn merkle_tree(&mut self) -> Result<Vec<u8>> { |
| 227 | self.data.seek(SeekFrom::Start(self.merkle_tree_offset))?; |
| 228 | let mut out = Vec::new(); |
| 229 | self.data.read_to_end(&mut out)?; |
| 230 | Ok(out) |
| 231 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | impl HashingInfo { |
| 235 | fn from(mut r: &mut dyn Read) -> Result<HashingInfo> { |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 236 | // Size of the entire hashing_info struct. We don't need this because each variable-sized |
| 237 | // fields in the struct are also length encoded. |
| 238 | r.read_u32::<LittleEndian>()?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 239 | Ok(HashingInfo { |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 240 | hash_algorithm: HashAlgorithm::from(r.read_u32::<LittleEndian>()?)?, |
| 241 | log2_blocksize: r.read_u8()?, |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 242 | salt: read_sized_array(&mut r)?, |
| 243 | raw_root_hash: read_sized_array(&mut r)?, |
| 244 | }) |
| 245 | } |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 246 | |
| 247 | fn write_into<W: Write + Seek>(&self, mut w: &mut W) -> Result<()> { |
| 248 | let start = w.stream_position()?; |
| 249 | // Size of the entire hashing_info struct. Since we don't know the size yet, fill the place |
| 250 | // with 0. The exact size will then be written below. |
| 251 | w.write_u32::<LittleEndian>(0)?; |
| 252 | |
| 253 | w.write_u32::<LittleEndian>(self.hash_algorithm.to_u32().unwrap())?; |
| 254 | w.write_u8(self.log2_blocksize)?; |
| 255 | write_sized_array(&mut w, &self.salt)?; |
| 256 | write_sized_array(&mut w, &self.raw_root_hash)?; |
| 257 | |
| 258 | // Determine the size of hashing_info, and write it in front of the struct where the value |
| 259 | // was initialized to zero. |
| 260 | let end = w.stream_position()?; |
| 261 | let size = end - start - std::mem::size_of::<u32>() as u64; |
| 262 | w.seek(SeekFrom::Start(start))?; |
| 263 | w.write_u32::<LittleEndian>(size as u32)?; |
| 264 | w.seek(SeekFrom::Start(end))?; |
| 265 | Ok(()) |
| 266 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | impl SigningInfo { |
| 270 | fn from(mut r: &mut dyn Read) -> Result<SigningInfo> { |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 271 | // Size of the entire signing_info struct. We don't need this because each variable-sized |
| 272 | // fields in the struct are also length encoded. |
| 273 | r.read_u32::<LittleEndian>()?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 274 | Ok(SigningInfo { |
| 275 | apk_digest: read_sized_array(&mut r)?, |
| 276 | x509_certificate: read_sized_array(&mut r)?, |
| 277 | additional_data: read_sized_array(&mut r)?, |
| 278 | public_key: read_sized_array(&mut r)?, |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 279 | signature_algorithm_id: SignatureAlgorithmId::from(r.read_u32::<LittleEndian>()?)?, |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 280 | signature: read_sized_array(&mut r)?, |
| 281 | }) |
| 282 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 283 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 284 | fn write_into<W: Write + Seek>(&self, mut w: &mut W) -> Result<()> { |
| 285 | let start = w.stream_position()?; |
| 286 | // Size of the entire signing_info struct. Since we don't know the size yet, fill the place |
| 287 | // with 0. The exact size will then be written below. |
| 288 | w.write_u32::<LittleEndian>(0)?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 289 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 290 | write_sized_array(&mut w, &self.apk_digest)?; |
| 291 | write_sized_array(&mut w, &self.x509_certificate)?; |
| 292 | write_sized_array(&mut w, &self.additional_data)?; |
| 293 | write_sized_array(&mut w, &self.public_key)?; |
| 294 | w.write_u32::<LittleEndian>(self.signature_algorithm_id.to_u32().unwrap())?; |
| 295 | write_sized_array(&mut w, &self.signature)?; |
| 296 | |
| 297 | // Determine the size of signing_info, and write it in front of the struct where the value |
| 298 | // was initialized to zero. |
| 299 | let end = w.stream_position()?; |
| 300 | let size = end - start - std::mem::size_of::<u32>() as u64; |
| 301 | w.seek(SeekFrom::Start(start))?; |
| 302 | w.write_u32::<LittleEndian>(size as u32)?; |
| 303 | w.seek(SeekFrom::Start(end))?; |
| 304 | Ok(()) |
| 305 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | fn read_sized_array(r: &mut dyn Read) -> Result<Box<[u8]>> { |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 309 | let size = r.read_u32::<LittleEndian>()?; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 310 | let mut data = vec![0; size as usize]; |
| 311 | r.read_exact(&mut data)?; |
| 312 | Ok(data.into_boxed_slice()) |
| 313 | } |
| 314 | |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 315 | fn write_sized_array(w: &mut dyn Write, data: &[u8]) -> Result<()> { |
| 316 | w.write_u32::<LittleEndian>(data.len() as u32)?; |
| 317 | Ok(w.write_all(data)?) |
| 318 | } |
| 319 | |
| 320 | fn log2(n: usize) -> u8 { |
| 321 | let num_bits = std::mem::size_of::<usize>() * 8; |
| 322 | (num_bits as u32 - n.leading_zeros() - 1) as u8 |
| 323 | } |
| 324 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 325 | #[cfg(test)] |
| 326 | mod tests { |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 327 | use super::*; |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 328 | use std::io::Cursor; |
| 329 | |
Jiyong Park | bde94ab | 2021-08-11 18:32:01 +0900 | [diff] [blame] | 330 | fn hexstring_from(s: &[u8]) -> String { |
| 331 | s.iter().map(|byte| format!("{:02x}", byte)).reduce(|i, j| i + &j).unwrap_or_default() |
| 332 | } |
| 333 | |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 334 | #[test] |
| 335 | fn parse_idsig_file() { |
| 336 | let idsig = Cursor::new(include_bytes!("../testdata/test.apk.idsig")); |
| 337 | let parsed = V4Signature::from(idsig).unwrap(); |
| 338 | |
| 339 | assert_eq!(Version::V2, parsed.version); |
| 340 | |
| 341 | let hi = parsed.hashing_info; |
| 342 | assert_eq!(HashAlgorithm::SHA256, hi.hash_algorithm); |
| 343 | assert_eq!(12, hi.log2_blocksize); |
| 344 | assert_eq!("", hexstring_from(hi.salt.as_ref())); |
| 345 | assert_eq!( |
| 346 | "ce1194fdb3cb2537daf0ac8cdf4926754adcbce5abeece7945fe25d204a0df6a", |
| 347 | hexstring_from(hi.raw_root_hash.as_ref()) |
| 348 | ); |
| 349 | |
| 350 | let si = parsed.signing_info; |
| 351 | assert_eq!( |
| 352 | "b5225523a813fb84ed599dd649698c080bcfed4fb19ddb00283a662a2683bc15", |
| 353 | hexstring_from(si.apk_digest.as_ref()) |
| 354 | ); |
| 355 | assert_eq!("", hexstring_from(si.additional_data.as_ref())); |
| 356 | assert_eq!( |
| 357 | "303d021c77304d0f4732a90372bbfce095223e4ba82427ceb381f69bc6762d78021d008b99924\ |
| 358 | a8585c38d7f654835eb219ae9e176b44e86dcb23153e3d9d6", |
| 359 | hexstring_from(si.signature.as_ref()) |
| 360 | ); |
| 361 | assert_eq!(SignatureAlgorithmId::DSA_SHA2_256, si.signature_algorithm_id); |
| 362 | |
| 363 | assert_eq!(36864, parsed.merkle_tree_size); |
| 364 | assert_eq!(2251, parsed.merkle_tree_offset); |
| 365 | } |
Jiyong Park | ec168e3 | 2021-08-13 10:26:34 +0900 | [diff] [blame] | 366 | |
| 367 | /// Parse an idsig file into V4Signature and write it. The written date must be the same as |
| 368 | /// the input file. |
| 369 | #[test] |
| 370 | fn parse_and_compose() { |
| 371 | let input = Cursor::new(include_bytes!("../testdata/test.apk.idsig")); |
| 372 | let mut parsed = V4Signature::from(input.clone()).unwrap(); |
| 373 | |
| 374 | let mut output = Cursor::new(Vec::new()); |
| 375 | parsed.write_into(&mut output).unwrap(); |
| 376 | |
| 377 | assert_eq!(input.get_ref().as_ref(), output.get_ref().as_slice()); |
| 378 | } |
| 379 | |
| 380 | /// Create V4Signature by hashing an APK. Merkle tree and the root hash should be the same |
| 381 | /// as those in the idsig file created by the signapk tool. |
| 382 | #[test] |
| 383 | fn digest_from_apk() { |
| 384 | let mut input = Cursor::new(include_bytes!("../testdata/test.apk")); |
| 385 | let mut created = |
| 386 | V4Signature::create(&mut input, 4096, &[], HashAlgorithm::SHA256).unwrap(); |
| 387 | |
| 388 | let golden = Cursor::new(include_bytes!("../testdata/test.apk.idsig")); |
| 389 | let mut golden = V4Signature::from(golden).unwrap(); |
| 390 | |
| 391 | // Compare the root hash |
| 392 | assert_eq!( |
| 393 | created.hashing_info.raw_root_hash.as_ref(), |
| 394 | golden.hashing_info.raw_root_hash.as_ref() |
| 395 | ); |
| 396 | |
| 397 | // Compare the merkle tree |
| 398 | assert_eq!( |
| 399 | created.merkle_tree().unwrap().as_slice(), |
| 400 | golden.merkle_tree().unwrap().as_slice() |
| 401 | ); |
| 402 | } |
Jiyong Park | 86c9b08 | 2021-06-04 19:03:48 +0900 | [diff] [blame] | 403 | } |