blob: f2018a140aa223b859692f0157e8dea5b3e6bb86 [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::{
18 get_public_key_der, pick_v4_apk_digest, testing::assert_contains, verify, SignatureAlgorithmID,
19};
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
26#[test]
27fn test_verify_truncated_cd() {
28 use zip::result::ZipError;
29 let res = verify("tests/data/v2-only-truncated-cd.apk");
Alice Wang92889352022-09-16 10:42:52 +000030 // TODO(b/190343842): consider making a helper for err assertion
Jooyung Hancee6de62021-08-11 15:52:07 +090031 assert!(matches!(
Andrew Walbran117cd5e2021-08-13 11:42:13 +000032 res.unwrap_err().root_cause().downcast_ref::<ZipError>().unwrap(),
Jooyung Hancee6de62021-08-11 15:52:07 +090033 ZipError::InvalidArchive(_),
34 ));
35}
Seungjae Yoo91e250a2022-06-07 02:21:56 +000036
37#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000038fn apex_signed_with_v3_rsa_pkcs1_sha512_is_valid() {
39 validate_apk("tests/data/test.apex", SignatureAlgorithmID::RsaPkcs1V15WithSha512);
Seungjae Yoo91e250a2022-06-07 02:21:56 +000040}
41
Seungjae Yoo91e250a2022-06-07 02:21:56 +000042#[test]
Alice Wangc68b95b2022-09-21 08:51:38 +000043fn apks_signed_with_v3_dsa_sha256_are_not_supported() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000044 for key_name in KEY_NAMES_DSA.iter() {
45 let res = verify(format!("tests/data/v3-only-with-dsa-sha256-{}.apk", key_name));
Alice Wangc68b95b2022-09-21 08:51:38 +000046 assert!(res.is_err(), "DSA algorithm is not supported for verification. See b/197052981.");
47 assert_contains(&res.unwrap_err().to_string(), "No supported signatures found");
Alice Wang676bb4a2022-09-19 14:21:39 +000048 }
49}
50
51#[test]
52fn apks_signed_with_v3_ecdsa_sha256_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000053 for key_name in KEY_NAMES_ECDSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000054 validate_apk(
55 format!("tests/data/v3-only-with-ecdsa-sha256-{}.apk", key_name),
56 SignatureAlgorithmID::EcdsaWithSha256,
57 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000058 }
59}
60
Seungjae Yoo91e250a2022-06-07 02:21:56 +000061#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000062fn apks_signed_with_v3_ecdsa_sha512_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000063 for key_name in KEY_NAMES_ECDSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000064 validate_apk(
65 format!("tests/data/v3-only-with-ecdsa-sha512-{}.apk", key_name),
66 SignatureAlgorithmID::EcdsaWithSha512,
67 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000068 }
69}
70
71#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000072fn apks_signed_with_v3_rsa_pkcs1_sha256_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000073 for key_name in KEY_NAMES_RSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000074 validate_apk(
75 format!("tests/data/v3-only-with-rsa-pkcs1-sha256-{}.apk", key_name),
76 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
77 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000078 }
79}
80
81#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +000082fn apks_signed_with_v3_rsa_pkcs1_sha512_are_valid() {
Seungjae Yoo91e250a2022-06-07 02:21:56 +000083 for key_name in KEY_NAMES_RSA.iter() {
Alice Wang676bb4a2022-09-19 14:21:39 +000084 validate_apk(
85 format!("tests/data/v3-only-with-rsa-pkcs1-sha512-{}.apk", key_name),
86 SignatureAlgorithmID::RsaPkcs1V15WithSha512,
87 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +000088 }
89}
90
Seungjae Yoo91e250a2022-06-07 02:21:56 +000091#[test]
92fn test_verify_v3_sig_does_not_verify() {
93 let path_list = [
Seungjae Yoo91e250a2022-06-07 02:21:56 +000094 "tests/data/v3-only-with-ecdsa-sha512-p521-sig-does-not-verify.apk",
95 "tests/data/v3-only-with-rsa-pkcs1-sha256-3072-sig-does-not-verify.apk",
96 ];
97 for path in path_list.iter() {
98 let res = verify(path);
99 assert!(res.is_err());
100 let error_msg = &res.unwrap_err().to_string();
101 assert!(
Alice Wanga66b5c02022-09-16 07:25:17 +0000102 error_msg.contains("Signature is invalid") || error_msg.contains("not implemented")
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000103 );
104 }
105}
106
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000107#[test]
108fn test_verify_v3_digest_mismatch() {
Alice Wangc68b95b2022-09-21 08:51:38 +0000109 let res = verify("tests/data/v3-only-with-rsa-pkcs1-sha512-8192-digest-mismatch.apk");
110 assert!(res.is_err());
111 assert_contains(&res.unwrap_err().to_string(), "Digest mismatch");
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000112}
113
114#[test]
115fn test_verify_v3_wrong_apk_sig_block_magic() {
116 let res = verify("tests/data/v3-only-with-ecdsa-sha512-p384-wrong-apk-sig-block-magic.apk");
117 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() {
123 let res =
124 verify("tests/data/v3-only-with-rsa-pkcs1-sha512-4096-apk-sig-block-size-mismatch.apk");
125 assert!(res.is_err());
126 assert_contains(
127 &res.unwrap_err().to_string(),
128 "APK Signing Block sizes in header and footer do not match",
129 );
130}
131
132#[test]
133fn test_verify_v3_cert_and_public_key_mismatch() {
134 let res = verify("tests/data/v3-only-cert-and-public-key-mismatch.apk");
135 assert!(res.is_err());
136 assert_contains(&res.unwrap_err().to_string(), "Public key mismatch");
137}
138
139#[test]
140fn test_verify_v3_empty() {
141 let res = verify("tests/data/v3-only-empty.apk");
142 assert!(res.is_err());
143 assert_contains(&res.unwrap_err().to_string(), "APK too small for APK Signing Block");
144}
145
146#[test]
147fn test_verify_v3_no_certs_in_sig() {
148 let res = verify("tests/data/v3-only-no-certs-in-sig.apk");
149 assert!(res.is_err());
150 assert_contains(&res.unwrap_err().to_string(), "No certificates listed");
151}
152
153#[test]
154fn test_verify_v3_no_supported_sig_algs() {
155 let res = verify("tests/data/v3-only-no-supported-sig-algs.apk");
156 assert!(res.is_err());
157 assert_contains(&res.unwrap_err().to_string(), "No supported signatures found");
158}
159
160#[test]
161fn test_verify_v3_signatures_and_digests_block_mismatch() {
162 let res = verify("tests/data/v3-only-signatures-and-digests-block-mismatch.apk");
163 assert!(res.is_err());
164 assert_contains(
165 &res.unwrap_err().to_string(),
166 "Signature algorithms don't match between digests and signatures records",
167 );
168}
169
170#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000171fn apk_signed_with_v3_unknown_additional_attr_is_valid() {
172 validate_apk(
173 "tests/data/v3-only-unknown-additional-attr.apk",
174 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
175 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000176}
177
178#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000179fn apk_signed_with_v3_unknown_pair_in_apk_sig_block_is_valid() {
180 validate_apk(
181 "tests/data/v3-only-unknown-pair-in-apk-sig-block.apk",
182 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
183 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000184}
185
186#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000187fn apk_signed_with_v3_ignorable_unsupported_sig_algs_is_valid() {
188 validate_apk(
189 "tests/data/v3-only-with-ignorable-unsupported-sig-algs.apk",
190 SignatureAlgorithmID::RsaPkcs1V15WithSha256,
191 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000192}
193
194#[test]
Alice Wang676bb4a2022-09-19 14:21:39 +0000195fn apk_signed_with_v3_stamp_is_valid() {
196 validate_apk("tests/data/v3-only-with-stamp.apk", SignatureAlgorithmID::EcdsaWithSha256);
Alice Wang67d3c002022-09-16 10:08:25 +0000197}
198
Alice Wang676bb4a2022-09-19 14:21:39 +0000199fn validate_apk<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) {
200 validate_apk_public_key(&apk_path);
201 validate_apk_digest(&apk_path, expected_algorithm_id);
202}
203
204/// Validates that the following public keys are equal:
205/// * public key from verification
206/// * public key extracted from apk without verification
207/// * expected public key from the corresponding .der file
Alice Wang67d3c002022-09-16 10:08:25 +0000208fn validate_apk_public_key<P: AsRef<Path>>(apk_path: P) {
Alice Wang676bb4a2022-09-19 14:21:39 +0000209 let public_key_from_verification = verify(&apk_path);
Alice Wang67d3c002022-09-16 10:08:25 +0000210 let public_key_from_verification =
211 public_key_from_verification.expect("Error in verification result");
212
213 let expected_public_key_path = format!("{}.der", apk_path.as_ref().to_str().unwrap());
Alice Wang676bb4a2022-09-19 14:21:39 +0000214 assert_bytes_eq_to_data_in_file(&public_key_from_verification, expected_public_key_path);
Alice Wang67d3c002022-09-16 10:08:25 +0000215
Alice Wang676bb4a2022-09-19 14:21:39 +0000216 let public_key_from_apk = get_public_key_der(&apk_path);
Alice Wang3c016622022-09-19 09:08:27 +0000217 let public_key_from_apk =
218 public_key_from_apk.expect("Error when extracting public key from apk");
Alice Wang676bb4a2022-09-19 14:21:39 +0000219 assert_eq!(
220 public_key_from_verification, public_key_from_apk,
221 "Public key extracted directly from apk does not match the public key from verification."
222 );
223}
224
225/// Validates that the following apk_digest are equal:
226/// * apk_digest directly extracted from apk without computation
227/// * expected apk digest from the corresponding .apk_digest file
228fn validate_apk_digest<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) {
229 let apk = fs::File::open(&apk_path).expect("Unabled to open apk file");
230
231 let (signature_algorithm_id, digest_from_apk) =
232 pick_v4_apk_digest(apk).expect("Error when extracting apk digest.");
233
234 assert_eq!(expected_algorithm_id, signature_algorithm_id);
235 let expected_digest_path = format!("{}.apk_digest", apk_path.as_ref().to_str().unwrap());
236 assert_bytes_eq_to_data_in_file(&digest_from_apk, expected_digest_path);
237}
238
239fn assert_bytes_eq_to_data_in_file<P: AsRef<Path> + std::fmt::Display>(
240 bytes_data: &[u8],
241 expected_data_path: P,
242) {
243 assert!(
244 fs::metadata(&expected_data_path).is_ok(),
245 "File does not exist. You can re-create it with:\n$ echo -en {} > {}\n",
246 bytes_data.iter().map(|b| format!("\\\\x{:02x}", b)).collect::<String>(),
247 expected_data_path
248 );
249 let expected_data = fs::read(&expected_data_path).unwrap();
250 assert_eq!(
251 expected_data, bytes_data,
252 "Actual data does not match the data from: {}",
253 expected_data_path
254 );
Seungjae Yoo91e250a2022-06-07 02:21:56 +0000255}