Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (C) 2017 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | Validate a given (signed) target_files.zip. |
| 19 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 20 | It performs the following checks to assert the integrity of the input zip. |
| 21 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 22 | - It verifies the file consistency between the ones in IMAGES/system.img (read |
| 23 | via IMAGES/system.map) and the ones under unpacked folder of SYSTEM/. The |
| 24 | same check also applies to the vendor image if present. |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 25 | |
| 26 | - It verifies the install-recovery script consistency, by comparing the |
| 27 | checksums in the script against the ones of IMAGES/{boot,recovery}.img. |
| 28 | |
| 29 | - It verifies the signed Verified Boot related images, for both of Verified |
| 30 | Boot 1.0 and 2.0 (aka AVB). |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 31 | """ |
| 32 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 33 | import argparse |
| 34 | import filecmp |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 35 | import logging |
| 36 | import os.path |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 37 | import re |
Tao Bao | c63626b | 2018-03-07 21:40:24 -0800 | [diff] [blame] | 38 | import zipfile |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 39 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 40 | import common |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 41 | |
| 42 | |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 43 | def _ReadFile(file_name, unpacked_name, round_up=False): |
| 44 | """Constructs and returns a File object. Rounds up its size if needed.""" |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 45 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 46 | assert os.path.exists(unpacked_name) |
Tao Bao | da30cfa | 2017-12-01 16:19:46 -0800 | [diff] [blame] | 47 | with open(unpacked_name, 'rb') as f: |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 48 | file_data = f.read() |
| 49 | file_size = len(file_data) |
| 50 | if round_up: |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 51 | file_size_rounded_up = common.RoundUpTo4K(file_size) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 52 | file_data += '\0' * (file_size_rounded_up - file_size) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 53 | return common.File(file_name, file_data) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def ValidateFileAgainstSha1(input_tmp, file_name, file_path, expected_sha1): |
| 57 | """Check if the file has the expected SHA-1.""" |
| 58 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 59 | logging.info('Validating the SHA-1 of %s', file_name) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 60 | unpacked_name = os.path.join(input_tmp, file_path) |
| 61 | assert os.path.exists(unpacked_name) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 62 | actual_sha1 = _ReadFile(file_name, unpacked_name, False).sha1 |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 63 | assert actual_sha1 == expected_sha1, \ |
| 64 | 'SHA-1 mismatches for {}. actual {}, expected {}'.format( |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 65 | file_name, actual_sha1, expected_sha1) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 66 | |
| 67 | |
Tao Bao | 63e2f49 | 2018-05-11 23:38:46 -0700 | [diff] [blame] | 68 | def ValidateFileConsistency(input_zip, input_tmp, info_dict): |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 69 | """Compare the files from image files and unpacked folders.""" |
| 70 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 71 | def CheckAllFiles(which): |
| 72 | logging.info('Checking %s image.', which) |
Tao Bao | c63626b | 2018-03-07 21:40:24 -0800 | [diff] [blame] | 73 | # Allow having shared blocks when loading the sparse image, because allowing |
| 74 | # that doesn't affect the checks below (we will have all the blocks on file, |
| 75 | # unless it's skipped due to the holes). |
| 76 | image = common.GetSparseImage(which, input_tmp, input_zip, True) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 77 | prefix = '/' + which |
| 78 | for entry in image.file_map: |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 79 | # Skip entries like '__NONZERO-0'. |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 80 | if not entry.startswith(prefix): |
| 81 | continue |
| 82 | |
| 83 | # Read the blocks that the file resides. Note that it will contain the |
| 84 | # bytes past the file length, which is expected to be padded with '\0's. |
| 85 | ranges = image.file_map[entry] |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 86 | |
Tao Bao | 2a20f34 | 2018-12-03 15:08:23 -0800 | [diff] [blame] | 87 | # Use the original RangeSet if applicable, which includes the shared |
| 88 | # blocks. And this needs to happen before checking the monotonicity flag. |
| 89 | if ranges.extra.get('uses_shared_blocks'): |
| 90 | file_ranges = ranges.extra['uses_shared_blocks'] |
| 91 | else: |
| 92 | file_ranges = ranges |
| 93 | |
xunchang | c0f77ee | 2019-02-20 15:03:43 -0800 | [diff] [blame] | 94 | incomplete = file_ranges.extra.get('incomplete', False) |
| 95 | if incomplete: |
| 96 | logging.warning('Skipping %s that has incomplete block list', entry) |
| 97 | continue |
| 98 | |
Tao Bao | d32936d | 2018-05-17 19:42:41 -0700 | [diff] [blame] | 99 | # TODO(b/79951650): Handle files with non-monotonic ranges. |
Tao Bao | 2a20f34 | 2018-12-03 15:08:23 -0800 | [diff] [blame] | 100 | if not file_ranges.monotonic: |
Tao Bao | d32936d | 2018-05-17 19:42:41 -0700 | [diff] [blame] | 101 | logging.warning( |
Tao Bao | 2a20f34 | 2018-12-03 15:08:23 -0800 | [diff] [blame] | 102 | 'Skipping %s that has non-monotonic ranges: %s', entry, file_ranges) |
Tao Bao | d32936d | 2018-05-17 19:42:41 -0700 | [diff] [blame] | 103 | continue |
| 104 | |
Tao Bao | 2a20f34 | 2018-12-03 15:08:23 -0800 | [diff] [blame] | 105 | blocks_sha1 = image.RangeSha1(file_ranges) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 106 | |
| 107 | # The filename under unpacked directory, such as SYSTEM/bin/sh. |
| 108 | unpacked_name = os.path.join( |
| 109 | input_tmp, which.upper(), entry[(len(prefix) + 1):]) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 110 | unpacked_file = _ReadFile(entry, unpacked_name, True) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 111 | file_sha1 = unpacked_file.sha1 |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 112 | assert blocks_sha1 == file_sha1, \ |
| 113 | 'file: %s, range: %s, blocks_sha1: %s, file_sha1: %s' % ( |
Tao Bao | 2a20f34 | 2018-12-03 15:08:23 -0800 | [diff] [blame] | 114 | entry, file_ranges, blocks_sha1, file_sha1) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 115 | |
| 116 | logging.info('Validating file consistency.') |
| 117 | |
Tao Bao | 63e2f49 | 2018-05-11 23:38:46 -0700 | [diff] [blame] | 118 | # TODO(b/79617342): Validate non-sparse images. |
| 119 | if info_dict.get('extfs_sparse_flag') != '-s': |
| 120 | logging.warning('Skipped due to target using non-sparse images') |
| 121 | return |
| 122 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 123 | # Verify IMAGES/system.img. |
| 124 | CheckAllFiles('system') |
| 125 | |
| 126 | # Verify IMAGES/vendor.img if applicable. |
| 127 | if 'VENDOR/' in input_zip.namelist(): |
| 128 | CheckAllFiles('vendor') |
| 129 | |
| 130 | # Not checking IMAGES/system_other.img since it doesn't have the map file. |
| 131 | |
| 132 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 133 | def ValidateInstallRecoveryScript(input_tmp, info_dict): |
| 134 | """Validate the SHA-1 embedded in install-recovery.sh. |
| 135 | |
| 136 | install-recovery.sh is written in common.py and has the following format: |
| 137 | |
| 138 | 1. full recovery: |
| 139 | ... |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 140 | if ! applypatch --check type:device:size:sha1; then |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame^] | 141 | applypatch --flash /vendor/etc/recovery.img \\ |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 142 | type:device:size:sha1 && \\ |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 143 | ... |
| 144 | |
| 145 | 2. recovery from boot: |
| 146 | ... |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 147 | if ! applypatch --check type:recovery_device:recovery_size:recovery_sha1; then |
| 148 | applypatch [--bonus bonus_args] \\ |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame^] | 149 | --patch /vendor/recovery-from-boot.p \\ |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 150 | --source type:boot_device:boot_size:boot_sha1 \\ |
| 151 | --target type:recovery_device:recovery_size:recovery_sha1 && \\ |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 152 | ... |
| 153 | |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame^] | 154 | For full recovery, we want to calculate the SHA-1 of /vendor/etc/recovery.img |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 155 | and compare it against the one embedded in the script. While for recovery |
| 156 | from boot, we want to check the SHA-1 for both recovery.img and boot.img |
| 157 | under IMAGES/. |
| 158 | """ |
| 159 | |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame^] | 160 | board_uses_vendorimage = info_dict.get("board_uses_vendorimage") == "true" |
| 161 | |
| 162 | if board_uses_vendorimage: |
| 163 | script_path = 'VENDOR/bin/install-recovery.sh' |
| 164 | recovery_img = 'VENDOR/etc/recovery.img' |
| 165 | else: |
| 166 | script_path = 'SYSTEM/vendor/bin/install-recovery.sh' |
| 167 | recovery_img = 'SYSTEM/vendor/etc/recovery.img' |
| 168 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 169 | if not os.path.exists(os.path.join(input_tmp, script_path)): |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 170 | logging.info('%s does not exist in input_tmp', script_path) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 171 | return |
| 172 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 173 | logging.info('Checking %s', script_path) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 174 | with open(os.path.join(input_tmp, script_path), 'r') as script: |
| 175 | lines = script.read().strip().split('\n') |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 176 | assert len(lines) >= 10 |
| 177 | check_cmd = re.search(r'if ! applypatch --check (\w+:.+:\w+:\w+);', |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 178 | lines[1].strip()) |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 179 | check_partition = check_cmd.group(1) |
| 180 | assert len(check_partition.split(':')) == 4 |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 181 | |
| 182 | full_recovery_image = info_dict.get("full_recovery_image") == "true" |
| 183 | if full_recovery_image: |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 184 | assert len(lines) == 10, "Invalid line count: {}".format(lines) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 185 | |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 186 | # Expect something like "EMMC:/dev/block/recovery:28:5f9c..62e3". |
| 187 | target = re.search(r'--target (.+) &&', lines[4].strip()) |
| 188 | assert target is not None, \ |
| 189 | "Failed to parse target line \"{}\"".format(lines[4]) |
| 190 | flash_partition = target.group(1) |
| 191 | |
| 192 | # Check we have the same recovery target in the check and flash commands. |
| 193 | assert check_partition == flash_partition, \ |
| 194 | "Mismatching targets: {} vs {}".format(check_partition, flash_partition) |
| 195 | |
| 196 | # Validate the SHA-1 of the recovery image. |
| 197 | recovery_sha1 = flash_partition.split(':')[3] |
| 198 | ValidateFileAgainstSha1( |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame^] | 199 | input_tmp, 'recovery.img', recovery_img, recovery_sha1) |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 200 | else: |
| 201 | assert len(lines) == 11, "Invalid line count: {}".format(lines) |
| 202 | |
| 203 | # --source boot_type:boot_device:boot_size:boot_sha1 |
| 204 | source = re.search(r'--source (\w+:.+:\w+:\w+) \\', lines[4].strip()) |
| 205 | assert source is not None, \ |
| 206 | "Failed to parse source line \"{}\"".format(lines[4]) |
| 207 | |
| 208 | source_partition = source.group(1) |
| 209 | source_info = source_partition.split(':') |
| 210 | assert len(source_info) == 4, \ |
| 211 | "Invalid source partition: {}".format(source_partition) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 212 | ValidateFileAgainstSha1(input_tmp, file_name='boot.img', |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 213 | file_path='IMAGES/boot.img', |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 214 | expected_sha1=source_info[3]) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 215 | |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 216 | # --target recovery_type:recovery_device:recovery_size:recovery_sha1 |
| 217 | target = re.search(r'--target (\w+:.+:\w+:\w+) && \\', lines[5].strip()) |
| 218 | assert target is not None, \ |
| 219 | "Failed to parse target line \"{}\"".format(lines[5]) |
| 220 | target_partition = target.group(1) |
| 221 | |
| 222 | # Check we have the same recovery target in the check and patch commands. |
| 223 | assert check_partition == target_partition, \ |
| 224 | "Mismatching targets: {} vs {}".format( |
| 225 | check_partition, target_partition) |
| 226 | |
| 227 | recovery_info = target_partition.split(':') |
| 228 | assert len(recovery_info) == 4, \ |
| 229 | "Invalid target partition: {}".format(target_partition) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 230 | ValidateFileAgainstSha1(input_tmp, file_name='recovery.img', |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 231 | file_path='IMAGES/recovery.img', |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 232 | expected_sha1=recovery_info[3]) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 233 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 234 | logging.info('Done checking %s', script_path) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 235 | |
| 236 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 237 | def ValidateVerifiedBootImages(input_tmp, info_dict, options): |
| 238 | """Validates the Verified Boot related images. |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 239 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 240 | For Verified Boot 1.0, it verifies the signatures of the bootable images |
| 241 | (boot/recovery etc), as well as the dm-verity metadata in system images |
| 242 | (system/vendor/product). For Verified Boot 2.0, it calls avbtool to verify |
| 243 | vbmeta.img, which in turn verifies all the descriptors listed in vbmeta. |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 244 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 245 | Args: |
| 246 | input_tmp: The top-level directory of unpacked target-files.zip. |
| 247 | info_dict: The loaded info dict. |
| 248 | options: A dict that contains the user-supplied public keys to be used for |
| 249 | image verification. In particular, 'verity_key' is used to verify the |
| 250 | bootable images in VB 1.0, and the vbmeta image in VB 2.0, where |
| 251 | applicable. 'verity_key_mincrypt' will be used to verify the system |
| 252 | images in VB 1.0. |
| 253 | |
| 254 | Raises: |
| 255 | AssertionError: On any verification failure. |
| 256 | """ |
| 257 | # Verified boot 1.0 (images signed with boot_signer and verity_signer). |
| 258 | if info_dict.get('boot_signer') == 'true': |
| 259 | logging.info('Verifying Verified Boot images...') |
| 260 | |
| 261 | # Verify the boot/recovery images (signed with boot_signer), against the |
| 262 | # given X.509 encoded pubkey (or falling back to the one in the info_dict if |
| 263 | # none given). |
| 264 | verity_key = options['verity_key'] |
| 265 | if verity_key is None: |
| 266 | verity_key = info_dict['verity_key'] + '.x509.pem' |
| 267 | for image in ('boot.img', 'recovery.img', 'recovery-two-step.img'): |
Tao Bao | 0480850 | 2019-07-25 23:11:41 -0700 | [diff] [blame] | 268 | if image == 'recovery-two-step.img': |
| 269 | image_path = os.path.join(input_tmp, 'OTA', image) |
| 270 | else: |
| 271 | image_path = os.path.join(input_tmp, 'IMAGES', image) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 272 | if not os.path.exists(image_path): |
| 273 | continue |
| 274 | |
| 275 | cmd = ['boot_signer', '-verify', image_path, '-certificate', verity_key] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 276 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 277 | stdoutdata, _ = proc.communicate() |
| 278 | assert proc.returncode == 0, \ |
| 279 | 'Failed to verify {} with boot_signer:\n{}'.format(image, stdoutdata) |
| 280 | logging.info( |
| 281 | 'Verified %s with boot_signer (key: %s):\n%s', image, verity_key, |
| 282 | stdoutdata.rstrip()) |
| 283 | |
| 284 | # Verify verity signed system images in Verified Boot 1.0. Note that not using |
| 285 | # 'elif' here, since 'boot_signer' and 'verity' are not bundled in VB 1.0. |
| 286 | if info_dict.get('verity') == 'true': |
Tao Bao | c998193 | 2019-09-16 12:10:43 -0700 | [diff] [blame] | 287 | # First verify that the verity key is built into the root image (regardless |
| 288 | # of system-as-root). |
| 289 | verity_key_mincrypt = os.path.join(input_tmp, 'ROOT', 'verity_key') |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 290 | assert os.path.exists(verity_key_mincrypt), 'Missing verity_key' |
| 291 | |
Tao Bao | c998193 | 2019-09-16 12:10:43 -0700 | [diff] [blame] | 292 | # Verify /verity_key matches the one given via command line, if any. |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 293 | if options['verity_key_mincrypt'] is None: |
| 294 | logging.warn( |
| 295 | 'Skipped checking the content of /verity_key, as the key file not ' |
| 296 | 'provided. Use --verity_key_mincrypt to specify.') |
| 297 | else: |
| 298 | expected_key = options['verity_key_mincrypt'] |
| 299 | assert filecmp.cmp(expected_key, verity_key_mincrypt, shallow=False), \ |
| 300 | "Mismatching mincrypt verity key files" |
| 301 | logging.info('Verified the content of /verity_key') |
| 302 | |
Tao Bao | c998193 | 2019-09-16 12:10:43 -0700 | [diff] [blame] | 303 | # For devices with a separate ramdisk (i.e. non-system-as-root), there must |
| 304 | # be a copy in ramdisk. |
| 305 | if info_dict.get("system_root_image") != "true": |
| 306 | verity_key_ramdisk = os.path.join( |
| 307 | input_tmp, 'BOOT', 'RAMDISK', 'verity_key') |
| 308 | assert os.path.exists(verity_key_ramdisk), 'Missing verity_key in ramdisk' |
| 309 | |
| 310 | assert filecmp.cmp( |
| 311 | verity_key_mincrypt, verity_key_ramdisk, shallow=False), \ |
| 312 | 'Mismatching verity_key files in root and ramdisk' |
| 313 | logging.info('Verified the content of /verity_key in ramdisk') |
| 314 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 315 | # Then verify the verity signed system/vendor/product images, against the |
| 316 | # verity pubkey in mincrypt format. |
| 317 | for image in ('system.img', 'vendor.img', 'product.img'): |
| 318 | image_path = os.path.join(input_tmp, 'IMAGES', image) |
| 319 | |
| 320 | # We are not checking if the image is actually enabled via info_dict (e.g. |
| 321 | # 'system_verity_block_device=...'). Because it's most likely a bug that |
| 322 | # skips signing some of the images in signed target-files.zip, while |
| 323 | # having the top-level verity flag enabled. |
| 324 | if not os.path.exists(image_path): |
| 325 | continue |
| 326 | |
| 327 | cmd = ['verity_verifier', image_path, '-mincrypt', verity_key_mincrypt] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame] | 328 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 329 | stdoutdata, _ = proc.communicate() |
| 330 | assert proc.returncode == 0, \ |
| 331 | 'Failed to verify {} with verity_verifier (key: {}):\n{}'.format( |
| 332 | image, verity_key_mincrypt, stdoutdata) |
| 333 | logging.info( |
| 334 | 'Verified %s with verity_verifier (key: %s):\n%s', image, |
| 335 | verity_key_mincrypt, stdoutdata.rstrip()) |
| 336 | |
| 337 | # Handle the case of Verified Boot 2.0 (AVB). |
| 338 | if info_dict.get("avb_enable") == "true": |
| 339 | logging.info('Verifying Verified Boot 2.0 (AVB) images...') |
| 340 | |
Tao Bao | a81d429 | 2019-03-26 12:13:04 -0700 | [diff] [blame] | 341 | key = options['verity_key'] |
| 342 | if key is None: |
| 343 | key = info_dict['avb_vbmeta_key_path'] |
| 344 | |
| 345 | # avbtool verifies all the images that have descriptors listed in vbmeta. |
| 346 | image = os.path.join(input_tmp, 'IMAGES', 'vbmeta.img') |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 347 | cmd = [info_dict['avb_avbtool'], 'verify_image', '--image', image, |
| 348 | '--key', key] |
Tao Bao | a81d429 | 2019-03-26 12:13:04 -0700 | [diff] [blame] | 349 | |
| 350 | # Append the args for chained partitions if any. |
Tao Bao | 08c190f | 2019-06-03 23:07:58 -0700 | [diff] [blame] | 351 | for partition in common.AVB_PARTITIONS + common.AVB_VBMETA_PARTITIONS: |
Tao Bao | a81d429 | 2019-03-26 12:13:04 -0700 | [diff] [blame] | 352 | key_name = 'avb_' + partition + '_key_path' |
| 353 | if info_dict.get(key_name) is not None: |
Tao Bao | 08c190f | 2019-06-03 23:07:58 -0700 | [diff] [blame] | 354 | # Use the key file from command line if specified; otherwise fall back |
| 355 | # to the one in info dict. |
| 356 | key_file = options.get(key_name, info_dict[key_name]) |
Tao Bao | a81d429 | 2019-03-26 12:13:04 -0700 | [diff] [blame] | 357 | chained_partition_arg = common.GetAvbChainedPartitionArg( |
Tao Bao | 08c190f | 2019-06-03 23:07:58 -0700 | [diff] [blame] | 358 | partition, info_dict, key_file) |
Tao Bao | a81d429 | 2019-03-26 12:13:04 -0700 | [diff] [blame] | 359 | cmd.extend(["--expected_chain_partition", chained_partition_arg]) |
| 360 | |
| 361 | proc = common.Run(cmd) |
| 362 | stdoutdata, _ = proc.communicate() |
| 363 | assert proc.returncode == 0, \ |
| 364 | 'Failed to verify {} with avbtool (key: {}):\n{}'.format( |
| 365 | image, key, stdoutdata) |
| 366 | |
| 367 | logging.info( |
| 368 | 'Verified %s with avbtool (key: %s):\n%s', image, key, |
| 369 | stdoutdata.rstrip()) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 370 | |
| 371 | |
| 372 | def main(): |
| 373 | parser = argparse.ArgumentParser( |
| 374 | description=__doc__, |
| 375 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 376 | parser.add_argument( |
| 377 | 'target_files', |
| 378 | help='the input target_files.zip to be validated') |
| 379 | parser.add_argument( |
| 380 | '--verity_key', |
| 381 | help='the verity public key to verify the bootable images (Verified ' |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 382 | 'Boot 1.0), or the vbmeta image (Verified Boot 2.0, aka AVB), where ' |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 383 | 'applicable') |
Tao Bao | 08c190f | 2019-06-03 23:07:58 -0700 | [diff] [blame] | 384 | for partition in common.AVB_PARTITIONS + common.AVB_VBMETA_PARTITIONS: |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 385 | parser.add_argument( |
| 386 | '--avb_' + partition + '_key_path', |
| 387 | help='the public or private key in PEM format to verify AVB chained ' |
| 388 | 'partition of {}'.format(partition)) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 389 | parser.add_argument( |
| 390 | '--verity_key_mincrypt', |
| 391 | help='the verity public key in mincrypt format to verify the system ' |
| 392 | 'images, if target using Verified Boot 1.0') |
| 393 | args = parser.parse_args() |
| 394 | |
| 395 | # Unprovided args will have 'None' as the value. |
| 396 | options = vars(args) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 397 | |
| 398 | logging_format = '%(asctime)s - %(filename)s - %(levelname)-8s: %(message)s' |
| 399 | date_format = '%Y/%m/%d %H:%M:%S' |
| 400 | logging.basicConfig(level=logging.INFO, format=logging_format, |
| 401 | datefmt=date_format) |
| 402 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 403 | logging.info("Unzipping the input target_files.zip: %s", args.target_files) |
| 404 | input_tmp = common.UnzipTemp(args.target_files) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 405 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 406 | info_dict = common.LoadInfoDict(input_tmp) |
Tao Bao | 63e2f49 | 2018-05-11 23:38:46 -0700 | [diff] [blame] | 407 | with zipfile.ZipFile(args.target_files, 'r') as input_zip: |
| 408 | ValidateFileConsistency(input_zip, input_tmp, info_dict) |
| 409 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 410 | ValidateInstallRecoveryScript(input_tmp, info_dict) |
| 411 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 412 | ValidateVerifiedBootImages(input_tmp, info_dict, options) |
| 413 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 414 | # TODO: Check if the OTA keys have been properly updated (the ones on /system, |
| 415 | # in recovery image). |
| 416 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 417 | logging.info("Done.") |
| 418 | |
| 419 | |
| 420 | if __name__ == '__main__': |
| 421 | try: |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 422 | main() |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 423 | finally: |
| 424 | common.Cleanup() |