blob: f08b3572247d1c9a38e3bcfd8c98d531b603fceb [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};
Alice Wang67d3c002022-09-16 10:08:25 +000020use std::{fs, matches, path::Path};
Jooyung Hand8397852021-08-10 16:29:36 +090021
Seungjae Yoo91e250a2022-06-07 02:21:56 +000022const KEY_NAMES_DSA: &[&str] = &["1024", "2048", "3072"];
23const KEY_NAMES_ECDSA: &[&str] = &["p256", "p384", "p521"];
24const KEY_NAMES_RSA: &[&str] = &["1024", "2048", "3072", "4096", "8192", "16384"];
Jooyung Hancee6de62021-08-11 15:52:07 +090025
Alan Stokes25f69362023-03-06 16:51:54 +000026const SDK_INT: u32 = 31;
27
Jooyung Hancee6de62021-08-11 15:52:07 +090028#[test]
29fn test_verify_truncated_cd() {
30 use zip::result::ZipError;
Alan Stokes25f69362023-03-06 16:51:54 +000031 let res = verify("tests/data/v2-only-truncated-cd.apk", SDK_INT);
Alice Wang92889352022-09-16 10:42:52 +000032 // TODO(b/190343842): consider making a helper for err assertion
Jooyung Hancee6de62021-08-11 15:52:07 +090033 assert!(matches!(
Andrew Walbran117cd5e2021-08-13 11:42:13 +000034 res.unwrap_err().root_cause().downcast_ref::<ZipError>().unwrap(),
Jooyung Hancee6de62021-08-11 15:52:07 +090035 ZipError::InvalidArchive(_),
36 ));
37}
Seungjae Yoo91e250a2022-06-07 02:21:56 +000038
39#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000040fn apex_signed_with_v3_rsa_pkcs1_sha512_is_valid() {
41 validate_apk("tests/data/test.apex", SignatureAlgorithmID::RsaPkcs1V15WithSha512);
Seungjae Yoo91e250a2022-06-07 02:21:56 +000042}
43
Seungjae Yoo91e250a2022-06-07 02:21:56 +000044#[test]
Alice Wang50701022022-09-21 08:51:38 +000045fn apks_signed_with_v3_dsa_sha256_are_not_supported() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000046 for key_name in KEY_NAMES_DSA.iter() {
Alan Stokes25f69362023-03-06 16:51:54 +000047 let res = verify(format!("tests/data/v3-only-with-dsa-sha256-{}.apk", key_name), SDK_INT);
Alice Wang50701022022-09-21 08:51:38 +000048 assert!(res.is_err(), "DSA algorithm is not supported for verification. See b/197052981.");
Alan Stokesa6876992023-01-20 12:26:25 +000049 assert_contains(&res.unwrap_err().to_string(), "No supported APK signatures found");
Alice Wang676bb4a2022-09-19 14:21:39 +000050 }
51}
52
53#[test]
54fn apks_signed_with_v3_ecdsa_sha256_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000055 for key_name in KEY_NAMES_ECDSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000056 validate_apk(
57 format!("tests/data/v3-only-with-ecdsa-sha256-{}.apk", key_name),
58 SignatureAlgorithmID::EcdsaWithSha256,
59 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000060 }
61}
62
Seungjae Yoo91e250a2022-06-07 02:21:56 +000063#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000064fn apks_signed_with_v3_ecdsa_sha512_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000065 for key_name in KEY_NAMES_ECDSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000066 validate_apk(
67 format!("tests/data/v3-only-with-ecdsa-sha512-{}.apk", key_name),
68 SignatureAlgorithmID::EcdsaWithSha512,
69 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000070 }
71}
72
73#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000074fn apks_signed_with_v3_rsa_pkcs1_sha256_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000075 for key_name in KEY_NAMES_RSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000076 validate_apk(
77 format!("tests/data/v3-only-with-rsa-pkcs1-sha256-{}.apk", key_name),
78 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
79 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000080 }
81}
82
83#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000084fn apks_signed_with_v3_rsa_pkcs1_sha512_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000085 for key_name in KEY_NAMES_RSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000086 validate_apk(
87 format!("tests/data/v3-only-with-rsa-pkcs1-sha512-{}.apk", key_name),
88 SignatureAlgorithmID::RsaPkcs1V15WithSha512,
89 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000090 }
91}
92
Seungjae Yoo91e250a2022-06-07 02:21:56 +000093#[test]
94fn test_verify_v3_sig_does_not_verify() {
95 let path_list = [
Seungjae Yoo91e250a2022-06-07 02:21:56 +000096 "tests/data/v3-only-with-ecdsa-sha512-p521-sig-does-not-verify.apk",
97 "tests/data/v3-only-with-rsa-pkcs1-sha256-3072-sig-does-not-verify.apk",
98 ];
99 for path in path_list.iter() {
Alan Stokes25f69362023-03-06 16:51:54 +0000100 let res = verify(path, SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000101 assert!(res.is_err());
Alice Wang50701022022-09-21 08:51:38 +0000102 assert_contains(&res.unwrap_err().to_string(), "Signature is invalid");
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000103 }
104}
105
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000106#[test]
107fn test_verify_v3_digest_mismatch() {
Alan Stokes25f69362023-03-06 16:51:54 +0000108 let res = verify("tests/data/v3-only-with-rsa-pkcs1-sha512-8192-digest-mismatch.apk", SDK_INT);
Alice Wang50701022022-09-21 08:51:38 +0000109 assert!(res.is_err());
110 assert_contains(&res.unwrap_err().to_string(), "Digest mismatch");
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000111}
112
113#[test]
114fn test_verify_v3_wrong_apk_sig_block_magic() {
Alan Stokes25f69362023-03-06 16:51:54 +0000115 let res =
116 verify("tests/data/v3-only-with-ecdsa-sha512-p384-wrong-apk-sig-block-magic.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000117 assert!(res.is_err());
118 assert_contains(&res.unwrap_err().to_string(), "No APK Signing Block");
119}
120
121#[test]
122fn test_verify_v3_apk_sig_block_size_mismatch() {
Alan Stokes25f69362023-03-06 16:51:54 +0000123 let res = verify(
124 "tests/data/v3-only-with-rsa-pkcs1-sha512-4096-apk-sig-block-size-mismatch.apk",
125 SDK_INT,
126 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000127 assert!(res.is_err());
128 assert_contains(
129 &res.unwrap_err().to_string(),
130 "APK Signing Block sizes in header and footer do not match",
131 );
132}
133
134#[test]
135fn test_verify_v3_cert_and_public_key_mismatch() {
Alan Stokes25f69362023-03-06 16:51:54 +0000136 let res = verify("tests/data/v3-only-cert-and-public-key-mismatch.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000137 assert!(res.is_err());
138 assert_contains(&res.unwrap_err().to_string(), "Public key mismatch");
139}
140
141#[test]
142fn test_verify_v3_empty() {
Alan Stokes25f69362023-03-06 16:51:54 +0000143 let res = verify("tests/data/v3-only-empty.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000144 assert!(res.is_err());
145 assert_contains(&res.unwrap_err().to_string(), "APK too small for APK Signing Block");
146}
147
148#[test]
149fn test_verify_v3_no_certs_in_sig() {
Alan Stokes25f69362023-03-06 16:51:54 +0000150 let res = verify("tests/data/v3-only-no-certs-in-sig.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000151 assert!(res.is_err());
152 assert_contains(&res.unwrap_err().to_string(), "No certificates listed");
153}
154
155#[test]
156fn test_verify_v3_no_supported_sig_algs() {
Alan Stokes25f69362023-03-06 16:51:54 +0000157 let res = verify("tests/data/v3-only-no-supported-sig-algs.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000158 assert!(res.is_err());
Alan Stokesa6876992023-01-20 12:26:25 +0000159 assert_contains(&res.unwrap_err().to_string(), "No supported APK signatures found");
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000160}
161
162#[test]
163fn test_verify_v3_signatures_and_digests_block_mismatch() {
Alan Stokes25f69362023-03-06 16:51:54 +0000164 let res = verify("tests/data/v3-only-signatures-and-digests-block-mismatch.apk", SDK_INT);
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000165 assert!(res.is_err());
166 assert_contains(
167 &res.unwrap_err().to_string(),
168 "Signature algorithms don't match between digests and signatures records",
169 );
170}
171
172#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000173fn apk_signed_with_v3_unknown_additional_attr_is_valid() {
174 validate_apk(
175 "tests/data/v3-only-unknown-additional-attr.apk",
176 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
177 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000178}
179
180#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000181fn apk_signed_with_v3_unknown_pair_in_apk_sig_block_is_valid() {
182 validate_apk(
183 "tests/data/v3-only-unknown-pair-in-apk-sig-block.apk",
184 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
185 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000186}
187
188#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000189fn apk_signed_with_v3_ignorable_unsupported_sig_algs_is_valid() {
190 validate_apk(
191 "tests/data/v3-only-with-ignorable-unsupported-sig-algs.apk",
192 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
193 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000194}
195
196#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000197fn apk_signed_with_v3_stamp_is_valid() {
198 validate_apk("tests/data/v3-only-with-stamp.apk", SignatureAlgorithmID::EcdsaWithSha256);
Alice Wang67d3c002022-09-16 10:08:25 +0000199}
200
Alice Wang676bb4a2022-09-19 14:21:39 +0000201fn validate_apk<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) {
202 validate_apk_public_key(&apk_path);
203 validate_apk_digest(&apk_path, expected_algorithm_id);
204}
205
206/// Validates that the following public keys are equal:
207/// * public key from verification
208/// * public key extracted from apk without verification
209/// * expected public key from the corresponding .der file
Alice Wang67d3c002022-09-16 10:08:25 +0000210fn validate_apk_public_key<P: AsRef<Path>>(apk_path: P) {
Alan Stokes25f69362023-03-06 16:51:54 +0000211 let public_key_from_verification = verify(&apk_path, SDK_INT);
Alice Wang67d3c002022-09-16 10:08:25 +0000212 let public_key_from_verification =
213 public_key_from_verification.expect("Error in verification result");
214
215 let expected_public_key_path = format!("{}.der", apk_path.as_ref().to_str().unwrap());
Alice Wang676bb4a2022-09-19 14:21:39 +0000216 assert_bytes_eq_to_data_in_file(&public_key_from_verification, expected_public_key_path);
Alice Wang67d3c002022-09-16 10:08:25 +0000217
Alan Stokes25f69362023-03-06 16:51:54 +0000218 let public_key_from_apk = get_public_key_der(&apk_path, SDK_INT);
Alice Wang3c016622022-09-19 09:08:27 +0000219 let public_key_from_apk =
220 public_key_from_apk.expect("Error when extracting public key from apk");
Alice Wang676bb4a2022-09-19 14:21:39 +0000221 assert_eq!(
222 public_key_from_verification, public_key_from_apk,
223 "Public key extracted directly from apk does not match the public key from verification."
224 );
225}
226
227/// Validates that the following apk_digest are equal:
228/// * apk_digest directly extracted from apk without computation
Alice Wang0cafa142022-09-23 15:17:02 +0000229/// * computed apk_digest
Alice Wang676bb4a2022-09-19 14:21:39 +0000230/// * expected apk digest from the corresponding .apk_digest file
231fn validate_apk_digest<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) {
232 let apk = fs::File::open(&apk_path).expect("Unabled to open apk file");
233
Alan Stokes25f69362023-03-06 16:51:54 +0000234 let (verified_algorithm_id, verified_digest) =
235 get_apk_digest(&apk, SDK_INT, /*verify=*/ true)
236 .expect("Error when extracting apk digest with verification.");
Alice Wang676bb4a2022-09-19 14:21:39 +0000237
Alice Wang0cafa142022-09-23 15:17:02 +0000238 assert_eq!(expected_algorithm_id, verified_algorithm_id);
Alice Wang676bb4a2022-09-19 14:21:39 +0000239 let expected_digest_path = format!("{}.apk_digest", apk_path.as_ref().to_str().unwrap());
Alice Wang0cafa142022-09-23 15:17:02 +0000240 assert_bytes_eq_to_data_in_file(&verified_digest, expected_digest_path);
241
Alan Stokes25f69362023-03-06 16:51:54 +0000242 let (unverified_algorithm_id, unverified_digest) =
243 get_apk_digest(&apk, SDK_INT, /*verify=*/ false)
244 .expect("Error when extracting apk digest without verification.");
Alice Wang0cafa142022-09-23 15:17:02 +0000245 assert_eq!(expected_algorithm_id, unverified_algorithm_id);
246 assert_eq!(verified_digest, unverified_digest);
Alice Wang676bb4a2022-09-19 14:21:39 +0000247}
248
249fn assert_bytes_eq_to_data_in_file<P: AsRef<Path> + std::fmt::Display>(
250 bytes_data: &[u8],
251 expected_data_path: P,
252) {
253 assert!(
254 fs::metadata(&expected_data_path).is_ok(),
255 "File does not exist. You can re-create it with:\n$ echo -en {} > {}\n",
256 bytes_data.iter().map(|b| format!("\\\\x{:02x}", b)).collect::<String>(),
257 expected_data_path
258 );
259 let expected_data = fs::read(&expected_data_path).unwrap();
260 assert_eq!(
261 expected_data, bytes_data,
262 "Actual data does not match the data from: {}",
263 expected_data_path
264 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000265}