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 | }; |
Alan Stokes | 068f6d4 | 2023-10-09 10:13:03 +0100 | [diff] [blame] | 20 | use apkzip::zip_sections; |
| 21 | use byteorder::{LittleEndian, ReadBytesExt}; |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 22 | use log::info; |
Charisee | ce83215 | 2023-11-01 18:01:09 +0000 | [diff] [blame] | 23 | use std::fmt::Write; |
Alan Stokes | 068f6d4 | 2023-10-09 10:13:03 +0100 | [diff] [blame] | 24 | use std::io::{Seek, SeekFrom}; |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 25 | use std::{fs, matches, path::Path}; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 26 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 27 | const KEY_NAMES_DSA: &[&str] = &["1024", "2048", "3072"]; |
| 28 | const KEY_NAMES_ECDSA: &[&str] = &["p256", "p384", "p521"]; |
| 29 | const KEY_NAMES_RSA: &[&str] = &["1024", "2048", "3072", "4096", "8192", "16384"]; |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 30 | |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 31 | const SDK_INT: u32 = 31; |
| 32 | |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 33 | /// Make sure any logging from the code under test ends up in logcat. |
| 34 | fn setup() { |
| 35 | android_logger::init_once( |
| 36 | android_logger::Config::default() |
| 37 | .with_tag("apkverify_test") |
| 38 | .with_min_level(log::Level::Info), |
| 39 | ); |
| 40 | info!("Test starting"); |
| 41 | } |
| 42 | |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 43 | #[test] |
Alan Stokes | 068f6d4 | 2023-10-09 10:13:03 +0100 | [diff] [blame] | 44 | fn test_zip_sections_with_apk() { |
| 45 | let mut reader = fs::File::open("tests/data/v3-only-with-stamp.apk").unwrap(); |
| 46 | let sections = zip_sections(&mut reader).unwrap(); |
| 47 | |
| 48 | // Checks Central directory. |
| 49 | assert_eq!( |
| 50 | sections.central_directory_offset + sections.central_directory_size, |
| 51 | sections.eocd_offset |
| 52 | ); |
| 53 | |
| 54 | // Checks EOCD. |
| 55 | const EOCD_SIGNATURE: u32 = 0x06054b50; |
| 56 | |
| 57 | reader.seek(SeekFrom::Start(sections.eocd_offset as u64)).unwrap(); |
| 58 | assert_eq!(reader.read_u32::<LittleEndian>().unwrap(), EOCD_SIGNATURE); |
| 59 | assert_eq!( |
| 60 | reader.metadata().unwrap().len(), |
| 61 | (sections.eocd_offset + sections.eocd_size) as u64 |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | #[test] |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 66 | fn test_verify_truncated_cd() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 67 | setup(); |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 68 | use zip::result::ZipError; |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 69 | let res = verify("tests/data/v2-only-truncated-cd.apk", SDK_INT); |
Alice Wang | 9288935 | 2022-09-16 10:42:52 +0000 | [diff] [blame] | 70 | // TODO(b/190343842): consider making a helper for err assertion |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 71 | assert!(matches!( |
Andrew Walbran | 117cd5e | 2021-08-13 11:42:13 +0000 | [diff] [blame] | 72 | res.unwrap_err().root_cause().downcast_ref::<ZipError>().unwrap(), |
Jooyung Han | cee6de6 | 2021-08-11 15:52:07 +0900 | [diff] [blame] | 73 | ZipError::InvalidArchive(_), |
| 74 | )); |
| 75 | } |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 76 | |
| 77 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 78 | fn apex_signed_with_v3_rsa_pkcs1_sha512_is_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 79 | setup(); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 80 | validate_apk("tests/data/test.apex", SignatureAlgorithmID::RsaPkcs1V15WithSha512); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 83 | #[test] |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 84 | fn apks_signed_with_v3_dsa_sha256_are_not_supported() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 85 | setup(); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 86 | for key_name in KEY_NAMES_DSA.iter() { |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 87 | let res = verify(format!("tests/data/v3-only-with-dsa-sha256-{}.apk", key_name), SDK_INT); |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 88 | assert!(res.is_err(), "DSA algorithm is not supported for verification. See b/197052981."); |
Alan Stokes | a687699 | 2023-01-20 12:26:25 +0000 | [diff] [blame] | 89 | assert_contains(&res.unwrap_err().to_string(), "No supported APK signatures found"); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn apks_signed_with_v3_ecdsa_sha256_are_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 95 | setup(); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 96 | for key_name in KEY_NAMES_ECDSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 97 | validate_apk( |
| 98 | format!("tests/data/v3-only-with-ecdsa-sha256-{}.apk", key_name), |
| 99 | SignatureAlgorithmID::EcdsaWithSha256, |
| 100 | ); |
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] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 105 | fn apks_signed_with_v3_ecdsa_sha512_are_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 106 | setup(); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 107 | for key_name in KEY_NAMES_ECDSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 108 | validate_apk( |
| 109 | format!("tests/data/v3-only-with-ecdsa-sha512-{}.apk", key_name), |
| 110 | SignatureAlgorithmID::EcdsaWithSha512, |
| 111 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 116 | fn apks_signed_with_v3_rsa_pkcs1_sha256_are_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 117 | setup(); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 118 | for key_name in KEY_NAMES_RSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 119 | validate_apk( |
| 120 | format!("tests/data/v3-only-with-rsa-pkcs1-sha256-{}.apk", key_name), |
| 121 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 122 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 127 | fn apks_signed_with_v3_rsa_pkcs1_sha512_are_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 128 | setup(); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 129 | for key_name in KEY_NAMES_RSA.iter() { |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 130 | validate_apk( |
| 131 | format!("tests/data/v3-only-with-rsa-pkcs1-sha512-{}.apk", key_name), |
| 132 | SignatureAlgorithmID::RsaPkcs1V15WithSha512, |
| 133 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 137 | #[test] |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 138 | fn test_verify_v3_sig_min_max_sdk() { |
| 139 | setup(); |
| 140 | // The Signer for this APK has min_sdk=24, max_sdk=32. |
| 141 | let path = "tests/data/v31-rsa-2048_2-tgt-33-1-tgt-28.apk"; |
| 142 | |
| 143 | let res = verify(path, 23); |
| 144 | assert!(res.is_err()); |
| 145 | assert_contains(&res.unwrap_err().to_string(), "0 signers found"); |
| 146 | |
| 147 | let res = verify(path, 24); |
| 148 | assert!(res.is_ok()); |
| 149 | |
| 150 | let res = verify(path, 32); |
| 151 | assert!(res.is_ok()); |
| 152 | |
| 153 | let res = verify(path, 33); |
| 154 | assert!(res.is_err()); |
| 155 | assert_contains(&res.unwrap_err().to_string(), "0 signers found"); |
| 156 | } |
| 157 | |
| 158 | #[test] |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 159 | fn test_verify_v3_sig_does_not_verify() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 160 | setup(); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 161 | let path_list = [ |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 162 | "tests/data/v3-only-with-ecdsa-sha512-p521-sig-does-not-verify.apk", |
| 163 | "tests/data/v3-only-with-rsa-pkcs1-sha256-3072-sig-does-not-verify.apk", |
| 164 | ]; |
| 165 | for path in path_list.iter() { |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 166 | let res = verify(path, SDK_INT); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 167 | assert!(res.is_err()); |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 168 | assert_contains(&res.unwrap_err().to_string(), "Signature is invalid"); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 172 | #[test] |
| 173 | fn test_verify_v3_digest_mismatch() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 174 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 175 | let res = verify("tests/data/v3-only-with-rsa-pkcs1-sha512-8192-digest-mismatch.apk", SDK_INT); |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 176 | assert!(res.is_err()); |
| 177 | assert_contains(&res.unwrap_err().to_string(), "Digest mismatch"); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn test_verify_v3_wrong_apk_sig_block_magic() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 182 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 183 | let res = |
| 184 | verify("tests/data/v3-only-with-ecdsa-sha512-p384-wrong-apk-sig-block-magic.apk", SDK_INT); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 185 | assert!(res.is_err()); |
| 186 | assert_contains(&res.unwrap_err().to_string(), "No APK Signing Block"); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn test_verify_v3_apk_sig_block_size_mismatch() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 191 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 192 | let res = verify( |
| 193 | "tests/data/v3-only-with-rsa-pkcs1-sha512-4096-apk-sig-block-size-mismatch.apk", |
| 194 | SDK_INT, |
| 195 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 196 | assert!(res.is_err()); |
| 197 | assert_contains( |
| 198 | &res.unwrap_err().to_string(), |
| 199 | "APK Signing Block sizes in header and footer do not match", |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn test_verify_v3_cert_and_public_key_mismatch() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 205 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 206 | let res = verify("tests/data/v3-only-cert-and-public-key-mismatch.apk", SDK_INT); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 207 | assert!(res.is_err()); |
| 208 | assert_contains(&res.unwrap_err().to_string(), "Public key mismatch"); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn test_verify_v3_empty() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 213 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 214 | let res = verify("tests/data/v3-only-empty.apk", SDK_INT); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 215 | assert!(res.is_err()); |
| 216 | assert_contains(&res.unwrap_err().to_string(), "APK too small for APK Signing Block"); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn test_verify_v3_no_certs_in_sig() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 221 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 222 | let res = verify("tests/data/v3-only-no-certs-in-sig.apk", SDK_INT); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 223 | assert!(res.is_err()); |
| 224 | assert_contains(&res.unwrap_err().to_string(), "No certificates listed"); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn test_verify_v3_no_supported_sig_algs() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 229 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 230 | let res = verify("tests/data/v3-only-no-supported-sig-algs.apk", SDK_INT); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 231 | assert!(res.is_err()); |
Alan Stokes | a687699 | 2023-01-20 12:26:25 +0000 | [diff] [blame] | 232 | assert_contains(&res.unwrap_err().to_string(), "No supported APK signatures found"); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn test_verify_v3_signatures_and_digests_block_mismatch() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 237 | setup(); |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 238 | let res = verify("tests/data/v3-only-signatures-and-digests-block-mismatch.apk", SDK_INT); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 239 | assert!(res.is_err()); |
| 240 | assert_contains( |
| 241 | &res.unwrap_err().to_string(), |
| 242 | "Signature algorithms don't match between digests and signatures records", |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 247 | fn apk_signed_with_v3_unknown_additional_attr_is_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 248 | setup(); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 249 | validate_apk( |
| 250 | "tests/data/v3-only-unknown-additional-attr.apk", |
| 251 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 252 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 256 | fn apk_signed_with_v3_unknown_pair_in_apk_sig_block_is_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 257 | setup(); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 258 | validate_apk( |
| 259 | "tests/data/v3-only-unknown-pair-in-apk-sig-block.apk", |
| 260 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 261 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 265 | fn apk_signed_with_v3_ignorable_unsupported_sig_algs_is_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 266 | setup(); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 267 | validate_apk( |
| 268 | "tests/data/v3-only-with-ignorable-unsupported-sig-algs.apk", |
| 269 | SignatureAlgorithmID::RsaPkcs1V15WithSha256, |
| 270 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | #[test] |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 274 | fn apk_signed_with_v3_stamp_is_valid() { |
Alan Stokes | 25c8621 | 2023-03-09 17:22:19 +0000 | [diff] [blame] | 275 | setup(); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 276 | validate_apk("tests/data/v3-only-with-stamp.apk", SignatureAlgorithmID::EcdsaWithSha256); |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 279 | fn validate_apk<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) { |
| 280 | validate_apk_public_key(&apk_path); |
| 281 | validate_apk_digest(&apk_path, expected_algorithm_id); |
| 282 | } |
| 283 | |
| 284 | /// Validates that the following public keys are equal: |
| 285 | /// * public key from verification |
| 286 | /// * public key extracted from apk without verification |
| 287 | /// * expected public key from the corresponding .der file |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 288 | fn validate_apk_public_key<P: AsRef<Path>>(apk_path: P) { |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 289 | let public_key_from_verification = verify(&apk_path, SDK_INT); |
Alice Wang | 67d3c00 | 2022-09-16 10:08:25 +0000 | [diff] [blame] | 290 | let public_key_from_verification = |
| 291 | public_key_from_verification.expect("Error in verification result"); |
| 292 | |
| 293 | 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] | 294 | 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] | 295 | |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 296 | let public_key_from_apk = get_public_key_der(&apk_path, SDK_INT); |
Alice Wang | 3c01662 | 2022-09-19 09:08:27 +0000 | [diff] [blame] | 297 | let public_key_from_apk = |
| 298 | public_key_from_apk.expect("Error when extracting public key from apk"); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 299 | assert_eq!( |
| 300 | public_key_from_verification, public_key_from_apk, |
| 301 | "Public key extracted directly from apk does not match the public key from verification." |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | /// Validates that the following apk_digest are equal: |
| 306 | /// * apk_digest directly extracted from apk without computation |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 307 | /// * computed apk_digest |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 308 | /// * expected apk digest from the corresponding .apk_digest file |
| 309 | fn validate_apk_digest<P: AsRef<Path>>(apk_path: P, expected_algorithm_id: SignatureAlgorithmID) { |
| 310 | let apk = fs::File::open(&apk_path).expect("Unabled to open apk file"); |
| 311 | |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 312 | let (verified_algorithm_id, verified_digest) = |
Alan Stokes | 068f6d4 | 2023-10-09 10:13:03 +0100 | [diff] [blame] | 313 | get_apk_digest(&apk, SDK_INT, /* verify= */ true) |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 314 | .expect("Error when extracting apk digest with verification."); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 315 | |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 316 | assert_eq!(expected_algorithm_id, verified_algorithm_id); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 317 | 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] | 318 | assert_bytes_eq_to_data_in_file(&verified_digest, expected_digest_path); |
| 319 | |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 320 | let (unverified_algorithm_id, unverified_digest) = |
Alan Stokes | 068f6d4 | 2023-10-09 10:13:03 +0100 | [diff] [blame] | 321 | get_apk_digest(&apk, SDK_INT, /* verify= */ false) |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 322 | .expect("Error when extracting apk digest without verification."); |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 323 | assert_eq!(expected_algorithm_id, unverified_algorithm_id); |
| 324 | assert_eq!(verified_digest, unverified_digest); |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | fn assert_bytes_eq_to_data_in_file<P: AsRef<Path> + std::fmt::Display>( |
| 328 | bytes_data: &[u8], |
| 329 | expected_data_path: P, |
| 330 | ) { |
| 331 | assert!( |
| 332 | fs::metadata(&expected_data_path).is_ok(), |
| 333 | "File does not exist. You can re-create it with:\n$ echo -en {} > {}\n", |
Charisee | ce83215 | 2023-11-01 18:01:09 +0000 | [diff] [blame] | 334 | bytes_data.iter().fold(String::new(), |mut output, b| { |
| 335 | let _ = write!(output, "\\\\x{:02x}", b); |
| 336 | output |
| 337 | }), |
Alice Wang | 676bb4a | 2022-09-19 14:21:39 +0000 | [diff] [blame] | 338 | expected_data_path |
| 339 | ); |
| 340 | let expected_data = fs::read(&expected_data_path).unwrap(); |
| 341 | assert_eq!( |
| 342 | expected_data, bytes_data, |
| 343 | "Actual data does not match the data from: {}", |
| 344 | expected_data_path |
| 345 | ); |
Seungjae Yoo | 91e250a | 2022-06-07 02:21:56 +0000 | [diff] [blame] | 346 | } |