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 | |
| 24 | import common |
| 25 | |
| 26 | logger = logging.getLogger(__name__) |
| 27 | |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 28 | OPTIONS = common.OPTIONS |
| 29 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 30 | APEX_PAYLOAD_IMAGE = 'apex_payload.img' |
| 31 | |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 32 | |
| 33 | class ApexInfoError(Exception): |
| 34 | """An Exception raised during Apex Information command.""" |
| 35 | |
| 36 | def __init__(self, message): |
| 37 | Exception.__init__(self, message) |
| 38 | |
| 39 | |
| 40 | class ApexSigningError(Exception): |
| 41 | """An Exception raised during Apex Payload signing.""" |
| 42 | |
| 43 | def __init__(self, message): |
| 44 | Exception.__init__(self, message) |
| 45 | |
| 46 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 47 | class ApexApkSigner(object): |
| 48 | """Class to sign the apk files in a apex payload image and repack the apex""" |
| 49 | |
| 50 | def __init__(self, apex_path, key_passwords, codename_to_api_level_map): |
| 51 | self.apex_path = apex_path |
| 52 | self.key_passwords = key_passwords |
| 53 | self.codename_to_api_level_map = codename_to_api_level_map |
| 54 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 55 | def ProcessApexFile(self, apk_keys, payload_key): |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 56 | """Scans and signs the apk files and repack the apex |
| 57 | |
| 58 | Args: |
| 59 | apk_keys: A dict that holds the signing keys for apk files. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 60 | |
| 61 | Returns: |
| 62 | The repacked apex file containing the signed apk files. |
| 63 | """ |
| 64 | list_cmd = ['deapexer', 'list', self.apex_path] |
| 65 | entries_names = common.RunAndCheckOutput(list_cmd).split() |
| 66 | apk_entries = [name for name in entries_names if name.endswith('.apk')] |
| 67 | |
| 68 | # No need to sign and repack, return the original apex path. |
| 69 | if not apk_entries: |
| 70 | logger.info('No apk file to sign in %s', self.apex_path) |
| 71 | return self.apex_path |
| 72 | |
| 73 | for entry in apk_entries: |
| 74 | apk_name = os.path.basename(entry) |
| 75 | if apk_name not in apk_keys: |
| 76 | raise ApexSigningError('Failed to find signing keys for apk file {} in' |
| 77 | ' apex {}. Use "-e <apkname>=" to specify a key' |
| 78 | .format(entry, self.apex_path)) |
| 79 | if not any(dirname in entry for dirname in ['app/', 'priv-app/', |
| 80 | 'overlay/']): |
| 81 | logger.warning('Apk path does not contain the intended directory name:' |
| 82 | ' %s', entry) |
| 83 | |
| 84 | payload_dir, has_signed_apk = self.ExtractApexPayloadAndSignApks( |
| 85 | apk_entries, apk_keys) |
| 86 | if not has_signed_apk: |
| 87 | logger.info('No apk file has been signed in %s', self.apex_path) |
| 88 | return self.apex_path |
| 89 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 90 | return self.RepackApexPayload(payload_dir, payload_key) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 91 | |
| 92 | def ExtractApexPayloadAndSignApks(self, apk_entries, apk_keys): |
| 93 | """Extracts the payload image and signs the containing apk files.""" |
| 94 | payload_dir = common.MakeTempDir() |
| 95 | extract_cmd = ['deapexer', 'extract', self.apex_path, payload_dir] |
| 96 | common.RunAndCheckOutput(extract_cmd) |
| 97 | |
| 98 | has_signed_apk = False |
| 99 | for entry in apk_entries: |
| 100 | apk_path = os.path.join(payload_dir, entry) |
| 101 | assert os.path.exists(self.apex_path) |
| 102 | |
| 103 | key_name = apk_keys.get(os.path.basename(entry)) |
| 104 | if key_name in common.SPECIAL_CERT_STRINGS: |
| 105 | logger.info('Not signing: %s due to special cert string', apk_path) |
| 106 | continue |
| 107 | |
| 108 | logger.info('Signing apk file %s in apex %s', apk_path, self.apex_path) |
| 109 | # Rename the unsigned apk and overwrite the original apk path with the |
| 110 | # signed apk file. |
| 111 | unsigned_apk = common.MakeTempFile() |
| 112 | os.rename(apk_path, unsigned_apk) |
| 113 | common.SignFile(unsigned_apk, apk_path, key_name, self.key_passwords, |
| 114 | codename_to_api_level_map=self.codename_to_api_level_map) |
| 115 | has_signed_apk = True |
| 116 | return payload_dir, has_signed_apk |
| 117 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 118 | def RepackApexPayload(self, payload_dir, payload_key): |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 119 | """Rebuilds the apex file with the updated payload directory.""" |
| 120 | apex_dir = common.MakeTempDir() |
| 121 | # Extract the apex file and reuse its meta files as repack parameters. |
| 122 | common.UnzipToDir(self.apex_path, apex_dir) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 123 | arguments_dict = { |
| 124 | 'manifest': os.path.join(apex_dir, 'apex_manifest.pb'), |
| 125 | 'build_info': os.path.join(apex_dir, 'apex_build_info.pb'), |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 126 | 'key': payload_key, |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 127 | } |
| 128 | for filename in arguments_dict.values(): |
| 129 | assert os.path.exists(filename), 'file {} not found'.format(filename) |
| 130 | |
| 131 | # The repack process will add back these files later in the payload image. |
| 132 | for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']: |
| 133 | path = os.path.join(payload_dir, name) |
| 134 | if os.path.isfile(path): |
| 135 | os.remove(path) |
| 136 | elif os.path.isdir(path): |
| 137 | shutil.rmtree(path) |
| 138 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 139 | # TODO(xunchang) the signing process can be improved by using |
| 140 | # '--unsigned_payload_only'. But we need to parse the vbmeta earlier for |
| 141 | # the signing arguments, e.g. algorithm, salt, etc. |
| 142 | payload_img = os.path.join(apex_dir, APEX_PAYLOAD_IMAGE) |
| 143 | generate_image_cmd = ['apexer', '--force', '--payload_only', |
| 144 | '--do_not_check_keyname', '--apexer_tool_path', |
| 145 | os.getenv('PATH')] |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 146 | for key, val in arguments_dict.items(): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 147 | generate_image_cmd.extend(['--' + key, val]) |
Tianjie Xu | 83bd55c | 2020-01-29 11:37:43 -0800 | [diff] [blame] | 148 | # optional arguments for apex repacking |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 149 | manifest_json = os.path.join(apex_dir, 'apex_manifest.json') |
| 150 | if os.path.exists(manifest_json): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 151 | generate_image_cmd.extend(['--manifest_json', manifest_json]) |
| 152 | generate_image_cmd.extend([payload_dir, payload_img]) |
Tianjie Xu | cea6ad1 | 2020-01-30 17:12:05 -0800 | [diff] [blame] | 153 | if OPTIONS.verbose: |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 154 | generate_image_cmd.append('-v') |
| 155 | common.RunAndCheckOutput(generate_image_cmd) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 156 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 157 | # Add the payload image back to the apex file. |
| 158 | common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE) |
| 159 | with zipfile.ZipFile(self.apex_path, 'a') as output_apex: |
| 160 | common.ZipWrite(output_apex, payload_img, APEX_PAYLOAD_IMAGE, |
| 161 | compress_type=zipfile.ZIP_STORED) |
| 162 | return self.apex_path |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 163 | |
| 164 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 165 | def SignApexPayload(avbtool, payload_file, payload_key_path, payload_key_name, |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 166 | algorithm, salt, no_hashtree, signing_args=None): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 167 | """Signs a given payload_file with the payload key.""" |
| 168 | # 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] | 169 | cmd = [avbtool, 'add_hashtree_footer', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 170 | '--do_not_generate_fec', |
| 171 | '--algorithm', algorithm, |
| 172 | '--key', payload_key_path, |
| 173 | '--prop', 'apex.key:{}'.format(payload_key_name), |
| 174 | '--image', payload_file, |
| 175 | '--salt', salt] |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 176 | if no_hashtree: |
| 177 | cmd.append('--no_hashtree') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 178 | if signing_args: |
| 179 | cmd.extend(shlex.split(signing_args)) |
| 180 | |
| 181 | try: |
| 182 | common.RunAndCheckOutput(cmd) |
| 183 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 184 | raise ApexSigningError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 185 | 'Failed to sign APEX payload {} with {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 186 | payload_file, payload_key_path, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 187 | |
| 188 | # Verify the signed payload image with specified public key. |
| 189 | logger.info('Verifying %s', payload_file) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 190 | VerifyApexPayload(avbtool, payload_file, payload_key_path, no_hashtree) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 191 | |
| 192 | |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 193 | def VerifyApexPayload(avbtool, payload_file, payload_key, no_hashtree=False): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 194 | """Verifies the APEX payload signature with the given key.""" |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 195 | cmd = [avbtool, 'verify_image', '--image', payload_file, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 196 | '--key', payload_key] |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 197 | if no_hashtree: |
| 198 | cmd.append('--accept_zeroed_hashtree') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 199 | try: |
| 200 | common.RunAndCheckOutput(cmd) |
| 201 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 202 | raise ApexSigningError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 203 | 'Failed to validate payload signing for {} with {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 204 | payload_file, payload_key, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 205 | |
| 206 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 207 | def ParseApexPayloadInfo(avbtool, payload_path): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 208 | """Parses the APEX payload info. |
| 209 | |
| 210 | Args: |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 211 | avbtool: The AVB tool to use. |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 212 | payload_path: The path to the payload image. |
| 213 | |
| 214 | Raises: |
| 215 | ApexInfoError on parsing errors. |
| 216 | |
| 217 | Returns: |
| 218 | 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] | 219 | contain Algorithm, Salt, Tree Size and apex.key. |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 220 | """ |
| 221 | if not os.path.exists(payload_path): |
| 222 | raise ApexInfoError('Failed to find image: {}'.format(payload_path)) |
| 223 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 224 | cmd = [avbtool, 'info_image', '--image', payload_path] |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 225 | try: |
| 226 | output = common.RunAndCheckOutput(cmd) |
| 227 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 228 | raise ApexInfoError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 229 | 'Failed to get APEX payload info for {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 230 | payload_path, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 231 | |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 232 | # Extract the Algorithm / Salt / Prop info / Tree size from payload (i.e. an |
| 233 | # image signed with avbtool). For example, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 234 | # Algorithm: SHA256_RSA4096 |
| 235 | PAYLOAD_INFO_PATTERN = ( |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 236 | r'^\s*(?P<key>Algorithm|Salt|Prop|Tree Size)\:\s*(?P<value>.*?)$') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 237 | payload_info_matcher = re.compile(PAYLOAD_INFO_PATTERN) |
| 238 | |
| 239 | payload_info = {} |
| 240 | for line in output.split('\n'): |
| 241 | line_info = payload_info_matcher.match(line) |
| 242 | if not line_info: |
| 243 | continue |
| 244 | |
| 245 | key, value = line_info.group('key'), line_info.group('value') |
| 246 | |
| 247 | if key == 'Prop': |
| 248 | # Further extract the property key-value pair, from a 'Prop:' line. For |
| 249 | # example, |
| 250 | # Prop: apex.key -> 'com.android.runtime' |
| 251 | # Note that avbtool writes single or double quotes around values. |
| 252 | PROPERTY_DESCRIPTOR_PATTERN = r'^\s*(?P<key>.*?)\s->\s*(?P<value>.*?)$' |
| 253 | |
| 254 | prop_matcher = re.compile(PROPERTY_DESCRIPTOR_PATTERN) |
| 255 | prop = prop_matcher.match(value) |
| 256 | if not prop: |
| 257 | raise ApexInfoError( |
| 258 | 'Failed to parse prop string {}'.format(value)) |
| 259 | |
| 260 | prop_key, prop_value = prop.group('key'), prop.group('value') |
| 261 | if prop_key == 'apex.key': |
| 262 | # avbtool dumps the prop value with repr(), which contains single / |
| 263 | # double quotes that we don't want. |
| 264 | payload_info[prop_key] = prop_value.strip('\"\'') |
| 265 | |
| 266 | else: |
| 267 | payload_info[key] = value |
| 268 | |
| 269 | # Sanity check. |
| 270 | for key in ('Algorithm', 'Salt', 'apex.key'): |
| 271 | if key not in payload_info: |
| 272 | raise ApexInfoError( |
| 273 | 'Failed to find {} prop in {}'.format(key, payload_path)) |
| 274 | |
| 275 | return payload_info |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 276 | |
| 277 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 278 | def SignApex(avbtool, apex_data, payload_key, container_key, container_pw, |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 279 | apk_keys, codename_to_api_level_map, |
| 280 | no_hashtree, signing_args=None): |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 281 | """Signs the current APEX with the given payload/container keys. |
| 282 | |
| 283 | Args: |
| 284 | apex_data: Raw APEX data. |
| 285 | payload_key: The path to payload signing key (w/ extension). |
| 286 | container_key: The path to container signing key (w/o extension). |
| 287 | container_pw: The matching password of the container_key, or None. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 288 | apk_keys: A dict that holds the signing keys for apk files. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 289 | 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] | 290 | no_hashtree: Don't include hashtree in the signed APEX. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 291 | signing_args: Additional args to be passed to the payload signer. |
| 292 | |
| 293 | Returns: |
| 294 | The path to the signed APEX file. |
| 295 | """ |
| 296 | apex_file = common.MakeTempFile(prefix='apex-', suffix='.apex') |
| 297 | with open(apex_file, 'wb') as apex_fp: |
| 298 | apex_fp.write(apex_data) |
| 299 | |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 300 | APEX_PUBKEY = 'apex_pubkey' |
| 301 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 302 | # 1. Extract the apex payload image and sign the containing apk files. Repack |
| 303 | # the apex file after signing. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 304 | apk_signer = ApexApkSigner(apex_file, container_pw, |
| 305 | codename_to_api_level_map) |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 306 | apex_file = apk_signer.ProcessApexFile(apk_keys, payload_key) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 307 | |
| 308 | # 2a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 309 | # payload_key. |
| 310 | payload_dir = common.MakeTempDir(prefix='apex-payload-') |
| 311 | with zipfile.ZipFile(apex_file) as apex_fd: |
| 312 | payload_file = apex_fd.extract(APEX_PAYLOAD_IMAGE, payload_dir) |
Baligh Uddin | 1588128 | 2019-08-25 12:01:44 -0700 | [diff] [blame] | 313 | zip_items = apex_fd.namelist() |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 314 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 315 | payload_info = ParseApexPayloadInfo(avbtool, payload_file) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 316 | SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 317 | avbtool, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 318 | payload_file, |
| 319 | payload_key, |
| 320 | payload_info['apex.key'], |
| 321 | payload_info['Algorithm'], |
| 322 | payload_info['Salt'], |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 323 | no_hashtree, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 324 | signing_args) |
| 325 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 326 | # 2b. Update the embedded payload public key. |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame^] | 327 | payload_public_key = common.ExtractAvbPublicKey(avbtool, payload_key) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 328 | common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE) |
Baligh Uddin | 1588128 | 2019-08-25 12:01:44 -0700 | [diff] [blame] | 329 | if APEX_PUBKEY in zip_items: |
| 330 | common.ZipDelete(apex_file, APEX_PUBKEY) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 331 | apex_zip = zipfile.ZipFile(apex_file, 'a') |
| 332 | common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE) |
| 333 | common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY) |
| 334 | common.ZipClose(apex_zip) |
| 335 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 336 | # 3. Align the files at page boundary (same as in apexer). |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 337 | aligned_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex') |
| 338 | common.RunAndCheckOutput(['zipalign', '-f', '4096', apex_file, aligned_apex]) |
| 339 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 340 | # 4. Sign the APEX container with container_key. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 341 | signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex') |
| 342 | |
| 343 | # Specify the 4K alignment when calling SignApk. |
| 344 | extra_signapk_args = OPTIONS.extra_signapk_args[:] |
| 345 | extra_signapk_args.extend(['-a', '4096']) |
| 346 | |
| 347 | common.SignFile( |
| 348 | aligned_apex, |
| 349 | signed_apex, |
| 350 | container_key, |
| 351 | container_pw, |
| 352 | codename_to_api_level_map=codename_to_api_level_map, |
| 353 | extra_signapk_args=extra_signapk_args) |
| 354 | |
| 355 | return signed_apex |