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 | |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 17 | use apkverify::{ |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 18 | get_apk_digest, get_public_key_der, testing::assert_contains, verify, SignatureAlgorithmID, |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 19 | }; |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 20 | use std::{fs, matches, path::Path}; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 21 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 22 | const KEY_NAMES_DSA: &[&str] = &["1024", "2048", "3072"]; |
| 23 | const KEY_NAMES_ECDSA: &[&str] = &["p256", "p384", "p521"]; |
| 24 | const KEY_NAMES_RSA: &[&str] = &["1024", "2048", "3072", "4096", "8192", "16384"]; |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 25 | |
| 26 | #[test] |
| 27 | fn test_verify_truncated_cd() { |
| 28 | use zip::result::ZipError; |
| 29 | let res = verify("tests/data/v2-only-truncated-cd.apk"); |
Alice Wang | 9288935 | 2022-09-16 10:42:52 +0000 | [diff] [blame] | 30 | // TODO(b/190343842): consider making a helper for err assertion |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 31 | assert!(matches!( |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 32 | res.unwrap_err().root_cause().downcast_ref::<ZipError>().unwrap(), |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 33 | ZipError::InvalidArchive(_), |
| 34 | )); |
| 35 | } |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 36 | |
| 37 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 38 | fn apex_signed_with_v3_rsa_pkcs1_sha512_is_valid() { |
| 39 | validate_apk("tests/data/test.apex", SignatureAlgorithmID::RsaPkcs1V15WithSha512); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 42 | #[test] |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 43 | fn apks_signed_with_v3_dsa_sha256_are_not_supported() { |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 44 | for key_name in KEY_NAMES_DSA.iter() { |
| 45 | let res = verify(format!("tests/data/v3-only-with-dsa-sha256-{}.apk", key_name)); |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 46 | 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 Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn apks_signed_with_v3_ecdsa_sha256_are_valid() { |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 53 | for key_name in KEY_NAMES_ECDSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 54 | validate_apk( |
| 55 | format!("tests/data/v3-only-with-ecdsa-sha256-{}.apk", key_name), |
| 56 | SignatureAlgorithmID::EcdsaWithSha256, |
| 57 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 61 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 62 | fn apks_signed_with_v3_ecdsa_sha512_are_valid() { |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 63 | for key_name in KEY_NAMES_ECDSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 64 | validate_apk( |
| 65 | format!("tests/data/v3-only-with-ecdsa-sha512-{}.apk", key_name), |
| 66 | SignatureAlgorithmID::EcdsaWithSha512, |
| 67 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 72 | fn apks_signed_with_v3_rsa_pkcs1_sha256_are_valid() { |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 73 | for key_name in KEY_NAMES_RSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 74 | validate_apk( |
| 75 | format!("tests/data/v3-only-with-rsa-pkcs1-sha256-{}.apk", key_name), |
| 76 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 77 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 82 | fn apks_signed_with_v3_rsa_pkcs1_sha512_are_valid() { |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 83 | for key_name in KEY_NAMES_RSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 84 | validate_apk( |
| 85 | format!("tests/data/v3-only-with-rsa-pkcs1-sha512-{}.apk", key_name), |
| 86 | SignatureAlgorithmID::RsaPkcs1V15WithSha512, |
| 87 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 91 | #[test] |
| 92 | fn test_verify_v3_sig_does_not_verify() { |
| 93 | let path_list = [ |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 94 | "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()); |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 100 | assert_contains(&res.unwrap_err().to_string(), "Signature is invalid"); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 104 | #[test] |
| 105 | fn test_verify_v3_digest_mismatch() { |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 106 | let res = verify("tests/data/v3-only-with-rsa-pkcs1-sha512-8192-digest-mismatch.apk"); |
| 107 | assert!(res.is_err()); |
| 108 | assert_contains(&res.unwrap_err().to_string(), "Digest mismatch"); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn test_verify_v3_wrong_apk_sig_block_magic() { |
| 113 | let res = verify("tests/data/v3-only-with-ecdsa-sha512-p384-wrong-apk-sig-block-magic.apk"); |
| 114 | assert!(res.is_err()); |
| 115 | assert_contains(&res.unwrap_err().to_string(), "No APK Signing Block"); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn test_verify_v3_apk_sig_block_size_mismatch() { |
| 120 | let res = |
| 121 | verify("tests/data/v3-only-with-rsa-pkcs1-sha512-4096-apk-sig-block-size-mismatch.apk"); |
| 122 | assert!(res.is_err()); |
| 123 | assert_contains( |
| 124 | &res.unwrap_err().to_string(), |
| 125 | "APK Signing Block sizes in header and footer do not match", |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn test_verify_v3_cert_and_public_key_mismatch() { |
| 131 | let res = verify("tests/data/v3-only-cert-and-public-key-mismatch.apk"); |
| 132 | assert!(res.is_err()); |
| 133 | assert_contains(&res.unwrap_err().to_string(), "Public key mismatch"); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn test_verify_v3_empty() { |
| 138 | let res = verify("tests/data/v3-only-empty.apk"); |
| 139 | assert!(res.is_err()); |
| 140 | assert_contains(&res.unwrap_err().to_string(), "APK too small for APK Signing Block"); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn test_verify_v3_no_certs_in_sig() { |
| 145 | let res = verify("tests/data/v3-only-no-certs-in-sig.apk"); |
| 146 | assert!(res.is_err()); |
| 147 | assert_contains(&res.unwrap_err().to_string(), "No certificates listed"); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn test_verify_v3_no_supported_sig_algs() { |
| 152 | let res = verify("tests/data/v3-only-no-supported-sig-algs.apk"); |
| 153 | assert!(res.is_err()); |
| 154 | assert_contains(&res.unwrap_err().to_string(), "No supported signatures found"); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn test_verify_v3_signatures_and_digests_block_mismatch() { |
| 159 | let res = verify("tests/data/v3-only-signatures-and-digests-block-mismatch.apk"); |
| 160 | assert!(res.is_err()); |
| 161 | assert_contains( |
| 162 | &res.unwrap_err().to_string(), |
| 163 | "Signature algorithms don't match between digests and signatures records", |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 168 | fn apk_signed_with_v3_unknown_additional_attr_is_valid() { |
| 169 | validate_apk( |
| 170 | "tests/data/v3-only-unknown-additional-attr.apk", |
| 171 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 172 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 176 | fn apk_signed_with_v3_unknown_pair_in_apk_sig_block_is_valid() { |
| 177 | validate_apk( |
| 178 | "tests/data/v3-only-unknown-pair-in-apk-sig-block.apk", |
| 179 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 180 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 184 | fn apk_signed_with_v3_ignorable_unsupported_sig_algs_is_valid() { |
| 185 | validate_apk( |
| 186 | "tests/data/v3-only-with-ignorable-unsupported-sig-algs.apk", |
| 187 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 188 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 192 | fn apk_signed_with_v3_stamp_is_valid() { |
| 193 | validate_apk("tests/data/v3-only-with-stamp.apk", SignatureAlgorithmID::EcdsaWithSha256); |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 196 | fn validate_apk<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) { |
| 197 | validate_apk_public_key(&apk_path); |
| 198 | validate_apk_digest(&apk_path, expected_algorithm_id); |
| 199 | } |
| 200 | |
| 201 | /// Validates that the following public keys are equal: |
| 202 | /// * public key from verification |
| 203 | /// * public key extracted from apk without verification |
| 204 | /// * expected public key from the corresponding .der file |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 205 | fn validate_apk_public_key<P: AsRef<Path>>(apk_path: P) { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 206 | let public_key_from_verification = verify(&apk_path); |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 207 | let public_key_from_verification = |
| 208 | public_key_from_verification.expect("Error in verification result"); |
| 209 | |
| 210 | let expected_public_key_path = format!("{}.der", apk_path.as_ref().to_str().unwrap()); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 211 | assert_bytes_eq_to_data_in_file(&public_key_from_verification, expected_public_key_path); |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 212 | |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 213 | let public_key_from_apk = get_public_key_der(&apk_path); |
Alice Wang | 3c01662 | 2022-09-19 09:08:27 +0000 | [diff] [blame] | 214 | let public_key_from_apk = |
| 215 | public_key_from_apk.expect("Error when extracting public key from apk"); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 216 | assert_eq!( |
| 217 | public_key_from_verification, public_key_from_apk, |
| 218 | "Public key extracted directly from apk does not match the public key from verification." |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /// Validates that the following apk_digest are equal: |
| 223 | /// * apk_digest directly extracted from apk without computation |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 224 | /// * computed apk_digest |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 225 | /// * expected apk digest from the corresponding .apk_digest file |
| 226 | fn validate_apk_digest<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) { |
| 227 | let apk = fs::File::open(&apk_path).expect("Unabled to open apk file"); |
| 228 | |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 229 | let (verified_algorithm_id, verified_digest) = get_apk_digest(&apk, /*verify=*/ true) |
| 230 | .expect("Error when extracting apk digest with verification."); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 231 | |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 232 | assert_eq!(expected_algorithm_id, verified_algorithm_id); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 233 | let expected_digest_path = format!("{}.apk_digest", apk_path.as_ref().to_str().unwrap()); |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 234 | assert_bytes_eq_to_data_in_file(&verified_digest, expected_digest_path); |
| 235 | |
| 236 | let (unverified_algorithm_id, unverified_digest) = get_apk_digest(&apk, /*verify=*/ false) |
| 237 | .expect("Error when extracting apk digest without verification."); |
| 238 | assert_eq!(expected_algorithm_id, unverified_algorithm_id); |
| 239 | assert_eq!(verified_digest, unverified_digest); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | fn assert_bytes_eq_to_data_in_file<P: AsRef<Path> + std::fmt::Display>( |
| 243 | bytes_data: &[u8], |
| 244 | expected_data_path: P, |
| 245 | ) { |
| 246 | assert!( |
| 247 | fs::metadata(&expected_data_path).is_ok(), |
| 248 | "File does not exist. You can re-create it with:\n$ echo -en {} > {}\n", |
| 249 | bytes_data.iter().map(|b| format!("\\\\x{:02x}", b)).collect::<String>(), |
| 250 | expected_data_path |
| 251 | ); |
| 252 | let expected_data = fs::read(&expected_data_path).unwrap(); |
| 253 | assert_eq!( |
| 254 | expected_data, bytes_data, |
| 255 | "Actual data does not match the data from: {}", |
| 256 | expected_data_path |
| 257 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 258 | } |