blob: 194ff5862ba672ed743f67dd879caf2d10ece385 [file] [log] [blame]
Tao Bao1cd59f22019-03-15 15:13:01 -07001#!/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
17import logging
18import os.path
19import re
20import shlex
Tianjie Xu88a759d2020-01-23 10:47:54 -080021import shutil
Tao Baoe7354ba2019-05-09 16:54:15 -070022import zipfile
Tao Bao1cd59f22019-03-15 15:13:01 -070023
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +000024import apex_manifest
Tao Bao1cd59f22019-03-15 15:13:01 -070025import common
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +000026from common import UnzipTemp, RunAndCheckOutput, MakeTempFile, OPTIONS
27
28import ota_metadata_pb2
29
Tao Bao1cd59f22019-03-15 15:13:01 -070030
31logger = logging.getLogger(__name__)
32
Tao Baoe7354ba2019-05-09 16:54:15 -070033OPTIONS = common.OPTIONS
34
Tianjiec180a5d2020-03-23 18:14:09 -070035APEX_PAYLOAD_IMAGE = 'apex_payload.img'
36
Nikita Ioffe36081482021-01-20 01:32:28 +000037APEX_PUBKEY = 'apex_pubkey'
38
Tao Bao1cd59f22019-03-15 15:13:01 -070039
40class ApexInfoError(Exception):
41 """An Exception raised during Apex Information command."""
42
43 def __init__(self, message):
44 Exception.__init__(self, message)
45
46
47class ApexSigningError(Exception):
48 """An Exception raised during Apex Payload signing."""
49
50 def __init__(self, message):
51 Exception.__init__(self, message)
52
53
Tianjie Xu88a759d2020-01-23 10:47:54 -080054class ApexApkSigner(object):
Jooyung Han0f5a41d2021-10-27 03:53:21 +090055 """Class to sign the apk files and other files in an apex payload image and repack the apex"""
Tianjie Xu88a759d2020-01-23 10:47:54 -080056
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +000057 def __init__(self, apex_path, key_passwords, codename_to_api_level_map, avbtool=None, sign_tool=None):
Tianjie Xu88a759d2020-01-23 10:47:54 -080058 self.apex_path = apex_path
Oleh Cherpake555ab12020-10-05 17:04:59 +030059 if not key_passwords:
60 self.key_passwords = dict()
61 else:
62 self.key_passwords = key_passwords
Tianjie Xu88a759d2020-01-23 10:47:54 -080063 self.codename_to_api_level_map = codename_to_api_level_map
Kelvin Zhangdd833dc2020-08-21 14:13:13 -040064 self.debugfs_path = os.path.join(
65 OPTIONS.search_path, "bin", "debugfs_static")
Dennis Shenf58e5482022-10-10 21:19:46 +000066 self.fsckerofs_path = os.path.join(
67 OPTIONS.search_path, "bin", "fsck.erofs")
Dennis Shena8d11432022-11-07 21:37:06 +000068 self.blkid_path = os.path.join(
69 OPTIONS.search_path, "bin", "blkid")
Jooyung Han0f5a41d2021-10-27 03:53:21 +090070 self.avbtool = avbtool if avbtool else "avbtool"
71 self.sign_tool = sign_tool
Tianjie Xu88a759d2020-01-23 10:47:54 -080072
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020073 def ProcessApexFile(self, apk_keys, payload_key, signing_args=None, is_sepolicy=False):
Jooyung Han0f5a41d2021-10-27 03:53:21 +090074 """Scans and signs the payload files and repack the apex
Tianjie Xu88a759d2020-01-23 10:47:54 -080075
76 Args:
77 apk_keys: A dict that holds the signing keys for apk files.
Tianjie Xu88a759d2020-01-23 10:47:54 -080078
79 Returns:
80 The repacked apex file containing the signed apk files.
81 """
Kelvin Zhangdd833dc2020-08-21 14:13:13 -040082 if not os.path.exists(self.debugfs_path):
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040083 raise ApexSigningError(
84 "Couldn't find location of debugfs_static: " +
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +000085 "Path {} does not exist. ".format(self.debugfs_path) +
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040086 "Make sure bin/debugfs_static can be found in -p <path>")
Dennis Shenf58e5482022-10-10 21:19:46 +000087 list_cmd = ['deapexer', '--debugfs_path', self.debugfs_path,
Dennis Shena8d11432022-11-07 21:37:06 +000088 'list', self.apex_path]
Tianjie Xu88a759d2020-01-23 10:47:54 -080089 entries_names = common.RunAndCheckOutput(list_cmd).split()
90 apk_entries = [name for name in entries_names if name.endswith('.apk')]
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020091 sepolicy_entries = []
92 if is_sepolicy:
93 sepolicy_entries = [name for name in entries_names if
94 name.startswith('./etc/SEPolicy') and name.endswith('.zip')]
Tianjie Xu88a759d2020-01-23 10:47:54 -080095
96 # No need to sign and repack, return the original apex path.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020097 if not apk_entries and not sepolicy_entries and self.sign_tool is None:
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +000098 logger.info('No apk file to sign in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -080099 return self.apex_path
100
101 for entry in apk_entries:
102 apk_name = os.path.basename(entry)
103 if apk_name not in apk_keys:
104 raise ApexSigningError('Failed to find signing keys for apk file {} in'
105 ' apex {}. Use "-e <apkname>=" to specify a key'
106 .format(entry, self.apex_path))
107 if not any(dirname in entry for dirname in ['app/', 'priv-app/',
108 'overlay/']):
109 logger.warning('Apk path does not contain the intended directory name:'
110 ' %s', entry)
111
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +0000112 payload_dir, has_signed_content = self.ExtractApexPayloadAndSignContents(
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200113 apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900114 if not has_signed_content:
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200115 logger.info('No contents have been signed in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800116 return self.apex_path
117
Baligh Uddin639b3b72020-03-25 20:50:23 -0700118 return self.RepackApexPayload(payload_dir, payload_key, signing_args)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800119
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200120 def ExtractApexPayloadAndSignContents(self, apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args):
Tianjie Xu88a759d2020-01-23 10:47:54 -0800121 """Extracts the payload image and signs the containing apk files."""
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400122 if not os.path.exists(self.debugfs_path):
123 raise ApexSigningError(
124 "Couldn't find location of debugfs_static: " +
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000125 "Path {} does not exist. ".format(self.debugfs_path) +
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400126 "Make sure bin/debugfs_static can be found in -p <path>")
Dennis Shenf58e5482022-10-10 21:19:46 +0000127 if not os.path.exists(self.fsckerofs_path):
128 raise ApexSigningError(
129 "Couldn't find location of fsck.erofs: " +
130 "Path {} does not exist. ".format(self.fsckerofs_path) +
131 "Make sure bin/fsck.erofs can be found in -p <path>")
Dennis Shena8d11432022-11-07 21:37:06 +0000132 if not os.path.exists(self.blkid_path):
133 raise ApexSigningError(
134 "Couldn't find location of blkid: " +
135 "Path {} does not exist. ".format(self.blkid_path) +
136 "Make sure bin/blkid can be found in -p <path>")
Tianjie Xu88a759d2020-01-23 10:47:54 -0800137 payload_dir = common.MakeTempDir()
Dennis Shenf58e5482022-10-10 21:19:46 +0000138 extract_cmd = ['deapexer', '--debugfs_path', self.debugfs_path,
Dennis Shena8d11432022-11-07 21:37:06 +0000139 '--fsckerofs_path', self.fsckerofs_path,
140 '--blkid_path', self.blkid_path, 'extract',
Dennis Shenf58e5482022-10-10 21:19:46 +0000141 self.apex_path, payload_dir]
Tianjie Xu88a759d2020-01-23 10:47:54 -0800142 common.RunAndCheckOutput(extract_cmd)
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200143 assert os.path.exists(self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800144
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900145 has_signed_content = False
Tianjie Xu88a759d2020-01-23 10:47:54 -0800146 for entry in apk_entries:
147 apk_path = os.path.join(payload_dir, entry)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800148
149 key_name = apk_keys.get(os.path.basename(entry))
150 if key_name in common.SPECIAL_CERT_STRINGS:
151 logger.info('Not signing: %s due to special cert string', apk_path)
152 continue
153
154 logger.info('Signing apk file %s in apex %s', apk_path, self.apex_path)
155 # Rename the unsigned apk and overwrite the original apk path with the
156 # signed apk file.
157 unsigned_apk = common.MakeTempFile()
158 os.rename(apk_path, unsigned_apk)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000159 common.SignFile(
160 unsigned_apk, apk_path, key_name, self.key_passwords.get(key_name),
161 codename_to_api_level_map=self.codename_to_api_level_map)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900162 has_signed_content = True
163
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200164 for entry in sepolicy_entries:
165 sepolicy_path = os.path.join(payload_dir, entry)
166
167 if not 'etc' in entry:
168 logger.warning('Sepolicy path does not contain the intended directory name etc:'
169 ' %s', entry)
170
171 key_name = apk_keys.get(os.path.basename(entry))
172 if key_name is None:
173 logger.warning('Failed to find signing keys for {} in'
174 ' apex {}, payload key will be used instead.'
175 ' Use "-e <name>=" to specify a key'
176 .format(entry, self.apex_path))
177 key_name = payload_key
178
179 if key_name in common.SPECIAL_CERT_STRINGS:
180 logger.info('Not signing: %s due to special cert string', sepolicy_path)
181 continue
182
183 if OPTIONS.sign_sepolicy_path is not None:
184 sig_path = os.path.join(payload_dir, sepolicy_path + '.sig')
185 fsv_sig_path = os.path.join(payload_dir, sepolicy_path + '.fsv_sig')
186 old_sig = common.MakeTempFile()
187 old_fsv_sig = common.MakeTempFile()
188 os.rename(sig_path, old_sig)
189 os.rename(fsv_sig_path, old_fsv_sig)
190
191 logger.info('Signing sepolicy file %s in apex %s', sepolicy_path, self.apex_path)
192 if common.SignSePolicy(sepolicy_path, key_name, self.key_passwords.get(key_name)):
193 has_signed_content = True
194
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900195 if self.sign_tool:
Jooyung Han8caba5e2021-10-27 03:58:09 +0900196 logger.info('Signing payload contents in apex %s with %s', self.apex_path, self.sign_tool)
Jooyung Han39259ec2022-02-07 15:56:53 +0900197 # Pass avbtool to the custom signing tool
198 cmd = [self.sign_tool, '--avbtool', self.avbtool]
199 # Pass signing_args verbatim which will be forwarded to avbtool (e.g. --signing_helper=...)
200 if signing_args:
201 cmd.extend(['--signing_args', '"{}"'.format(signing_args)])
202 cmd.extend([payload_key, payload_dir])
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900203 common.RunAndCheckOutput(cmd)
204 has_signed_content = True
205
206 return payload_dir, has_signed_content
Tianjie Xu88a759d2020-01-23 10:47:54 -0800207
Baligh Uddin639b3b72020-03-25 20:50:23 -0700208 def RepackApexPayload(self, payload_dir, payload_key, signing_args=None):
Tianjie Xu88a759d2020-01-23 10:47:54 -0800209 """Rebuilds the apex file with the updated payload directory."""
210 apex_dir = common.MakeTempDir()
211 # Extract the apex file and reuse its meta files as repack parameters.
212 common.UnzipToDir(self.apex_path, apex_dir)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800213 arguments_dict = {
214 'manifest': os.path.join(apex_dir, 'apex_manifest.pb'),
215 'build_info': os.path.join(apex_dir, 'apex_build_info.pb'),
Tianjie Xu88a759d2020-01-23 10:47:54 -0800216 'key': payload_key,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800217 }
218 for filename in arguments_dict.values():
219 assert os.path.exists(filename), 'file {} not found'.format(filename)
220
221 # The repack process will add back these files later in the payload image.
222 for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']:
223 path = os.path.join(payload_dir, name)
224 if os.path.isfile(path):
225 os.remove(path)
226 elif os.path.isdir(path):
Baligh Uddinbe2d7d02022-02-26 02:34:06 +0000227 shutil.rmtree(path, ignore_errors=True)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800228
Tianjiec180a5d2020-03-23 18:14:09 -0700229 # TODO(xunchang) the signing process can be improved by using
230 # '--unsigned_payload_only'. But we need to parse the vbmeta earlier for
231 # the signing arguments, e.g. algorithm, salt, etc.
232 payload_img = os.path.join(apex_dir, APEX_PAYLOAD_IMAGE)
233 generate_image_cmd = ['apexer', '--force', '--payload_only',
234 '--do_not_check_keyname', '--apexer_tool_path',
235 os.getenv('PATH')]
Tianjie Xu88a759d2020-01-23 10:47:54 -0800236 for key, val in arguments_dict.items():
Tianjiec180a5d2020-03-23 18:14:09 -0700237 generate_image_cmd.extend(['--' + key, val])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700238
239 # Add quote to the signing_args as we will pass
240 # --signing_args "--signing_helper_with_files=%path" to apexer
241 if signing_args:
Kelvin Zhangd6b799a2020-08-19 14:54:42 -0400242 generate_image_cmd.extend(
243 ['--signing_args', '"{}"'.format(signing_args)])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700244
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800245 # optional arguments for apex repacking
Tianjie Xu88a759d2020-01-23 10:47:54 -0800246 manifest_json = os.path.join(apex_dir, 'apex_manifest.json')
247 if os.path.exists(manifest_json):
Tianjiec180a5d2020-03-23 18:14:09 -0700248 generate_image_cmd.extend(['--manifest_json', manifest_json])
249 generate_image_cmd.extend([payload_dir, payload_img])
Tianjie Xucea6ad12020-01-30 17:12:05 -0800250 if OPTIONS.verbose:
Tianjiec180a5d2020-03-23 18:14:09 -0700251 generate_image_cmd.append('-v')
252 common.RunAndCheckOutput(generate_image_cmd)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800253
Tianjiec180a5d2020-03-23 18:14:09 -0700254 # Add the payload image back to the apex file.
255 common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400256 with zipfile.ZipFile(self.apex_path, 'a', allowZip64=True) as output_apex:
Tianjiec180a5d2020-03-23 18:14:09 -0700257 common.ZipWrite(output_apex, payload_img, APEX_PAYLOAD_IMAGE,
258 compress_type=zipfile.ZIP_STORED)
259 return self.apex_path
Tianjie Xu88a759d2020-01-23 10:47:54 -0800260
261
Tao Bao1ac886e2019-06-26 11:58:22 -0700262def SignApexPayload(avbtool, payload_file, payload_key_path, payload_key_name,
Jiyong Parka1887f32020-05-19 23:18:03 +0900263 algorithm, salt, hash_algorithm, no_hashtree, signing_args=None):
Tao Bao1cd59f22019-03-15 15:13:01 -0700264 """Signs a given payload_file with the payload key."""
265 # Add the new footer. Old footer, if any, will be replaced by avbtool.
Tao Bao1ac886e2019-06-26 11:58:22 -0700266 cmd = [avbtool, 'add_hashtree_footer',
Tao Bao1cd59f22019-03-15 15:13:01 -0700267 '--do_not_generate_fec',
268 '--algorithm', algorithm,
269 '--key', payload_key_path,
270 '--prop', 'apex.key:{}'.format(payload_key_name),
271 '--image', payload_file,
Jiyong Parka1887f32020-05-19 23:18:03 +0900272 '--salt', salt,
273 '--hash_algorithm', hash_algorithm]
Tao Bao448004a2019-09-19 07:55:02 -0700274 if no_hashtree:
275 cmd.append('--no_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700276 if signing_args:
277 cmd.extend(shlex.split(signing_args))
278
279 try:
280 common.RunAndCheckOutput(cmd)
281 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700282 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700283 'Failed to sign APEX payload {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700284 payload_file, payload_key_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700285
286 # Verify the signed payload image with specified public key.
287 logger.info('Verifying %s', payload_file)
Tao Bao448004a2019-09-19 07:55:02 -0700288 VerifyApexPayload(avbtool, payload_file, payload_key_path, no_hashtree)
Tao Bao1cd59f22019-03-15 15:13:01 -0700289
290
Tao Bao448004a2019-09-19 07:55:02 -0700291def VerifyApexPayload(avbtool, payload_file, payload_key, no_hashtree=False):
Tao Bao1cd59f22019-03-15 15:13:01 -0700292 """Verifies the APEX payload signature with the given key."""
Tao Bao1ac886e2019-06-26 11:58:22 -0700293 cmd = [avbtool, 'verify_image', '--image', payload_file,
Tao Bao1cd59f22019-03-15 15:13:01 -0700294 '--key', payload_key]
Tao Bao448004a2019-09-19 07:55:02 -0700295 if no_hashtree:
296 cmd.append('--accept_zeroed_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700297 try:
298 common.RunAndCheckOutput(cmd)
299 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700300 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700301 'Failed to validate payload signing for {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700302 payload_file, payload_key, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700303
304
Tao Bao1ac886e2019-06-26 11:58:22 -0700305def ParseApexPayloadInfo(avbtool, payload_path):
Tao Bao1cd59f22019-03-15 15:13:01 -0700306 """Parses the APEX payload info.
307
308 Args:
Tao Bao1ac886e2019-06-26 11:58:22 -0700309 avbtool: The AVB tool to use.
Tao Bao1cd59f22019-03-15 15:13:01 -0700310 payload_path: The path to the payload image.
311
312 Raises:
313 ApexInfoError on parsing errors.
314
315 Returns:
316 A dict that contains payload property-value pairs. The dict should at least
Tao Bao448004a2019-09-19 07:55:02 -0700317 contain Algorithm, Salt, Tree Size and apex.key.
Tao Bao1cd59f22019-03-15 15:13:01 -0700318 """
319 if not os.path.exists(payload_path):
320 raise ApexInfoError('Failed to find image: {}'.format(payload_path))
321
Tao Bao1ac886e2019-06-26 11:58:22 -0700322 cmd = [avbtool, 'info_image', '--image', payload_path]
Tao Bao1cd59f22019-03-15 15:13:01 -0700323 try:
324 output = common.RunAndCheckOutput(cmd)
325 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700326 raise ApexInfoError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700327 'Failed to get APEX payload info for {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700328 payload_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700329
Jiyong Parka1887f32020-05-19 23:18:03 +0900330 # Extract the Algorithm / Hash Algorithm / Salt / Prop info / Tree size from
331 # payload (i.e. an image signed with avbtool). For example,
Tao Bao1cd59f22019-03-15 15:13:01 -0700332 # Algorithm: SHA256_RSA4096
333 PAYLOAD_INFO_PATTERN = (
Jiyong Parka1887f32020-05-19 23:18:03 +0900334 r'^\s*(?P<key>Algorithm|Hash Algorithm|Salt|Prop|Tree Size)\:\s*(?P<value>.*?)$')
Tao Bao1cd59f22019-03-15 15:13:01 -0700335 payload_info_matcher = re.compile(PAYLOAD_INFO_PATTERN)
336
337 payload_info = {}
338 for line in output.split('\n'):
339 line_info = payload_info_matcher.match(line)
340 if not line_info:
341 continue
342
343 key, value = line_info.group('key'), line_info.group('value')
344
345 if key == 'Prop':
346 # Further extract the property key-value pair, from a 'Prop:' line. For
347 # example,
348 # Prop: apex.key -> 'com.android.runtime'
349 # Note that avbtool writes single or double quotes around values.
350 PROPERTY_DESCRIPTOR_PATTERN = r'^\s*(?P<key>.*?)\s->\s*(?P<value>.*?)$'
351
352 prop_matcher = re.compile(PROPERTY_DESCRIPTOR_PATTERN)
353 prop = prop_matcher.match(value)
354 if not prop:
355 raise ApexInfoError(
356 'Failed to parse prop string {}'.format(value))
357
358 prop_key, prop_value = prop.group('key'), prop.group('value')
359 if prop_key == 'apex.key':
360 # avbtool dumps the prop value with repr(), which contains single /
361 # double quotes that we don't want.
362 payload_info[prop_key] = prop_value.strip('\"\'')
363
364 else:
365 payload_info[key] = value
366
Ivan Lozanob021b2a2020-07-28 09:31:06 -0400367 # Validation check.
Jiyong Parka1887f32020-05-19 23:18:03 +0900368 for key in ('Algorithm', 'Salt', 'apex.key', 'Hash Algorithm'):
Tao Bao1cd59f22019-03-15 15:13:01 -0700369 if key not in payload_info:
370 raise ApexInfoError(
371 'Failed to find {} prop in {}'.format(key, payload_path))
372
373 return payload_info
Tao Baoe7354ba2019-05-09 16:54:15 -0700374
375
Nikita Ioffe36081482021-01-20 01:32:28 +0000376def SignUncompressedApex(avbtool, apex_file, payload_key, container_key,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000377 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200378 no_hashtree, signing_args=None, sign_tool=None,
379 is_sepolicy=False):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000380 """Signs the current uncompressed APEX with the given payload/container keys.
Tao Baoe7354ba2019-05-09 16:54:15 -0700381
382 Args:
Nikita Ioffe36081482021-01-20 01:32:28 +0000383 apex_file: Uncompressed APEX file.
Tao Baoe7354ba2019-05-09 16:54:15 -0700384 payload_key: The path to payload signing key (w/ extension).
385 container_key: The path to container signing key (w/o extension).
386 container_pw: The matching password of the container_key, or None.
Tianjie Xu88a759d2020-01-23 10:47:54 -0800387 apk_keys: A dict that holds the signing keys for apk files.
Tao Baoe7354ba2019-05-09 16:54:15 -0700388 codename_to_api_level_map: A dict that maps from codename to API level.
Tao Bao448004a2019-09-19 07:55:02 -0700389 no_hashtree: Don't include hashtree in the signed APEX.
Tao Baoe7354ba2019-05-09 16:54:15 -0700390 signing_args: Additional args to be passed to the payload signer.
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900391 sign_tool: A tool to sign the contents of the APEX.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200392 is_sepolicy: Indicates if the apex is a sepolicy.apex
Tao Baoe7354ba2019-05-09 16:54:15 -0700393
394 Returns:
395 The path to the signed APEX file.
396 """
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900397 # 1. Extract the apex payload image and sign the files (e.g. APKs). Repack
Tianjie Xu88a759d2020-01-23 10:47:54 -0800398 # the apex file after signing.
Tianjie Xu88a759d2020-01-23 10:47:54 -0800399 apk_signer = ApexApkSigner(apex_file, container_pw,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900400 codename_to_api_level_map,
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +0000401 avbtool, sign_tool)
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200402 apex_file = apk_signer.ProcessApexFile(
403 apk_keys, payload_key, signing_args, is_sepolicy)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800404
405 # 2a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given
Tao Baoe7354ba2019-05-09 16:54:15 -0700406 # payload_key.
407 payload_dir = common.MakeTempDir(prefix='apex-payload-')
408 with zipfile.ZipFile(apex_file) as apex_fd:
409 payload_file = apex_fd.extract(APEX_PAYLOAD_IMAGE, payload_dir)
Baligh Uddin15881282019-08-25 12:01:44 -0700410 zip_items = apex_fd.namelist()
Tao Baoe7354ba2019-05-09 16:54:15 -0700411
Tao Bao1ac886e2019-06-26 11:58:22 -0700412 payload_info = ParseApexPayloadInfo(avbtool, payload_file)
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400413 if no_hashtree is None:
414 no_hashtree = payload_info.get("Tree Size", 0) == 0
Tao Baoe7354ba2019-05-09 16:54:15 -0700415 SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700416 avbtool,
Tao Baoe7354ba2019-05-09 16:54:15 -0700417 payload_file,
418 payload_key,
419 payload_info['apex.key'],
420 payload_info['Algorithm'],
421 payload_info['Salt'],
Jiyong Parka1887f32020-05-19 23:18:03 +0900422 payload_info['Hash Algorithm'],
Tao Bao448004a2019-09-19 07:55:02 -0700423 no_hashtree,
Tao Baoe7354ba2019-05-09 16:54:15 -0700424 signing_args)
425
Tianjie Xu88a759d2020-01-23 10:47:54 -0800426 # 2b. Update the embedded payload public key.
Tianjiec180a5d2020-03-23 18:14:09 -0700427 payload_public_key = common.ExtractAvbPublicKey(avbtool, payload_key)
Tao Baoe7354ba2019-05-09 16:54:15 -0700428 common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE)
Baligh Uddin15881282019-08-25 12:01:44 -0700429 if APEX_PUBKEY in zip_items:
430 common.ZipDelete(apex_file, APEX_PUBKEY)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400431 apex_zip = zipfile.ZipFile(apex_file, 'a', allowZip64=True)
Tao Baoe7354ba2019-05-09 16:54:15 -0700432 common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE)
433 common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY)
Kelvin Zhang37a42902022-10-26 12:49:03 -0700434 apex_zip.close()
Tao Baoe7354ba2019-05-09 16:54:15 -0700435
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900436 # 3. Sign the APEX container with container_key.
Tao Baoe7354ba2019-05-09 16:54:15 -0700437 signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex')
438
439 # Specify the 4K alignment when calling SignApk.
440 extra_signapk_args = OPTIONS.extra_signapk_args[:]
Jooyung Hanebe9afe2021-07-12 13:23:52 +0900441 extra_signapk_args.extend(['-a', '4096', '--align-file-size'])
Tao Baoe7354ba2019-05-09 16:54:15 -0700442
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000443 password = container_pw.get(container_key) if container_pw else None
Tao Baoe7354ba2019-05-09 16:54:15 -0700444 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900445 apex_file,
Tao Baoe7354ba2019-05-09 16:54:15 -0700446 signed_apex,
447 container_key,
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000448 password,
Tao Baoe7354ba2019-05-09 16:54:15 -0700449 codename_to_api_level_map=codename_to_api_level_map,
450 extra_signapk_args=extra_signapk_args)
451
452 return signed_apex
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000453
454
Nikita Ioffe36081482021-01-20 01:32:28 +0000455def SignCompressedApex(avbtool, apex_file, payload_key, container_key,
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400456 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200457 no_hashtree, signing_args=None, sign_tool=None,
458 is_sepolicy=False):
Nikita Ioffe36081482021-01-20 01:32:28 +0000459 """Signs the current compressed APEX with the given payload/container keys.
460
461 Args:
462 apex_file: Raw uncompressed APEX data.
463 payload_key: The path to payload signing key (w/ extension).
464 container_key: The path to container signing key (w/o extension).
465 container_pw: The matching password of the container_key, or None.
466 apk_keys: A dict that holds the signing keys for apk files.
467 codename_to_api_level_map: A dict that maps from codename to API level.
468 no_hashtree: Don't include hashtree in the signed APEX.
469 signing_args: Additional args to be passed to the payload signer.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200470 is_sepolicy: Indicates if the apex is a sepolicy.apex
Nikita Ioffe36081482021-01-20 01:32:28 +0000471
472 Returns:
473 The path to the signed APEX file.
474 """
475 debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static')
476
477 # 1. Decompress original_apex inside compressed apex.
478 original_apex_file = common.MakeTempFile(prefix='original-apex-',
479 suffix='.apex')
480 # Decompression target path should not exist
481 os.remove(original_apex_file)
482 common.RunAndCheckOutput(['deapexer', '--debugfs_path', debugfs_path,
483 'decompress', '--input', apex_file,
484 '--output', original_apex_file])
485
486 # 2. Sign original_apex
487 signed_original_apex_file = SignUncompressedApex(
488 avbtool,
489 original_apex_file,
490 payload_key,
491 container_key,
492 container_pw,
493 apk_keys,
494 codename_to_api_level_map,
495 no_hashtree,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900496 signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200497 sign_tool,
498 is_sepolicy)
Nikita Ioffe36081482021-01-20 01:32:28 +0000499
500 # 3. Compress signed original apex.
501 compressed_apex_file = common.MakeTempFile(prefix='apex-container-',
502 suffix='.capex')
503 common.RunAndCheckOutput(['apex_compression_tool',
504 'compress',
505 '--apex_compression_tool_path', os.getenv('PATH'),
506 '--input', signed_original_apex_file,
507 '--output', compressed_apex_file])
508
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900509 # 4. Sign the APEX container with container_key.
Nikita Ioffe36081482021-01-20 01:32:28 +0000510 signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.capex')
511
Nikita Ioffe36081482021-01-20 01:32:28 +0000512 password = container_pw.get(container_key) if container_pw else None
513 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900514 compressed_apex_file,
Nikita Ioffe36081482021-01-20 01:32:28 +0000515 signed_apex,
516 container_key,
517 password,
518 codename_to_api_level_map=codename_to_api_level_map,
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900519 extra_signapk_args=OPTIONS.extra_signapk_args)
Nikita Ioffe36081482021-01-20 01:32:28 +0000520
521 return signed_apex
522
523
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000524def SignApex(avbtool, apex_data, payload_key, container_key, container_pw,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200525 apk_keys, codename_to_api_level_map, no_hashtree,
526 signing_args=None, sign_tool=None, is_sepolicy=False):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000527 """Signs the current APEX with the given payload/container keys.
528
529 Args:
530 apex_file: Path to apex file path.
531 payload_key: The path to payload signing key (w/ extension).
532 container_key: The path to container signing key (w/o extension).
533 container_pw: The matching password of the container_key, or None.
534 apk_keys: A dict that holds the signing keys for apk files.
535 codename_to_api_level_map: A dict that maps from codename to API level.
536 no_hashtree: Don't include hashtree in the signed APEX.
537 signing_args: Additional args to be passed to the payload signer.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200538 is_sepolicy: Indicates if the apex is a sepolicy.apex
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000539
540 Returns:
541 The path to the signed APEX file.
542 """
543 apex_file = common.MakeTempFile(prefix='apex-container-', suffix='.apex')
544 with open(apex_file, 'wb') as output_fp:
545 output_fp.write(apex_data)
546
Nikita Ioffe36081482021-01-20 01:32:28 +0000547 debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static')
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000548 cmd = ['deapexer', '--debugfs_path', debugfs_path,
549 'info', '--print-type', apex_file]
550
551 try:
552 apex_type = common.RunAndCheckOutput(cmd).strip()
553 if apex_type == 'UNCOMPRESSED':
554 return SignUncompressedApex(
555 avbtool,
Nikita Ioffe36081482021-01-20 01:32:28 +0000556 apex_file,
557 payload_key=payload_key,
558 container_key=container_key,
Kelvin Zhang137807d2022-09-14 15:02:46 -0700559 container_pw=container_pw,
Nikita Ioffe36081482021-01-20 01:32:28 +0000560 codename_to_api_level_map=codename_to_api_level_map,
561 no_hashtree=no_hashtree,
562 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900563 signing_args=signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200564 sign_tool=sign_tool,
565 is_sepolicy=is_sepolicy)
Nikita Ioffe36081482021-01-20 01:32:28 +0000566 elif apex_type == 'COMPRESSED':
567 return SignCompressedApex(
568 avbtool,
569 apex_file,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000570 payload_key=payload_key,
571 container_key=container_key,
Kelvin Zhang137807d2022-09-14 15:02:46 -0700572 container_pw=container_pw,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000573 codename_to_api_level_map=codename_to_api_level_map,
574 no_hashtree=no_hashtree,
575 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900576 signing_args=signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200577 sign_tool=sign_tool,
578 is_sepolicy=is_sepolicy)
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000579 else:
580 # TODO(b/172912232): support signing compressed apex
581 raise ApexInfoError('Unsupported apex type {}'.format(apex_type))
582
583 except common.ExternalError as e:
584 raise ApexInfoError(
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000585 'Failed to get type for {}:\n{}'.format(apex_file, e))
586
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400587
Daniel Normane9af70a2021-04-15 16:39:22 -0700588def GetApexInfoFromTargetFiles(input_file, partition, compressed_only=True):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000589 """
590 Get information about system APEX stored in the input_file zip
591
592 Args:
593 input_file: The filename of the target build target-files zip or directory.
594
595 Return:
596 A list of ota_metadata_pb2.ApexInfo() populated using the APEX stored in
597 /system partition of the input_file
598 """
599
600 # Extract the apex files so that we can run checks on them
601 if not isinstance(input_file, str):
602 raise RuntimeError("must pass filepath to target-files zip or directory")
603
Daniel Normane9af70a2021-04-15 16:39:22 -0700604 apex_subdir = os.path.join(partition.upper(), 'apex')
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000605 if os.path.isdir(input_file):
606 tmp_dir = input_file
607 else:
Daniel Normane9af70a2021-04-15 16:39:22 -0700608 tmp_dir = UnzipTemp(input_file, [os.path.join(apex_subdir, '*')])
609 target_dir = os.path.join(tmp_dir, apex_subdir)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000610
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800611 # Partial target-files packages for vendor-only builds may not contain
612 # a system apex directory.
613 if not os.path.exists(target_dir):
Daniel Normane9af70a2021-04-15 16:39:22 -0700614 logger.info('No APEX directory at path: %s', target_dir)
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800615 return []
616
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000617 apex_infos = []
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500618
619 debugfs_path = "debugfs"
620 if OPTIONS.search_path:
621 debugfs_path = os.path.join(OPTIONS.search_path, "bin", "debugfs_static")
Dennis Shenf58e5482022-10-10 21:19:46 +0000622
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500623 deapexer = 'deapexer'
624 if OPTIONS.search_path:
Kelvin Zhang05a3f682021-01-29 14:38:24 -0500625 deapexer_path = os.path.join(OPTIONS.search_path, "bin", "deapexer")
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500626 if os.path.isfile(deapexer_path):
627 deapexer = deapexer_path
Dennis Shenf58e5482022-10-10 21:19:46 +0000628
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000629 for apex_filename in os.listdir(target_dir):
630 apex_filepath = os.path.join(target_dir, apex_filename)
631 if not os.path.isfile(apex_filepath) or \
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400632 not zipfile.is_zipfile(apex_filepath):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000633 logger.info("Skipping %s because it's not a zipfile", apex_filepath)
634 continue
635 apex_info = ota_metadata_pb2.ApexInfo()
636 # Open the apex file to retrieve information
637 manifest = apex_manifest.fromApex(apex_filepath)
638 apex_info.package_name = manifest.name
639 apex_info.version = manifest.version
640 # Check if the file is compressed or not
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000641 apex_type = RunAndCheckOutput([
642 deapexer, "--debugfs_path", debugfs_path,
643 'info', '--print-type', apex_filepath]).rstrip()
644 if apex_type == 'COMPRESSED':
645 apex_info.is_compressed = True
646 elif apex_type == 'UNCOMPRESSED':
647 apex_info.is_compressed = False
648 else:
649 raise RuntimeError('Not an APEX file: ' + apex_type)
650
651 # Decompress compressed APEX to determine its size
652 if apex_info.is_compressed:
653 decompressed_file_path = MakeTempFile(prefix="decompressed-",
654 suffix=".apex")
655 # Decompression target path should not exist
656 os.remove(decompressed_file_path)
657 RunAndCheckOutput([deapexer, 'decompress', '--input', apex_filepath,
658 '--output', decompressed_file_path])
659 apex_info.decompressed_size = os.path.getsize(decompressed_file_path)
660
Daniel Normane9af70a2021-04-15 16:39:22 -0700661 if not compressed_only or apex_info.is_compressed:
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500662 apex_infos.append(apex_info)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000663
664 return apex_infos