blob: 680c81e034115ae7a0923edf3b23a360ee2b1b70 [file] [log] [blame]
Jooyung Han12a0b702021-08-05 23:20:31 +09001/*
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
Alice Wang676bb4a2022-09-19 14:21:39 +000017use apkverify::{
Alice Wang0cafa142022-09-23 15:17:02 +000018 get_apk_digest, get_public_key_der, testing::assert_contains, verify, SignatureAlgorithmID,
Alice Wang676bb4a2022-09-19 14:21:39 +000019};
Alan Stokes068f6d42023-10-09 10:13:03 +010020use apkzip::zip_sections;
21use byteorder::{LittleEndian, ReadBytesExt};
Alan Stokes25c86212023-03-09 17:22:19 +000022use log::info;
Alan Stokes068f6d42023-10-09 10:13:03 +010023use std::io::{Seek, SeekFrom};
Alice Wang67d3c002022-09-16 10:08:25 +000024use std::{fs, matches, path::Path};
Jooyung Hand8397852021-08-10 16:29:36 +090025
Seungjae Yoo91e250a2022-06-07 02:21:56 +000026const KEY_NAMES_DSA: &[&str] = &["1024", "2048", "3072"];
27const KEY_NAMES_ECDSA: &[&str] = &["p256", "p384", "p521"];
28const KEY_NAMES_RSA: &[&str] = &["1024", "2048", "3072", "4096", "8192", "16384"];
Jooyung Hancee6de62021-08-11 15:52:07 +090029
Alan Stokes25f69362023-03-06 16:51:54 +000030const SDK_INT: u32 = 31;
31
Alan Stokes25c86212023-03-09 17:22:19 +000032/// Make sure any logging from the code under test ends up in logcat.
33fn setup() {
34 android_logger::init_once(
35 android_logger::Config::default()
36 .with_tag("apkverify_test")
37 .with_min_level(log::Level::Info),
38 );
39 info!("Test starting");
40}
41
Jooyung Hancee6de62021-08-11 15:52:07 +090042#[test]
Alan Stokes068f6d42023-10-09 10:13:03 +010043fn test_zip_sections_with_apk() {
44 let mut reader = fs::File::open("tests/data/v3-only-with-stamp.apk").unwrap();
45 let sections = zip_sections(&mut reader).unwrap();
46
47 // Checks Central directory.
48 assert_eq!(
49 sections.central_directory_offset + sections.central_directory_size,
50 sections.eocd_offset
51 );
52
53 // Checks EOCD.
54 const EOCD_SIGNATURE: u32 = 0x06054b50;
55
56 reader.seek(SeekFrom::Start(sections.eocd_offset as u64)).unwrap();
57 assert_eq!(reader.read_u32::<LittleEndian>().unwrap(), EOCD_SIGNATURE);
58 assert_eq!(
59 reader.metadata().unwrap().len(),
60 (sections.eocd_offset + sections.eocd_size) as u64
61 );
62}
63
64#[test]
Jooyung Hancee6de62021-08-11 15:52:07 +090065fn test_verify_truncated_cd() {
Alan Stokes25c86212023-03-09 17:22:19 +000066 setup();
Jooyung Hancee6de62021-08-11 15:52:07 +090067 use zip::result::ZipError;
Alan Stokes25f69362023-03-06 16:51:54 +000068 let res = verify("tests/data/v2-only-truncated-cd.apk", SDK_INT);
Alice Wang92889352022-09-16 10:42:52 +000069 // TODO(b/190343842): consider making a helper for err assertion
Jooyung Hancee6de62021-08-11 15:52:07 +090070 assert!(matches!(
Andrew Walbran117cd5e2021-08-13 11:42:13 +000071 res.unwrap_err().root_cause().downcast_ref::<ZipError>().unwrap(),
Jooyung Hancee6de62021-08-11 15:52:07 +090072 ZipError::InvalidArchive(_),
73 ));
74}
Seungjae Yoo91e250a2022-06-07 02:21:56 +000075
76#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000077fn apex_signed_with_v3_rsa_pkcs1_sha512_is_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +000078 setup();
Alice Wang676bb4a2022-09-19 14:21:39 +000079 validate_apk("tests/data/test.apex", SignatureAlgorithmID::RsaPkcs1V15WithSha512);
Seungjae Yoo91e250a2022-06-07 02:21:56 +000080}
81
Seungjae Yoo91e250a2022-06-07 02:21:56 +000082#[test]
Alice Wang50701022022-09-21 08:51:38 +000083fn apks_signed_with_v3_dsa_sha256_are_not_supported() {
Alan Stokes25c86212023-03-09 17:22:19 +000084 setup();
Seungjae Yoo91e250a2022-06-07 02:21:56 +000085 for key_name in KEY_NAMES_DSA.iter() {
Alan Stokes25f69362023-03-06 16:51:54 +000086 let res = verify(format!("tests/data/v3-only-with-dsa-sha256-{}.apk", key_name), SDK_INT);
Alice Wang50701022022-09-21 08:51:38 +000087 assert!(res.is_err(), "DSA algorithm is not supported for verification. See b/197052981.");
Alan Stokesa6876992023-01-20 12:26:25 +000088 assert_contains(&res.unwrap_err().to_string(), "No supported APK signatures found");
Alice Wang676bb4a2022-09-19 14:21:39 +000089 }
90}
91
92#[test]
93fn apks_signed_with_v3_ecdsa_sha256_are_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +000094 setup();
Seungjae Yoo91e250a2022-06-07 02:21:56 +000095 for key_name in KEY_NAMES_ECDSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000096 validate_apk(
97 format!("tests/data/v3-only-with-ecdsa-sha256-{}.apk", key_name),
98 SignatureAlgorithmID::EcdsaWithSha256,
99 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000100 }
101}
102
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000103#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000104fn apks_signed_with_v3_ecdsa_sha512_are_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +0000105 setup();
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000106 for key_name in KEY_NAMES_ECDSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +0000107 validate_apk(
108 format!("tests/data/v3-only-with-ecdsa-sha512-{}.apk", key_name),
109 SignatureAlgorithmID::EcdsaWithSha512,
110 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000111 }
112}
113
114#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000115fn apks_signed_with_v3_rsa_pkcs1_sha256_are_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +0000116 setup();
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000117 for key_name in KEY_NAMES_RSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +0000118 validate_apk(
119 format!("tests/data/v3-only-with-rsa-pkcs1-sha256-{}.apk", key_name),
120 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
121 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000122 }
123}
124
125#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000126fn apks_signed_with_v3_rsa_pkcs1_sha512_are_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +0000127 setup();
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000128 for key_name in KEY_NAMES_RSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +0000129 validate_apk(
130 format!("tests/data/v3-only-with-rsa-pkcs1-sha512-{}.apk", key_name),
131 SignatureAlgorithmID::RsaPkcs1V15WithSha512,
132 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000133 }
134}
135
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000136#[test]
Alan Stokes25c86212023-03-09 17:22:19 +0000137fn test_verify_v3_sig_min_max_sdk() {
138 setup();
139 // The Signer for this APK has min_sdk=24, max_sdk=32.
140 let path = "tests/data/v31-rsa-2048_2-tgt-33-1-tgt-28.apk";
141
142 let res = verify(path, 23);
143 assert!(res.is_err());
144 assert_contains(&res.unwrap_err().to_string(), "0 signers found");
145
146 let res = verify(path, 24);
147 assert!(res.is_ok());
148
149 let res = verify(path, 32);
150 assert!(res.is_ok());
151
152 let res = verify(path, 33);
153 assert!(res.is_err());
154 assert_contains(&res.unwrap_err().to_string(), "0 signers found");
155}
156
157#[test]
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000158fn test_verify_v3_sig_does_not_verify() {
Alan Stokes25c86212023-03-09 17:22:19 +0000159 setup();
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000160 let path_list = [
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000161 "tests/data/v3-only-with-ecdsa-sha512-p521-sig-does-not-verify.apk",
162 "tests/data/v3-only-with-rsa-pkcs1-sha256-3072-sig-does-not-verify.apk",
163 ];
164 for path in path_list.iter() {
Alan Stokes25f69362023-03-06 16:51:54 +0000165 let res = verify(path, SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000166 assert!(res.is_err());
Alice Wang50701022022-09-21 08:51:38 +0000167 assert_contains(&res.unwrap_err().to_string(), "Signature is invalid");
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000168 }
169}
170
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000171#[test]
172fn test_verify_v3_digest_mismatch() {
Alan Stokes25c86212023-03-09 17:22:19 +0000173 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000174 let res = verify("tests/data/v3-only-with-rsa-pkcs1-sha512-8192-digest-mismatch.apk", SDK_INT);
Alice Wang50701022022-09-21 08:51:38 +0000175 assert!(res.is_err());
176 assert_contains(&res.unwrap_err().to_string(), "Digest mismatch");
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000177}
178
179#[test]
180fn test_verify_v3_wrong_apk_sig_block_magic() {
Alan Stokes25c86212023-03-09 17:22:19 +0000181 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000182 let res =
183 verify("tests/data/v3-only-with-ecdsa-sha512-p384-wrong-apk-sig-block-magic.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000184 assert!(res.is_err());
185 assert_contains(&res.unwrap_err().to_string(), "No APK Signing Block");
186}
187
188#[test]
189fn test_verify_v3_apk_sig_block_size_mismatch() {
Alan Stokes25c86212023-03-09 17:22:19 +0000190 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000191 let res = verify(
192 "tests/data/v3-only-with-rsa-pkcs1-sha512-4096-apk-sig-block-size-mismatch.apk",
193 SDK_INT,
194 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000195 assert!(res.is_err());
196 assert_contains(
197 &res.unwrap_err().to_string(),
198 "APK Signing Block sizes in header and footer do not match",
199 );
200}
201
202#[test]
203fn test_verify_v3_cert_and_public_key_mismatch() {
Alan Stokes25c86212023-03-09 17:22:19 +0000204 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000205 let res = verify("tests/data/v3-only-cert-and-public-key-mismatch.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000206 assert!(res.is_err());
207 assert_contains(&res.unwrap_err().to_string(), "Public key mismatch");
208}
209
210#[test]
211fn test_verify_v3_empty() {
Alan Stokes25c86212023-03-09 17:22:19 +0000212 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000213 let res = verify("tests/data/v3-only-empty.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000214 assert!(res.is_err());
215 assert_contains(&res.unwrap_err().to_string(), "APK too small for APK Signing Block");
216}
217
218#[test]
219fn test_verify_v3_no_certs_in_sig() {
Alan Stokes25c86212023-03-09 17:22:19 +0000220 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000221 let res = verify("tests/data/v3-only-no-certs-in-sig.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000222 assert!(res.is_err());
223 assert_contains(&res.unwrap_err().to_string(), "No certificates listed");
224}
225
226#[test]
227fn test_verify_v3_no_supported_sig_algs() {
Alan Stokes25c86212023-03-09 17:22:19 +0000228 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000229 let res = verify("tests/data/v3-only-no-supported-sig-algs.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000230 assert!(res.is_err());
Alan Stokesa6876992023-01-20 12:26:25 +0000231 assert_contains(&res.unwrap_err().to_string(), "No supported APK signatures found");
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000232}
233
234#[test]
235fn test_verify_v3_signatures_and_digests_block_mismatch() {
Alan Stokes25c86212023-03-09 17:22:19 +0000236 setup();
Alan Stokes25f69362023-03-06 16:51:54 +0000237 let res = verify("tests/data/v3-only-signatures-and-digests-block-mismatch.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000238 assert!(res.is_err());
239 assert_contains(
240 &res.unwrap_err().to_string(),
241 "Signature algorithms don't match between digests and signatures records",
242 );
243}
244
245#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000246fn apk_signed_with_v3_unknown_additional_attr_is_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +0000247 setup();
Alice Wang676bb4a2022-09-19 14:21:39 +0000248 validate_apk(
249 "tests/data/v3-only-unknown-additional-attr.apk",
250 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
251 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000252}
253
254#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000255fn apk_signed_with_v3_unknown_pair_in_apk_sig_block_is_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +0000256 setup();
Alice Wang676bb4a2022-09-19 14:21:39 +0000257 validate_apk(
258 "tests/data/v3-only-unknown-pair-in-apk-sig-block.apk",
259 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
260 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000261}
262
263#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000264fn apk_signed_with_v3_ignorable_unsupported_sig_algs_is_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +0000265 setup();
Alice Wang676bb4a2022-09-19 14:21:39 +0000266 validate_apk(
267 "tests/data/v3-only-with-ignorable-unsupported-sig-algs.apk",
268 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
269 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000270}
271
272#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000273fn apk_signed_with_v3_stamp_is_valid() {
Alan Stokes25c86212023-03-09 17:22:19 +0000274 setup();
Alice Wang676bb4a2022-09-19 14:21:39 +0000275 validate_apk("tests/data/v3-only-with-stamp.apk", SignatureAlgorithmID::EcdsaWithSha256);
Alice Wang67d3c002022-09-16 10:08:25 +0000276}
277
Alice Wang676bb4a2022-09-19 14:21:39 +0000278fn validate_apk<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) {
279 validate_apk_public_key(&apk_path);
280 validate_apk_digest(&apk_path, expected_algorithm_id);
281}
282
283/// Validates that the following public keys are equal:
284/// * public key from verification
285/// * public key extracted from apk without verification
286/// * expected public key from the corresponding .der file
Alice Wang67d3c002022-09-16 10:08:25 +0000287fn validate_apk_public_key<P: AsRef<Path>>(apk_path: P) {
Alan Stokes25f69362023-03-06 16:51:54 +0000288 let public_key_from_verification = verify(&apk_path, SDK_INT);
Alice Wang67d3c002022-09-16 10:08:25 +0000289 let public_key_from_verification =
290 public_key_from_verification.expect("Error in verification result");
291
292 let expected_public_key_path = format!("{}.der", apk_path.as_ref().to_str().unwrap());
Alice Wang676bb4a2022-09-19 14:21:39 +0000293 assert_bytes_eq_to_data_in_file(&public_key_from_verification, expected_public_key_path);
Alice Wang67d3c002022-09-16 10:08:25 +0000294
Alan Stokes25f69362023-03-06 16:51:54 +0000295 let public_key_from_apk = get_public_key_der(&apk_path, SDK_INT);
Alice Wang3c016622022-09-19 09:08:27 +0000296 let public_key_from_apk =
297 public_key_from_apk.expect("Error when extracting public key from apk");
Alice Wang676bb4a2022-09-19 14:21:39 +0000298 assert_eq!(
299 public_key_from_verification, public_key_from_apk,
300 "Public key extracted directly from apk does not match the public key from verification."
301 );
302}
303
304/// Validates that the following apk_digest are equal:
305/// * apk_digest directly extracted from apk without computation
Alice Wang0cafa142022-09-23 15:17:02 +0000306/// * computed apk_digest
Alice Wang676bb4a2022-09-19 14:21:39 +0000307/// * expected apk digest from the corresponding .apk_digest file
308fn validate_apk_digest<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) {
309 let apk = fs::File::open(&apk_path).expect("Unabled to open apk file");
310
Alan Stokes25f69362023-03-06 16:51:54 +0000311 let (verified_algorithm_id, verified_digest) =
Alan Stokes068f6d42023-10-09 10:13:03 +0100312 get_apk_digest(&apk, SDK_INT, /* verify= */ true)
Alan Stokes25f69362023-03-06 16:51:54 +0000313 .expect("Error when extracting apk digest with verification.");
Alice Wang676bb4a2022-09-19 14:21:39 +0000314
Alice Wang0cafa142022-09-23 15:17:02 +0000315 assert_eq!(expected_algorithm_id, verified_algorithm_id);
Alice Wang676bb4a2022-09-19 14:21:39 +0000316 let expected_digest_path = format!("{}.apk_digest", apk_path.as_ref().to_str().unwrap());
Alice Wang0cafa142022-09-23 15:17:02 +0000317 assert_bytes_eq_to_data_in_file(&verified_digest, expected_digest_path);
318
Alan Stokes25f69362023-03-06 16:51:54 +0000319 let (unverified_algorithm_id, unverified_digest) =
Alan Stokes068f6d42023-10-09 10:13:03 +0100320 get_apk_digest(&apk, SDK_INT, /* verify= */ false)
Alan Stokes25f69362023-03-06 16:51:54 +0000321 .expect("Error when extracting apk digest without verification.");
Alice Wang0cafa142022-09-23 15:17:02 +0000322 assert_eq!(expected_algorithm_id, unverified_algorithm_id);
323 assert_eq!(verified_digest, unverified_digest);
Alice Wang676bb4a2022-09-19 14:21:39 +0000324}
325
326fn assert_bytes_eq_to_data_in_file<P: AsRef<Path> + std::fmt::Display>(
327 bytes_data: &[u8],
328 expected_data_path: P,
329) {
330 assert!(
331 fs::metadata(&expected_data_path).is_ok(),
332 "File does not exist. You can re-create it with:\n$ echo -en {} > {}\n",
333 bytes_data.iter().map(|b| format!("\\\\x{:02x}", b)).collect::<String>(),
334 expected_data_path
335 );
336 let expected_data = fs::read(&expected_data_path).unwrap();
337 assert_eq!(
338 expected_data, bytes_data,
339 "Actual data does not match the data from: {}",
340 expected_data_path
341 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000342}