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 | |
| 17 | //! Verifies APK Signature Scheme V3 |
Alice Wang | af1d15b | 2022-09-09 11:09:51 +0000 | [diff] [blame] | 18 | //! |
| 19 | //! [v3 verification]: https://source.android.com/security/apksigning/v3#verification |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 20 | |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 21 | use anyhow::{ensure, Context, Result}; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 22 | use bytes::Bytes; |
Andrew Scull | c208eb4 | 2022-05-22 16:17:52 +0000 | [diff] [blame] | 23 | use openssl::pkey::{self, PKey}; |
Alice Wang | 79713d9 | 2022-07-14 15:10:03 +0000 | [diff] [blame] | 24 | use openssl::x509::X509; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 25 | use std::fs::File; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 26 | use std::io::{Read, Seek}; |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 27 | use std::ops::RangeInclusive; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 28 | use std::path::Path; |
| 29 | |
Alice Wang | 5d0f89a | 2022-09-15 15:06:10 +0000 | [diff] [blame] | 30 | use crate::algorithms::SignatureAlgorithmID; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 31 | use crate::bytes_ext::{BytesExt, LengthPrefixed, ReadFromBytes}; |
Jooyung Han | 5b4c70e | 2021-08-09 16:36:13 +0900 | [diff] [blame] | 32 | use crate::sigutil::*; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 33 | |
| 34 | pub const APK_SIGNATURE_SCHEME_V3_BLOCK_ID: u32 = 0xf05368c0; |
| 35 | |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 36 | type Signers = LengthPrefixed<Vec<LengthPrefixed<Signer>>>; |
| 37 | |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 38 | #[derive(Debug)] |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 39 | pub(crate) struct Signer { |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 40 | signed_data: LengthPrefixed<Bytes>, // not verified yet |
| 41 | min_sdk: u32, |
| 42 | max_sdk: u32, |
| 43 | signatures: LengthPrefixed<Vec<LengthPrefixed<Signature>>>, |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 44 | public_key: PKey<pkey::Public>, |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | impl Signer { |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 48 | fn sdk_range(&self) -> RangeInclusive<u32> { |
| 49 | self.min_sdk..=self.max_sdk |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | struct SignedData { |
| 54 | digests: LengthPrefixed<Vec<LengthPrefixed<Digest>>>, |
| 55 | certificates: LengthPrefixed<Vec<LengthPrefixed<X509Certificate>>>, |
| 56 | min_sdk: u32, |
| 57 | max_sdk: u32, |
Alice Wang | 4b7c0ba | 2022-09-07 15:12:36 +0000 | [diff] [blame] | 58 | #[allow(dead_code)] |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 59 | additional_attributes: LengthPrefixed<Vec<LengthPrefixed<AdditionalAttributes>>>, |
| 60 | } |
| 61 | |
| 62 | impl SignedData { |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 63 | fn sdk_range(&self) -> RangeInclusive<u32> { |
| 64 | self.min_sdk..=self.max_sdk |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 65 | } |
Alice Wang | cd0fa45 | 2022-09-21 09:48:33 +0000 | [diff] [blame] | 66 | |
| 67 | fn find_digest_by_algorithm(&self, algorithm_id: SignatureAlgorithmID) -> Result<&Digest> { |
| 68 | Ok(self |
| 69 | .digests |
| 70 | .iter() |
| 71 | .find(|&dig| dig.signature_algorithm_id == Some(algorithm_id)) |
| 72 | .context(format!("Digest not found for algorithm: {:?}", algorithm_id))?) |
| 73 | } |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Jooyung Han | 5b4c70e | 2021-08-09 16:36:13 +0900 | [diff] [blame] | 76 | #[derive(Debug)] |
Alice Wang | f27626a | 2022-09-27 12:36:22 +0000 | [diff] [blame] | 77 | pub(crate) struct Signature { |
Alice Wang | d73d0ff | 2022-09-20 11:33:30 +0000 | [diff] [blame] | 78 | /// Option is used here to allow us to ignore unsupported algorithm. |
Alice Wang | f27626a | 2022-09-27 12:36:22 +0000 | [diff] [blame] | 79 | pub(crate) signature_algorithm_id: Option<SignatureAlgorithmID>, |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 80 | signature: LengthPrefixed<Bytes>, |
| 81 | } |
| 82 | |
| 83 | struct Digest { |
Alice Wang | d73d0ff | 2022-09-20 11:33:30 +0000 | [diff] [blame] | 84 | signature_algorithm_id: Option<SignatureAlgorithmID>, |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 85 | digest: LengthPrefixed<Bytes>, |
| 86 | } |
| 87 | |
| 88 | type X509Certificate = Bytes; |
| 89 | type AdditionalAttributes = Bytes; |
| 90 | |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 91 | /// Verifies APK Signature Scheme v3 signatures of the provided APK and returns the public key |
Andrew Scull | f3fd4c6 | 2022-05-22 14:41:21 +0000 | [diff] [blame] | 92 | /// associated with the signer in DER format. |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 93 | pub fn verify<P: AsRef<Path>>(apk_path: P, current_sdk: u32) -> Result<Box<[u8]>> { |
Alice Wang | 3c01662 | 2022-09-19 09:08:27 +0000 | [diff] [blame] | 94 | let apk = File::open(apk_path.as_ref())?; |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 95 | let (signer, mut sections) = extract_signer_and_apk_sections(apk, current_sdk)?; |
Alice Wang | 7170127 | 2022-09-20 10:03:02 +0000 | [diff] [blame] | 96 | signer.verify(&mut sections) |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 97 | } |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 98 | |
Jiyong Park | a41535b | 2021-09-10 19:31:48 +0900 | [diff] [blame] | 99 | /// Gets the public key (in DER format) that was used to sign the given APK/APEX file |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 100 | pub fn get_public_key_der<P: AsRef<Path>>(apk_path: P, current_sdk: u32) -> Result<Box<[u8]>> { |
Alice Wang | 3c01662 | 2022-09-19 09:08:27 +0000 | [diff] [blame] | 101 | let apk = File::open(apk_path.as_ref())?; |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 102 | let (signer, _) = extract_signer_and_apk_sections(apk, current_sdk)?; |
Alice Wang | 7170127 | 2022-09-20 10:03:02 +0000 | [diff] [blame] | 103 | Ok(signer.public_key.public_key_to_der()?.into_boxed_slice()) |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 106 | pub(crate) fn extract_signer_and_apk_sections<R: Read + Seek>( |
| 107 | apk: R, |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 108 | current_sdk: u32, |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 109 | ) -> Result<(Signer, ApkSections<R>)> { |
Andrew Scull | a11b83a | 2022-06-01 09:23:13 +0000 | [diff] [blame] | 110 | let mut sections = ApkSections::new(apk)?; |
Alice Wang | 7170127 | 2022-09-20 10:03:02 +0000 | [diff] [blame] | 111 | let mut block = sections.find_signature(APK_SIGNATURE_SCHEME_V3_BLOCK_ID).context( |
Alan Stokes | a687699 | 2023-01-20 12:26:25 +0000 | [diff] [blame] | 112 | "Fallback to v2 when v3 block not found is not yet implemented.", // b/197052981 |
Alice Wang | 7170127 | 2022-09-20 10:03:02 +0000 | [diff] [blame] | 113 | )?; |
Alan Stokes | 25f6936 | 2023-03-06 16:51:54 +0000 | [diff] [blame] | 114 | let signers = block.read::<Signers>()?.into_inner(); |
| 115 | let mut supported = |
| 116 | signers.into_iter().filter(|s| s.sdk_range().contains(¤t_sdk)).collect::<Vec<_>>(); |
Alice Wang | 7170127 | 2022-09-20 10:03:02 +0000 | [diff] [blame] | 117 | ensure!( |
| 118 | supported.len() == 1, |
| 119 | "APK Signature Scheme V3 only supports one signer: {} signers found.", |
| 120 | supported.len() |
| 121 | ); |
| 122 | Ok((supported.pop().unwrap().into_inner(), sections)) |
Andrew Scull | a11b83a | 2022-06-01 09:23:13 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 125 | impl Signer { |
Alice Wang | f27626a | 2022-09-27 12:36:22 +0000 | [diff] [blame] | 126 | /// Selects the signature that has the strongest supported `SignatureAlgorithmID`. |
| 127 | /// The strongest signature is used in both v3 verification and v4 apk digest computation. |
| 128 | pub(crate) fn strongest_signature(&self) -> Result<&Signature> { |
Andrew Scull | 9173eb8 | 2022-06-01 09:17:14 +0000 | [diff] [blame] | 129 | Ok(self |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 130 | .signatures |
| 131 | .iter() |
Alice Wang | 5070102 | 2022-09-21 08:51:38 +0000 | [diff] [blame] | 132 | .filter(|sig| sig.signature_algorithm_id.map_or(false, |algo| algo.is_supported())) |
Alice Wang | d73d0ff | 2022-09-20 11:33:30 +0000 | [diff] [blame] | 133 | .max_by_key(|sig| sig.signature_algorithm_id.unwrap().content_digest_algorithm()) |
Alan Stokes | a687699 | 2023-01-20 12:26:25 +0000 | [diff] [blame] | 134 | .context("No supported APK signatures found; DSA is not supported")?) |
Andrew Scull | 9173eb8 | 2022-06-01 09:17:14 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Alice Wang | f27626a | 2022-09-27 12:36:22 +0000 | [diff] [blame] | 137 | pub(crate) fn find_digest_by_algorithm( |
| 138 | &self, |
| 139 | algorithm_id: SignatureAlgorithmID, |
| 140 | ) -> Result<Box<[u8]>> { |
Andrew Scull | a11b83a | 2022-06-01 09:23:13 +0000 | [diff] [blame] | 141 | let signed_data: SignedData = self.signed_data.slice(..).read()?; |
Alice Wang | f27626a | 2022-09-27 12:36:22 +0000 | [diff] [blame] | 142 | let digest = signed_data.find_digest_by_algorithm(algorithm_id)?; |
| 143 | Ok(digest.digest.as_ref().to_vec().into_boxed_slice()) |
Andrew Scull | a11b83a | 2022-06-01 09:23:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 146 | /// Verifies the strongest signature from signatures against signed data using public key. |
| 147 | /// Returns the verified signed data. |
| 148 | fn verify_signature(&self, strongest: &Signature) -> Result<SignedData> { |
| 149 | let mut verifier = strongest |
| 150 | .signature_algorithm_id |
| 151 | .context("Unsupported algorithm")? |
| 152 | .new_verifier(&self.public_key)?; |
| 153 | verifier.update(&self.signed_data)?; |
| 154 | ensure!(verifier.verify(&strongest.signature)?, "Signature is invalid."); |
| 155 | // It is now safe to parse signed data. |
| 156 | self.signed_data.slice(..).read() |
| 157 | } |
| 158 | |
Alice Wang | af1d15b | 2022-09-09 11:09:51 +0000 | [diff] [blame] | 159 | /// The steps in this method implements APK Signature Scheme v3 verification step 3. |
Andrew Scull | 9173eb8 | 2022-06-01 09:17:14 +0000 | [diff] [blame] | 160 | fn verify<R: Read + Seek>(&self, sections: &mut ApkSections<R>) -> Result<Box<[u8]>> { |
| 161 | // 1. Choose the strongest supported signature algorithm ID from signatures. |
| 162 | let strongest = self.strongest_signature()?; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 163 | |
| 164 | // 2. Verify the corresponding signature from signatures against signed data using public key. |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 165 | let verified_signed_data = self.verify_signature(strongest)?; |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 166 | |
| 167 | // 3. Verify the min and max SDK versions in the signed data match those specified for the |
| 168 | // signer. |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 169 | ensure!( |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 170 | self.sdk_range() == verified_signed_data.sdk_range(), |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 171 | "SDK versions mismatch between signed and unsigned in v3 signer block." |
| 172 | ); |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 173 | |
| 174 | // 4. Verify that the ordered list of signature algorithm IDs in digests and signatures is |
| 175 | // identical. (This is to prevent signature stripping/addition.) |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 176 | ensure!( |
| 177 | self.signatures |
| 178 | .iter() |
| 179 | .map(|sig| sig.signature_algorithm_id) |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 180 | .eq(verified_signed_data.digests.iter().map(|dig| dig.signature_algorithm_id)), |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 181 | "Signature algorithms don't match between digests and signatures records" |
| 182 | ); |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 183 | |
| 184 | // 5. Compute the digest of APK contents using the same digest algorithm as the digest |
| 185 | // algorithm used by the signature algorithm. |
Alice Wang | cd0fa45 | 2022-09-21 09:48:33 +0000 | [diff] [blame] | 186 | let digest = verified_signed_data.find_digest_by_algorithm( |
| 187 | strongest.signature_algorithm_id.context("Unsupported algorithm")?, |
| 188 | )?; |
| 189 | let computed = sections.compute_digest(digest.signature_algorithm_id.unwrap())?; |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 190 | |
| 191 | // 6. Verify that the computed digest is identical to the corresponding digest from digests. |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 192 | ensure!( |
| 193 | computed == digest.digest.as_ref(), |
| 194 | "Digest mismatch: computed={:?} vs expected={:?}", |
Tanmoy Mollik | 40ff803 | 2022-11-25 15:00:04 +0000 | [diff] [blame] | 195 | hex::encode(&computed), |
| 196 | hex::encode(digest.digest.as_ref()), |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 197 | ); |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 198 | |
Alice Wang | 79713d9 | 2022-07-14 15:10:03 +0000 | [diff] [blame] | 199 | // 7. Verify that public key of the first certificate of certificates is identical |
Jooyung Han | 543e712 | 2021-08-11 01:48:45 +0900 | [diff] [blame] | 200 | // to public key. |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 201 | let cert = verified_signed_data.certificates.first().context("No certificates listed")?; |
Alice Wang | 79713d9 | 2022-07-14 15:10:03 +0000 | [diff] [blame] | 202 | let cert = X509::from_der(cert.as_ref())?; |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 203 | ensure!( |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 204 | cert.public_key()?.public_eq(&self.public_key), |
Alice Wang | bc4b9a9 | 2022-09-16 13:13:18 +0000 | [diff] [blame] | 205 | "Public key mismatch between certificate and signature record" |
| 206 | ); |
Jooyung Han | 543e712 | 2021-08-11 01:48:45 +0900 | [diff] [blame] | 207 | |
Alice Wang | 9288935 | 2022-09-16 10:42:52 +0000 | [diff] [blame] | 208 | // TODO(b/245914104) |
| 209 | // 8. If the proof-of-rotation attribute exists for the signer verify that the |
| 210 | // struct is valid and this signer is the last certificate in the list. |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 211 | Ok(self.public_key.public_key_to_der()?.into_boxed_slice()) |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 215 | // ReadFromBytes implementations |
Alice Wang | 9288935 | 2022-09-16 10:42:52 +0000 | [diff] [blame] | 216 | // TODO(b/190343842): add derive macro: #[derive(ReadFromBytes)] |
Jooyung Han | 12a0b70 | 2021-08-05 23:20:31 +0900 | [diff] [blame] | 217 | |
| 218 | impl ReadFromBytes for Signer { |
| 219 | fn read_from_bytes(buf: &mut Bytes) -> Result<Self> { |
| 220 | Ok(Self { |
| 221 | signed_data: buf.read()?, |
| 222 | min_sdk: buf.read()?, |
| 223 | max_sdk: buf.read()?, |
| 224 | signatures: buf.read()?, |
| 225 | public_key: buf.read()?, |
| 226 | }) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | impl ReadFromBytes for SignedData { |
| 231 | fn read_from_bytes(buf: &mut Bytes) -> Result<Self> { |
| 232 | Ok(Self { |
| 233 | digests: buf.read()?, |
| 234 | certificates: buf.read()?, |
| 235 | min_sdk: buf.read()?, |
| 236 | max_sdk: buf.read()?, |
| 237 | additional_attributes: buf.read()?, |
| 238 | }) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | impl ReadFromBytes for Signature { |
| 243 | fn read_from_bytes(buf: &mut Bytes) -> Result<Self> { |
| 244 | Ok(Signature { signature_algorithm_id: buf.read()?, signature: buf.read()? }) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | impl ReadFromBytes for Digest { |
| 249 | fn read_from_bytes(buf: &mut Bytes) -> Result<Self> { |
| 250 | Ok(Self { signature_algorithm_id: buf.read()?, digest: buf.read()? }) |
| 251 | } |
| 252 | } |
Jooyung Han | d839785 | 2021-08-10 16:29:36 +0900 | [diff] [blame] | 253 | |
Alice Wang | a7cac42 | 2022-09-20 13:57:32 +0000 | [diff] [blame] | 254 | impl ReadFromBytes for PKey<pkey::Public> { |
| 255 | fn read_from_bytes(buf: &mut Bytes) -> Result<Self> { |
| 256 | let raw_public_key = buf.read::<LengthPrefixed<Bytes>>()?; |
| 257 | Ok(PKey::public_key_from_der(raw_public_key.as_ref())?) |
| 258 | } |
| 259 | } |