Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2019 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 | import logging |
| 18 | import os.path |
| 19 | import re |
| 20 | import shlex |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 21 | import shutil |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 22 | import zipfile |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 23 | |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 24 | import apex_manifest |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 25 | import common |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 26 | from common import UnzipTemp, RunAndCheckOutput, MakeTempFile, OPTIONS |
| 27 | |
| 28 | import ota_metadata_pb2 |
| 29 | |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 30 | |
| 31 | logger = logging.getLogger(__name__) |
| 32 | |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 33 | OPTIONS = common.OPTIONS |
| 34 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 35 | APEX_PAYLOAD_IMAGE = 'apex_payload.img' |
| 36 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 37 | APEX_PUBKEY = 'apex_pubkey' |
| 38 | |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 39 | |
| 40 | class ApexInfoError(Exception): |
| 41 | """An Exception raised during Apex Information command.""" |
| 42 | |
| 43 | def __init__(self, message): |
| 44 | Exception.__init__(self, message) |
| 45 | |
| 46 | |
| 47 | class ApexSigningError(Exception): |
| 48 | """An Exception raised during Apex Payload signing.""" |
| 49 | |
| 50 | def __init__(self, message): |
| 51 | Exception.__init__(self, message) |
| 52 | |
| 53 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 54 | class ApexApkSigner(object): |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 55 | """Class to sign the apk files and other files in an apex payload image and repack the apex""" |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 56 | |
Melisa Carranza Zúñiga | 8e3198a | 2022-04-13 16:23:45 +0000 | [diff] [blame] | 57 | def __init__(self, apex_path, key_passwords, codename_to_api_level_map, avbtool=None, sign_tool=None): |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 58 | self.apex_path = apex_path |
Oleh Cherpak | e555ab1 | 2020-10-05 17:04:59 +0300 | [diff] [blame] | 59 | if not key_passwords: |
| 60 | self.key_passwords = dict() |
| 61 | else: |
| 62 | self.key_passwords = key_passwords |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 63 | self.codename_to_api_level_map = codename_to_api_level_map |
Kelvin Zhang | dd833dc | 2020-08-21 14:13:13 -0400 | [diff] [blame] | 64 | self.debugfs_path = os.path.join( |
| 65 | OPTIONS.search_path, "bin", "debugfs_static") |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 66 | self.fsckerofs_path = os.path.join( |
| 67 | OPTIONS.search_path, "bin", "fsck.erofs") |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 68 | self.avbtool = avbtool if avbtool else "avbtool" |
| 69 | self.sign_tool = sign_tool |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 70 | |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 71 | def ProcessApexFile(self, apk_keys, payload_key, signing_args=None, is_sepolicy=False): |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 72 | """Scans and signs the payload files and repack the apex |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 73 | |
| 74 | Args: |
| 75 | apk_keys: A dict that holds the signing keys for apk files. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 76 | |
| 77 | Returns: |
| 78 | The repacked apex file containing the signed apk files. |
| 79 | """ |
Kelvin Zhang | dd833dc | 2020-08-21 14:13:13 -0400 | [diff] [blame] | 80 | if not os.path.exists(self.debugfs_path): |
Kelvin Zhang | d6b799a | 2020-08-19 14:54:42 -0400 | [diff] [blame] | 81 | raise ApexSigningError( |
| 82 | "Couldn't find location of debugfs_static: " + |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 83 | "Path {} does not exist. ".format(self.debugfs_path) + |
Kelvin Zhang | d6b799a | 2020-08-19 14:54:42 -0400 | [diff] [blame] | 84 | "Make sure bin/debugfs_static can be found in -p <path>") |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 85 | list_cmd = ['deapexer', '--debugfs_path', self.debugfs_path, |
Dennis Shen | a8d1143 | 2022-11-07 21:37:06 +0000 | [diff] [blame] | 86 | 'list', self.apex_path] |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 87 | entries_names = common.RunAndCheckOutput(list_cmd).split() |
| 88 | apk_entries = [name for name in entries_names if name.endswith('.apk')] |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 89 | sepolicy_entries = [] |
| 90 | if is_sepolicy: |
| 91 | sepolicy_entries = [name for name in entries_names if |
| 92 | name.startswith('./etc/SEPolicy') and name.endswith('.zip')] |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 93 | |
| 94 | # No need to sign and repack, return the original apex path. |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 95 | if not apk_entries and not sepolicy_entries and self.sign_tool is None: |
Melisa Carranza Zúñiga | 8e3198a | 2022-04-13 16:23:45 +0000 | [diff] [blame] | 96 | logger.info('No apk file to sign in %s', self.apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 97 | return self.apex_path |
| 98 | |
| 99 | for entry in apk_entries: |
| 100 | apk_name = os.path.basename(entry) |
| 101 | if apk_name not in apk_keys: |
| 102 | raise ApexSigningError('Failed to find signing keys for apk file {} in' |
| 103 | ' apex {}. Use "-e <apkname>=" to specify a key' |
| 104 | .format(entry, self.apex_path)) |
| 105 | if not any(dirname in entry for dirname in ['app/', 'priv-app/', |
| 106 | 'overlay/']): |
| 107 | logger.warning('Apk path does not contain the intended directory name:' |
| 108 | ' %s', entry) |
| 109 | |
Melisa Carranza Zúñiga | 8e3198a | 2022-04-13 16:23:45 +0000 | [diff] [blame] | 110 | payload_dir, has_signed_content = self.ExtractApexPayloadAndSignContents( |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 111 | apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 112 | if not has_signed_content: |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 113 | logger.info('No contents have been signed in %s', self.apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 114 | return self.apex_path |
| 115 | |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 116 | return self.RepackApexPayload(payload_dir, payload_key, signing_args) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 117 | |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 118 | def ExtractApexPayloadAndSignContents(self, apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args): |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 119 | """Extracts the payload image and signs the containing apk files.""" |
Kelvin Zhang | dd833dc | 2020-08-21 14:13:13 -0400 | [diff] [blame] | 120 | if not os.path.exists(self.debugfs_path): |
| 121 | raise ApexSigningError( |
| 122 | "Couldn't find location of debugfs_static: " + |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 123 | "Path {} does not exist. ".format(self.debugfs_path) + |
Kelvin Zhang | dd833dc | 2020-08-21 14:13:13 -0400 | [diff] [blame] | 124 | "Make sure bin/debugfs_static can be found in -p <path>") |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 125 | if not os.path.exists(self.fsckerofs_path): |
| 126 | raise ApexSigningError( |
| 127 | "Couldn't find location of fsck.erofs: " + |
| 128 | "Path {} does not exist. ".format(self.fsckerofs_path) + |
| 129 | "Make sure bin/fsck.erofs can be found in -p <path>") |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 130 | payload_dir = common.MakeTempDir() |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 131 | extract_cmd = ['deapexer', '--debugfs_path', self.debugfs_path, |
Dennis Shen | a8d1143 | 2022-11-07 21:37:06 +0000 | [diff] [blame] | 132 | '--fsckerofs_path', self.fsckerofs_path, |
Jooyung Han | 6294902 | 2023-06-14 15:16:34 +0900 | [diff] [blame^] | 133 | 'extract', |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 134 | self.apex_path, payload_dir] |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 135 | common.RunAndCheckOutput(extract_cmd) |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 136 | assert os.path.exists(self.apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 137 | |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 138 | has_signed_content = False |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 139 | for entry in apk_entries: |
| 140 | apk_path = os.path.join(payload_dir, entry) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 141 | |
| 142 | key_name = apk_keys.get(os.path.basename(entry)) |
| 143 | if key_name in common.SPECIAL_CERT_STRINGS: |
| 144 | logger.info('Not signing: %s due to special cert string', apk_path) |
| 145 | continue |
| 146 | |
| 147 | logger.info('Signing apk file %s in apex %s', apk_path, self.apex_path) |
| 148 | # Rename the unsigned apk and overwrite the original apk path with the |
| 149 | # signed apk file. |
| 150 | unsigned_apk = common.MakeTempFile() |
| 151 | os.rename(apk_path, unsigned_apk) |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 152 | common.SignFile( |
| 153 | unsigned_apk, apk_path, key_name, self.key_passwords.get(key_name), |
| 154 | codename_to_api_level_map=self.codename_to_api_level_map) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 155 | has_signed_content = True |
| 156 | |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 157 | for entry in sepolicy_entries: |
| 158 | sepolicy_path = os.path.join(payload_dir, entry) |
| 159 | |
| 160 | if not 'etc' in entry: |
| 161 | logger.warning('Sepolicy path does not contain the intended directory name etc:' |
| 162 | ' %s', entry) |
| 163 | |
| 164 | key_name = apk_keys.get(os.path.basename(entry)) |
| 165 | if key_name is None: |
| 166 | logger.warning('Failed to find signing keys for {} in' |
| 167 | ' apex {}, payload key will be used instead.' |
| 168 | ' Use "-e <name>=" to specify a key' |
| 169 | .format(entry, self.apex_path)) |
| 170 | key_name = payload_key |
| 171 | |
| 172 | if key_name in common.SPECIAL_CERT_STRINGS: |
| 173 | logger.info('Not signing: %s due to special cert string', sepolicy_path) |
| 174 | continue |
| 175 | |
| 176 | if OPTIONS.sign_sepolicy_path is not None: |
| 177 | sig_path = os.path.join(payload_dir, sepolicy_path + '.sig') |
| 178 | fsv_sig_path = os.path.join(payload_dir, sepolicy_path + '.fsv_sig') |
| 179 | old_sig = common.MakeTempFile() |
| 180 | old_fsv_sig = common.MakeTempFile() |
| 181 | os.rename(sig_path, old_sig) |
| 182 | os.rename(fsv_sig_path, old_fsv_sig) |
| 183 | |
| 184 | logger.info('Signing sepolicy file %s in apex %s', sepolicy_path, self.apex_path) |
| 185 | if common.SignSePolicy(sepolicy_path, key_name, self.key_passwords.get(key_name)): |
| 186 | has_signed_content = True |
| 187 | |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 188 | if self.sign_tool: |
Jooyung Han | 8caba5e | 2021-10-27 03:58:09 +0900 | [diff] [blame] | 189 | logger.info('Signing payload contents in apex %s with %s', self.apex_path, self.sign_tool) |
Jooyung Han | 39259ec | 2022-02-07 15:56:53 +0900 | [diff] [blame] | 190 | # Pass avbtool to the custom signing tool |
| 191 | cmd = [self.sign_tool, '--avbtool', self.avbtool] |
| 192 | # Pass signing_args verbatim which will be forwarded to avbtool (e.g. --signing_helper=...) |
| 193 | if signing_args: |
| 194 | cmd.extend(['--signing_args', '"{}"'.format(signing_args)]) |
| 195 | cmd.extend([payload_key, payload_dir]) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 196 | common.RunAndCheckOutput(cmd) |
| 197 | has_signed_content = True |
| 198 | |
| 199 | return payload_dir, has_signed_content |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 200 | |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 201 | def RepackApexPayload(self, payload_dir, payload_key, signing_args=None): |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 202 | """Rebuilds the apex file with the updated payload directory.""" |
| 203 | apex_dir = common.MakeTempDir() |
| 204 | # Extract the apex file and reuse its meta files as repack parameters. |
| 205 | common.UnzipToDir(self.apex_path, apex_dir) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 206 | arguments_dict = { |
| 207 | 'manifest': os.path.join(apex_dir, 'apex_manifest.pb'), |
| 208 | 'build_info': os.path.join(apex_dir, 'apex_build_info.pb'), |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 209 | 'key': payload_key, |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 210 | } |
| 211 | for filename in arguments_dict.values(): |
| 212 | assert os.path.exists(filename), 'file {} not found'.format(filename) |
| 213 | |
| 214 | # The repack process will add back these files later in the payload image. |
| 215 | for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']: |
| 216 | path = os.path.join(payload_dir, name) |
| 217 | if os.path.isfile(path): |
| 218 | os.remove(path) |
| 219 | elif os.path.isdir(path): |
Baligh Uddin | be2d7d0 | 2022-02-26 02:34:06 +0000 | [diff] [blame] | 220 | shutil.rmtree(path, ignore_errors=True) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 221 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 222 | # TODO(xunchang) the signing process can be improved by using |
| 223 | # '--unsigned_payload_only'. But we need to parse the vbmeta earlier for |
| 224 | # the signing arguments, e.g. algorithm, salt, etc. |
| 225 | payload_img = os.path.join(apex_dir, APEX_PAYLOAD_IMAGE) |
| 226 | generate_image_cmd = ['apexer', '--force', '--payload_only', |
| 227 | '--do_not_check_keyname', '--apexer_tool_path', |
| 228 | os.getenv('PATH')] |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 229 | for key, val in arguments_dict.items(): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 230 | generate_image_cmd.extend(['--' + key, val]) |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 231 | |
| 232 | # Add quote to the signing_args as we will pass |
| 233 | # --signing_args "--signing_helper_with_files=%path" to apexer |
| 234 | if signing_args: |
Kelvin Zhang | d6b799a | 2020-08-19 14:54:42 -0400 | [diff] [blame] | 235 | generate_image_cmd.extend( |
| 236 | ['--signing_args', '"{}"'.format(signing_args)]) |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 237 | |
Tianjie Xu | 83bd55c | 2020-01-29 11:37:43 -0800 | [diff] [blame] | 238 | # optional arguments for apex repacking |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 239 | manifest_json = os.path.join(apex_dir, 'apex_manifest.json') |
| 240 | if os.path.exists(manifest_json): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 241 | generate_image_cmd.extend(['--manifest_json', manifest_json]) |
| 242 | generate_image_cmd.extend([payload_dir, payload_img]) |
Tianjie Xu | cea6ad1 | 2020-01-30 17:12:05 -0800 | [diff] [blame] | 243 | if OPTIONS.verbose: |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 244 | generate_image_cmd.append('-v') |
| 245 | common.RunAndCheckOutput(generate_image_cmd) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 246 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 247 | # Add the payload image back to the apex file. |
| 248 | common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 249 | with zipfile.ZipFile(self.apex_path, 'a', allowZip64=True) as output_apex: |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 250 | common.ZipWrite(output_apex, payload_img, APEX_PAYLOAD_IMAGE, |
| 251 | compress_type=zipfile.ZIP_STORED) |
| 252 | return self.apex_path |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 253 | |
| 254 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 255 | def SignApexPayload(avbtool, payload_file, payload_key_path, payload_key_name, |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 256 | algorithm, salt, hash_algorithm, no_hashtree, signing_args=None): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 257 | """Signs a given payload_file with the payload key.""" |
| 258 | # Add the new footer. Old footer, if any, will be replaced by avbtool. |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 259 | cmd = [avbtool, 'add_hashtree_footer', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 260 | '--do_not_generate_fec', |
| 261 | '--algorithm', algorithm, |
| 262 | '--key', payload_key_path, |
| 263 | '--prop', 'apex.key:{}'.format(payload_key_name), |
| 264 | '--image', payload_file, |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 265 | '--salt', salt, |
| 266 | '--hash_algorithm', hash_algorithm] |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 267 | if no_hashtree: |
| 268 | cmd.append('--no_hashtree') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 269 | if signing_args: |
| 270 | cmd.extend(shlex.split(signing_args)) |
| 271 | |
| 272 | try: |
| 273 | common.RunAndCheckOutput(cmd) |
| 274 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 275 | raise ApexSigningError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 276 | 'Failed to sign APEX payload {} with {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 277 | payload_file, payload_key_path, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 278 | |
| 279 | # Verify the signed payload image with specified public key. |
| 280 | logger.info('Verifying %s', payload_file) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 281 | VerifyApexPayload(avbtool, payload_file, payload_key_path, no_hashtree) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 282 | |
| 283 | |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 284 | def VerifyApexPayload(avbtool, payload_file, payload_key, no_hashtree=False): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 285 | """Verifies the APEX payload signature with the given key.""" |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 286 | cmd = [avbtool, 'verify_image', '--image', payload_file, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 287 | '--key', payload_key] |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 288 | if no_hashtree: |
| 289 | cmd.append('--accept_zeroed_hashtree') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 290 | try: |
| 291 | common.RunAndCheckOutput(cmd) |
| 292 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 293 | raise ApexSigningError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 294 | 'Failed to validate payload signing for {} with {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 295 | payload_file, payload_key, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 296 | |
| 297 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 298 | def ParseApexPayloadInfo(avbtool, payload_path): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 299 | """Parses the APEX payload info. |
| 300 | |
| 301 | Args: |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 302 | avbtool: The AVB tool to use. |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 303 | payload_path: The path to the payload image. |
| 304 | |
| 305 | Raises: |
| 306 | ApexInfoError on parsing errors. |
| 307 | |
| 308 | Returns: |
| 309 | A dict that contains payload property-value pairs. The dict should at least |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 310 | contain Algorithm, Salt, Tree Size and apex.key. |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 311 | """ |
| 312 | if not os.path.exists(payload_path): |
| 313 | raise ApexInfoError('Failed to find image: {}'.format(payload_path)) |
| 314 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 315 | cmd = [avbtool, 'info_image', '--image', payload_path] |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 316 | try: |
| 317 | output = common.RunAndCheckOutput(cmd) |
| 318 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 319 | raise ApexInfoError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 320 | 'Failed to get APEX payload info for {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 321 | payload_path, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 322 | |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 323 | # Extract the Algorithm / Hash Algorithm / Salt / Prop info / Tree size from |
| 324 | # payload (i.e. an image signed with avbtool). For example, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 325 | # Algorithm: SHA256_RSA4096 |
| 326 | PAYLOAD_INFO_PATTERN = ( |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 327 | r'^\s*(?P<key>Algorithm|Hash Algorithm|Salt|Prop|Tree Size)\:\s*(?P<value>.*?)$') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 328 | payload_info_matcher = re.compile(PAYLOAD_INFO_PATTERN) |
| 329 | |
| 330 | payload_info = {} |
| 331 | for line in output.split('\n'): |
| 332 | line_info = payload_info_matcher.match(line) |
| 333 | if not line_info: |
| 334 | continue |
| 335 | |
| 336 | key, value = line_info.group('key'), line_info.group('value') |
| 337 | |
| 338 | if key == 'Prop': |
| 339 | # Further extract the property key-value pair, from a 'Prop:' line. For |
| 340 | # example, |
| 341 | # Prop: apex.key -> 'com.android.runtime' |
| 342 | # Note that avbtool writes single or double quotes around values. |
| 343 | PROPERTY_DESCRIPTOR_PATTERN = r'^\s*(?P<key>.*?)\s->\s*(?P<value>.*?)$' |
| 344 | |
| 345 | prop_matcher = re.compile(PROPERTY_DESCRIPTOR_PATTERN) |
| 346 | prop = prop_matcher.match(value) |
| 347 | if not prop: |
| 348 | raise ApexInfoError( |
| 349 | 'Failed to parse prop string {}'.format(value)) |
| 350 | |
| 351 | prop_key, prop_value = prop.group('key'), prop.group('value') |
| 352 | if prop_key == 'apex.key': |
| 353 | # avbtool dumps the prop value with repr(), which contains single / |
| 354 | # double quotes that we don't want. |
| 355 | payload_info[prop_key] = prop_value.strip('\"\'') |
| 356 | |
| 357 | else: |
| 358 | payload_info[key] = value |
| 359 | |
Ivan Lozano | b021b2a | 2020-07-28 09:31:06 -0400 | [diff] [blame] | 360 | # Validation check. |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 361 | for key in ('Algorithm', 'Salt', 'apex.key', 'Hash Algorithm'): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 362 | if key not in payload_info: |
| 363 | raise ApexInfoError( |
| 364 | 'Failed to find {} prop in {}'.format(key, payload_path)) |
| 365 | |
| 366 | return payload_info |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 367 | |
| 368 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 369 | def SignUncompressedApex(avbtool, apex_file, payload_key, container_key, |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 370 | container_pw, apk_keys, codename_to_api_level_map, |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 371 | no_hashtree, signing_args=None, sign_tool=None, |
| 372 | is_sepolicy=False): |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 373 | """Signs the current uncompressed APEX with the given payload/container keys. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 374 | |
| 375 | Args: |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 376 | apex_file: Uncompressed APEX file. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 377 | payload_key: The path to payload signing key (w/ extension). |
| 378 | container_key: The path to container signing key (w/o extension). |
| 379 | container_pw: The matching password of the container_key, or None. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 380 | apk_keys: A dict that holds the signing keys for apk files. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 381 | codename_to_api_level_map: A dict that maps from codename to API level. |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 382 | no_hashtree: Don't include hashtree in the signed APEX. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 383 | signing_args: Additional args to be passed to the payload signer. |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 384 | sign_tool: A tool to sign the contents of the APEX. |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 385 | is_sepolicy: Indicates if the apex is a sepolicy.apex |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 386 | |
| 387 | Returns: |
| 388 | The path to the signed APEX file. |
| 389 | """ |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 390 | # 1. Extract the apex payload image and sign the files (e.g. APKs). Repack |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 391 | # the apex file after signing. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 392 | apk_signer = ApexApkSigner(apex_file, container_pw, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 393 | codename_to_api_level_map, |
Melisa Carranza Zúñiga | 8e3198a | 2022-04-13 16:23:45 +0000 | [diff] [blame] | 394 | avbtool, sign_tool) |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 395 | apex_file = apk_signer.ProcessApexFile( |
| 396 | apk_keys, payload_key, signing_args, is_sepolicy) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 397 | |
| 398 | # 2a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 399 | # payload_key. |
| 400 | payload_dir = common.MakeTempDir(prefix='apex-payload-') |
| 401 | with zipfile.ZipFile(apex_file) as apex_fd: |
| 402 | payload_file = apex_fd.extract(APEX_PAYLOAD_IMAGE, payload_dir) |
Baligh Uddin | 1588128 | 2019-08-25 12:01:44 -0700 | [diff] [blame] | 403 | zip_items = apex_fd.namelist() |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 404 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 405 | payload_info = ParseApexPayloadInfo(avbtool, payload_file) |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 406 | if no_hashtree is None: |
| 407 | no_hashtree = payload_info.get("Tree Size", 0) == 0 |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 408 | SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 409 | avbtool, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 410 | payload_file, |
| 411 | payload_key, |
| 412 | payload_info['apex.key'], |
| 413 | payload_info['Algorithm'], |
| 414 | payload_info['Salt'], |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 415 | payload_info['Hash Algorithm'], |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 416 | no_hashtree, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 417 | signing_args) |
| 418 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 419 | # 2b. Update the embedded payload public key. |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 420 | payload_public_key = common.ExtractAvbPublicKey(avbtool, payload_key) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 421 | common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE) |
Baligh Uddin | 1588128 | 2019-08-25 12:01:44 -0700 | [diff] [blame] | 422 | if APEX_PUBKEY in zip_items: |
| 423 | common.ZipDelete(apex_file, APEX_PUBKEY) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 424 | apex_zip = zipfile.ZipFile(apex_file, 'a', allowZip64=True) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 425 | common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE) |
| 426 | common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY) |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 427 | common.ZipClose(apex_zip) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 428 | |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 429 | # 3. Sign the APEX container with container_key. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 430 | signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex') |
| 431 | |
| 432 | # Specify the 4K alignment when calling SignApk. |
| 433 | extra_signapk_args = OPTIONS.extra_signapk_args[:] |
Jooyung Han | ebe9afe | 2021-07-12 13:23:52 +0900 | [diff] [blame] | 434 | extra_signapk_args.extend(['-a', '4096', '--align-file-size']) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 435 | |
Nikita Ioffe | c3fdfed | 2021-01-11 23:50:31 +0000 | [diff] [blame] | 436 | password = container_pw.get(container_key) if container_pw else None |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 437 | common.SignFile( |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 438 | apex_file, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 439 | signed_apex, |
| 440 | container_key, |
Nikita Ioffe | c3fdfed | 2021-01-11 23:50:31 +0000 | [diff] [blame] | 441 | password, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 442 | codename_to_api_level_map=codename_to_api_level_map, |
| 443 | extra_signapk_args=extra_signapk_args) |
| 444 | |
| 445 | return signed_apex |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 446 | |
| 447 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 448 | def SignCompressedApex(avbtool, apex_file, payload_key, container_key, |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 449 | container_pw, apk_keys, codename_to_api_level_map, |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 450 | no_hashtree, signing_args=None, sign_tool=None, |
| 451 | is_sepolicy=False): |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 452 | """Signs the current compressed APEX with the given payload/container keys. |
| 453 | |
| 454 | Args: |
| 455 | apex_file: Raw uncompressed APEX data. |
| 456 | payload_key: The path to payload signing key (w/ extension). |
| 457 | container_key: The path to container signing key (w/o extension). |
| 458 | container_pw: The matching password of the container_key, or None. |
| 459 | apk_keys: A dict that holds the signing keys for apk files. |
| 460 | codename_to_api_level_map: A dict that maps from codename to API level. |
| 461 | no_hashtree: Don't include hashtree in the signed APEX. |
| 462 | signing_args: Additional args to be passed to the payload signer. |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 463 | is_sepolicy: Indicates if the apex is a sepolicy.apex |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 464 | |
| 465 | Returns: |
| 466 | The path to the signed APEX file. |
| 467 | """ |
| 468 | debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static') |
| 469 | |
| 470 | # 1. Decompress original_apex inside compressed apex. |
| 471 | original_apex_file = common.MakeTempFile(prefix='original-apex-', |
| 472 | suffix='.apex') |
| 473 | # Decompression target path should not exist |
| 474 | os.remove(original_apex_file) |
| 475 | common.RunAndCheckOutput(['deapexer', '--debugfs_path', debugfs_path, |
| 476 | 'decompress', '--input', apex_file, |
| 477 | '--output', original_apex_file]) |
| 478 | |
| 479 | # 2. Sign original_apex |
| 480 | signed_original_apex_file = SignUncompressedApex( |
| 481 | avbtool, |
| 482 | original_apex_file, |
| 483 | payload_key, |
| 484 | container_key, |
| 485 | container_pw, |
| 486 | apk_keys, |
| 487 | codename_to_api_level_map, |
| 488 | no_hashtree, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 489 | signing_args, |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 490 | sign_tool, |
| 491 | is_sepolicy) |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 492 | |
| 493 | # 3. Compress signed original apex. |
| 494 | compressed_apex_file = common.MakeTempFile(prefix='apex-container-', |
| 495 | suffix='.capex') |
| 496 | common.RunAndCheckOutput(['apex_compression_tool', |
| 497 | 'compress', |
| 498 | '--apex_compression_tool_path', os.getenv('PATH'), |
| 499 | '--input', signed_original_apex_file, |
| 500 | '--output', compressed_apex_file]) |
| 501 | |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 502 | # 4. Sign the APEX container with container_key. |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 503 | signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.capex') |
| 504 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 505 | password = container_pw.get(container_key) if container_pw else None |
| 506 | common.SignFile( |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 507 | compressed_apex_file, |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 508 | signed_apex, |
| 509 | container_key, |
| 510 | password, |
| 511 | codename_to_api_level_map=codename_to_api_level_map, |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 512 | extra_signapk_args=OPTIONS.extra_signapk_args) |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 513 | |
| 514 | return signed_apex |
| 515 | |
| 516 | |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 517 | def SignApex(avbtool, apex_data, payload_key, container_key, container_pw, |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 518 | apk_keys, codename_to_api_level_map, no_hashtree, |
| 519 | signing_args=None, sign_tool=None, is_sepolicy=False): |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 520 | """Signs the current APEX with the given payload/container keys. |
| 521 | |
| 522 | Args: |
| 523 | apex_file: Path to apex file path. |
| 524 | payload_key: The path to payload signing key (w/ extension). |
| 525 | container_key: The path to container signing key (w/o extension). |
| 526 | container_pw: The matching password of the container_key, or None. |
| 527 | apk_keys: A dict that holds the signing keys for apk files. |
| 528 | codename_to_api_level_map: A dict that maps from codename to API level. |
| 529 | no_hashtree: Don't include hashtree in the signed APEX. |
| 530 | signing_args: Additional args to be passed to the payload signer. |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 531 | is_sepolicy: Indicates if the apex is a sepolicy.apex |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 532 | |
| 533 | Returns: |
| 534 | The path to the signed APEX file. |
| 535 | """ |
| 536 | apex_file = common.MakeTempFile(prefix='apex-container-', suffix='.apex') |
| 537 | with open(apex_file, 'wb') as output_fp: |
| 538 | output_fp.write(apex_data) |
| 539 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 540 | debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static') |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 541 | cmd = ['deapexer', '--debugfs_path', debugfs_path, |
| 542 | 'info', '--print-type', apex_file] |
| 543 | |
| 544 | try: |
| 545 | apex_type = common.RunAndCheckOutput(cmd).strip() |
| 546 | if apex_type == 'UNCOMPRESSED': |
| 547 | return SignUncompressedApex( |
| 548 | avbtool, |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 549 | apex_file, |
| 550 | payload_key=payload_key, |
| 551 | container_key=container_key, |
Kelvin Zhang | 137807d | 2022-09-14 15:02:46 -0700 | [diff] [blame] | 552 | container_pw=container_pw, |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 553 | codename_to_api_level_map=codename_to_api_level_map, |
| 554 | no_hashtree=no_hashtree, |
| 555 | apk_keys=apk_keys, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 556 | signing_args=signing_args, |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 557 | sign_tool=sign_tool, |
| 558 | is_sepolicy=is_sepolicy) |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 559 | elif apex_type == 'COMPRESSED': |
| 560 | return SignCompressedApex( |
| 561 | avbtool, |
| 562 | apex_file, |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 563 | payload_key=payload_key, |
| 564 | container_key=container_key, |
Kelvin Zhang | 137807d | 2022-09-14 15:02:46 -0700 | [diff] [blame] | 565 | container_pw=container_pw, |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 566 | codename_to_api_level_map=codename_to_api_level_map, |
| 567 | no_hashtree=no_hashtree, |
| 568 | apk_keys=apk_keys, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 569 | signing_args=signing_args, |
Melisa Carranza Zuniga | e0a977a | 2022-06-16 18:44:27 +0200 | [diff] [blame] | 570 | sign_tool=sign_tool, |
| 571 | is_sepolicy=is_sepolicy) |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 572 | else: |
| 573 | # TODO(b/172912232): support signing compressed apex |
| 574 | raise ApexInfoError('Unsupported apex type {}'.format(apex_type)) |
| 575 | |
| 576 | except common.ExternalError as e: |
| 577 | raise ApexInfoError( |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 578 | 'Failed to get type for {}:\n{}'.format(apex_file, e)) |
| 579 | |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 580 | |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 581 | def GetApexInfoFromTargetFiles(input_file, partition, compressed_only=True): |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 582 | """ |
| 583 | Get information about system APEX stored in the input_file zip |
| 584 | |
| 585 | Args: |
| 586 | input_file: The filename of the target build target-files zip or directory. |
| 587 | |
| 588 | Return: |
| 589 | A list of ota_metadata_pb2.ApexInfo() populated using the APEX stored in |
| 590 | /system partition of the input_file |
| 591 | """ |
| 592 | |
| 593 | # Extract the apex files so that we can run checks on them |
| 594 | if not isinstance(input_file, str): |
| 595 | raise RuntimeError("must pass filepath to target-files zip or directory") |
| 596 | |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 597 | apex_subdir = os.path.join(partition.upper(), 'apex') |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 598 | if os.path.isdir(input_file): |
| 599 | tmp_dir = input_file |
| 600 | else: |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 601 | tmp_dir = UnzipTemp(input_file, [os.path.join(apex_subdir, '*')]) |
| 602 | target_dir = os.path.join(tmp_dir, apex_subdir) |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 603 | |
Daniel Norman | b4b07ab | 2021-02-17 13:22:21 -0800 | [diff] [blame] | 604 | # Partial target-files packages for vendor-only builds may not contain |
| 605 | # a system apex directory. |
| 606 | if not os.path.exists(target_dir): |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 607 | logger.info('No APEX directory at path: %s', target_dir) |
Daniel Norman | b4b07ab | 2021-02-17 13:22:21 -0800 | [diff] [blame] | 608 | return [] |
| 609 | |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 610 | apex_infos = [] |
Kelvin Zhang | c72718c | 2021-01-27 14:17:14 -0500 | [diff] [blame] | 611 | |
| 612 | debugfs_path = "debugfs" |
| 613 | if OPTIONS.search_path: |
| 614 | debugfs_path = os.path.join(OPTIONS.search_path, "bin", "debugfs_static") |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 615 | |
Kelvin Zhang | c72718c | 2021-01-27 14:17:14 -0500 | [diff] [blame] | 616 | deapexer = 'deapexer' |
| 617 | if OPTIONS.search_path: |
Kelvin Zhang | 05a3f68 | 2021-01-29 14:38:24 -0500 | [diff] [blame] | 618 | deapexer_path = os.path.join(OPTIONS.search_path, "bin", "deapexer") |
Kelvin Zhang | c72718c | 2021-01-27 14:17:14 -0500 | [diff] [blame] | 619 | if os.path.isfile(deapexer_path): |
| 620 | deapexer = deapexer_path |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 621 | |
Håkan Kvist | 01e3819 | 2023-04-13 21:49:19 +0200 | [diff] [blame] | 622 | for apex_filename in sorted(os.listdir(target_dir)): |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 623 | apex_filepath = os.path.join(target_dir, apex_filename) |
| 624 | if not os.path.isfile(apex_filepath) or \ |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 625 | not zipfile.is_zipfile(apex_filepath): |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 626 | logger.info("Skipping %s because it's not a zipfile", apex_filepath) |
| 627 | continue |
| 628 | apex_info = ota_metadata_pb2.ApexInfo() |
| 629 | # Open the apex file to retrieve information |
| 630 | manifest = apex_manifest.fromApex(apex_filepath) |
| 631 | apex_info.package_name = manifest.name |
| 632 | apex_info.version = manifest.version |
| 633 | # Check if the file is compressed or not |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 634 | apex_type = RunAndCheckOutput([ |
| 635 | deapexer, "--debugfs_path", debugfs_path, |
| 636 | 'info', '--print-type', apex_filepath]).rstrip() |
| 637 | if apex_type == 'COMPRESSED': |
| 638 | apex_info.is_compressed = True |
| 639 | elif apex_type == 'UNCOMPRESSED': |
| 640 | apex_info.is_compressed = False |
| 641 | else: |
| 642 | raise RuntimeError('Not an APEX file: ' + apex_type) |
| 643 | |
| 644 | # Decompress compressed APEX to determine its size |
| 645 | if apex_info.is_compressed: |
| 646 | decompressed_file_path = MakeTempFile(prefix="decompressed-", |
| 647 | suffix=".apex") |
| 648 | # Decompression target path should not exist |
| 649 | os.remove(decompressed_file_path) |
| 650 | RunAndCheckOutput([deapexer, 'decompress', '--input', apex_filepath, |
| 651 | '--output', decompressed_file_path]) |
| 652 | apex_info.decompressed_size = os.path.getsize(decompressed_file_path) |
| 653 | |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 654 | if not compressed_only or apex_info.is_compressed: |
Kelvin Zhang | c72718c | 2021-01-27 14:17:14 -0500 | [diff] [blame] | 655 | apex_infos.append(apex_info) |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 656 | |
| 657 | return apex_infos |