blob: bfc87b8826ee823526806b0e737852b981a209b9 [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")
Jooyung Han0f5a41d2021-10-27 03:53:21 +090068 self.avbtool = avbtool if avbtool else "avbtool"
69 self.sign_tool = sign_tool
Tianjie Xu88a759d2020-01-23 10:47:54 -080070
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020071 def ProcessApexFile(self, apk_keys, payload_key, signing_args=None, is_sepolicy=False):
Jooyung Han0f5a41d2021-10-27 03:53:21 +090072 """Scans and signs the payload files and repack the apex
Tianjie Xu88a759d2020-01-23 10:47:54 -080073
74 Args:
75 apk_keys: A dict that holds the signing keys for apk files.
Tianjie Xu88a759d2020-01-23 10:47:54 -080076
77 Returns:
78 The repacked apex file containing the signed apk files.
79 """
Kelvin Zhangdd833dc2020-08-21 14:13:13 -040080 if not os.path.exists(self.debugfs_path):
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040081 raise ApexSigningError(
82 "Couldn't find location of debugfs_static: " +
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +000083 "Path {} does not exist. ".format(self.debugfs_path) +
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040084 "Make sure bin/debugfs_static can be found in -p <path>")
Dennis Shenf58e5482022-10-10 21:19:46 +000085 list_cmd = ['deapexer', '--debugfs_path', self.debugfs_path,
Dennis Shena8d11432022-11-07 21:37:06 +000086 'list', self.apex_path]
Tianjie Xu88a759d2020-01-23 10:47:54 -080087 entries_names = common.RunAndCheckOutput(list_cmd).split()
88 apk_entries = [name for name in entries_names if name.endswith('.apk')]
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020089 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 Xu88a759d2020-01-23 10:47:54 -080093
94 # No need to sign and repack, return the original apex path.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020095 if not apk_entries and not sepolicy_entries and self.sign_tool is None:
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +000096 logger.info('No apk file to sign in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -080097 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úñiga8e3198a2022-04-13 16:23:45 +0000110 payload_dir, has_signed_content = self.ExtractApexPayloadAndSignContents(
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200111 apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900112 if not has_signed_content:
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200113 logger.info('No contents have been signed in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800114 return self.apex_path
115
Baligh Uddin639b3b72020-03-25 20:50:23 -0700116 return self.RepackApexPayload(payload_dir, payload_key, signing_args)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800117
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200118 def ExtractApexPayloadAndSignContents(self, apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args):
Tianjie Xu88a759d2020-01-23 10:47:54 -0800119 """Extracts the payload image and signs the containing apk files."""
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400120 if not os.path.exists(self.debugfs_path):
121 raise ApexSigningError(
122 "Couldn't find location of debugfs_static: " +
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000123 "Path {} does not exist. ".format(self.debugfs_path) +
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400124 "Make sure bin/debugfs_static can be found in -p <path>")
Dennis Shenf58e5482022-10-10 21:19:46 +0000125 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 Xu88a759d2020-01-23 10:47:54 -0800130 payload_dir = common.MakeTempDir()
Dennis Shenf58e5482022-10-10 21:19:46 +0000131 extract_cmd = ['deapexer', '--debugfs_path', self.debugfs_path,
Dennis Shena8d11432022-11-07 21:37:06 +0000132 '--fsckerofs_path', self.fsckerofs_path,
Jooyung Han62949022023-06-14 15:16:34 +0900133 'extract',
Dennis Shenf58e5482022-10-10 21:19:46 +0000134 self.apex_path, payload_dir]
Tianjie Xu88a759d2020-01-23 10:47:54 -0800135 common.RunAndCheckOutput(extract_cmd)
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200136 assert os.path.exists(self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800137
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900138 has_signed_content = False
Tianjie Xu88a759d2020-01-23 10:47:54 -0800139 for entry in apk_entries:
140 apk_path = os.path.join(payload_dir, entry)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800141
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 Islam9fd58862021-01-06 13:33:25 +0000152 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 Han0f5a41d2021-10-27 03:53:21 +0900155 has_signed_content = True
156
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200157 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 Han0f5a41d2021-10-27 03:53:21 +0900188 if self.sign_tool:
Jooyung Han8caba5e2021-10-27 03:58:09 +0900189 logger.info('Signing payload contents in apex %s with %s', self.apex_path, self.sign_tool)
Jooyung Han39259ec2022-02-07 15:56:53 +0900190 # 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 Han0f5a41d2021-10-27 03:53:21 +0900196 common.RunAndCheckOutput(cmd)
197 has_signed_content = True
198
199 return payload_dir, has_signed_content
Tianjie Xu88a759d2020-01-23 10:47:54 -0800200
Baligh Uddin639b3b72020-03-25 20:50:23 -0700201 def RepackApexPayload(self, payload_dir, payload_key, signing_args=None):
Tianjie Xu88a759d2020-01-23 10:47:54 -0800202 """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 Xu88a759d2020-01-23 10:47:54 -0800206 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 Xu88a759d2020-01-23 10:47:54 -0800209 'key': payload_key,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800210 }
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 Uddinbe2d7d02022-02-26 02:34:06 +0000220 shutil.rmtree(path, ignore_errors=True)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800221
Tianjiec180a5d2020-03-23 18:14:09 -0700222 # 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 Xu88a759d2020-01-23 10:47:54 -0800229 for key, val in arguments_dict.items():
Tianjiec180a5d2020-03-23 18:14:09 -0700230 generate_image_cmd.extend(['--' + key, val])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700231
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 Zhangd6b799a2020-08-19 14:54:42 -0400235 generate_image_cmd.extend(
236 ['--signing_args', '"{}"'.format(signing_args)])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700237
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800238 # optional arguments for apex repacking
Tianjie Xu88a759d2020-01-23 10:47:54 -0800239 manifest_json = os.path.join(apex_dir, 'apex_manifest.json')
240 if os.path.exists(manifest_json):
Tianjiec180a5d2020-03-23 18:14:09 -0700241 generate_image_cmd.extend(['--manifest_json', manifest_json])
242 generate_image_cmd.extend([payload_dir, payload_img])
Tianjie Xucea6ad12020-01-30 17:12:05 -0800243 if OPTIONS.verbose:
Tianjiec180a5d2020-03-23 18:14:09 -0700244 generate_image_cmd.append('-v')
245 common.RunAndCheckOutput(generate_image_cmd)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800246
Tianjiec180a5d2020-03-23 18:14:09 -0700247 # Add the payload image back to the apex file.
248 common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400249 with zipfile.ZipFile(self.apex_path, 'a', allowZip64=True) as output_apex:
Tianjiec180a5d2020-03-23 18:14:09 -0700250 common.ZipWrite(output_apex, payload_img, APEX_PAYLOAD_IMAGE,
251 compress_type=zipfile.ZIP_STORED)
252 return self.apex_path
Tianjie Xu88a759d2020-01-23 10:47:54 -0800253
254
Tao Bao1ac886e2019-06-26 11:58:22 -0700255def SignApexPayload(avbtool, payload_file, payload_key_path, payload_key_name,
Jiyong Parka1887f32020-05-19 23:18:03 +0900256 algorithm, salt, hash_algorithm, no_hashtree, signing_args=None):
Tao Bao1cd59f22019-03-15 15:13:01 -0700257 """Signs a given payload_file with the payload key."""
258 # Add the new footer. Old footer, if any, will be replaced by avbtool.
Tao Bao1ac886e2019-06-26 11:58:22 -0700259 cmd = [avbtool, 'add_hashtree_footer',
Tao Bao1cd59f22019-03-15 15:13:01 -0700260 '--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 Parka1887f32020-05-19 23:18:03 +0900265 '--salt', salt,
266 '--hash_algorithm', hash_algorithm]
Tao Bao448004a2019-09-19 07:55:02 -0700267 if no_hashtree:
268 cmd.append('--no_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700269 if signing_args:
270 cmd.extend(shlex.split(signing_args))
271
272 try:
273 common.RunAndCheckOutput(cmd)
274 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700275 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700276 'Failed to sign APEX payload {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700277 payload_file, payload_key_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700278
279 # Verify the signed payload image with specified public key.
280 logger.info('Verifying %s', payload_file)
Tao Bao448004a2019-09-19 07:55:02 -0700281 VerifyApexPayload(avbtool, payload_file, payload_key_path, no_hashtree)
Tao Bao1cd59f22019-03-15 15:13:01 -0700282
283
Tao Bao448004a2019-09-19 07:55:02 -0700284def VerifyApexPayload(avbtool, payload_file, payload_key, no_hashtree=False):
Tao Bao1cd59f22019-03-15 15:13:01 -0700285 """Verifies the APEX payload signature with the given key."""
Tao Bao1ac886e2019-06-26 11:58:22 -0700286 cmd = [avbtool, 'verify_image', '--image', payload_file,
Tao Bao1cd59f22019-03-15 15:13:01 -0700287 '--key', payload_key]
Tao Bao448004a2019-09-19 07:55:02 -0700288 if no_hashtree:
289 cmd.append('--accept_zeroed_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700290 try:
291 common.RunAndCheckOutput(cmd)
292 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700293 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700294 'Failed to validate payload signing for {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700295 payload_file, payload_key, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700296
297
Tao Bao1ac886e2019-06-26 11:58:22 -0700298def ParseApexPayloadInfo(avbtool, payload_path):
Tao Bao1cd59f22019-03-15 15:13:01 -0700299 """Parses the APEX payload info.
300
301 Args:
Tao Bao1ac886e2019-06-26 11:58:22 -0700302 avbtool: The AVB tool to use.
Tao Bao1cd59f22019-03-15 15:13:01 -0700303 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 Bao448004a2019-09-19 07:55:02 -0700310 contain Algorithm, Salt, Tree Size and apex.key.
Tao Bao1cd59f22019-03-15 15:13:01 -0700311 """
312 if not os.path.exists(payload_path):
313 raise ApexInfoError('Failed to find image: {}'.format(payload_path))
314
Tao Bao1ac886e2019-06-26 11:58:22 -0700315 cmd = [avbtool, 'info_image', '--image', payload_path]
Tao Bao1cd59f22019-03-15 15:13:01 -0700316 try:
317 output = common.RunAndCheckOutput(cmd)
318 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700319 raise ApexInfoError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700320 'Failed to get APEX payload info for {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700321 payload_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700322
Jiyong Parka1887f32020-05-19 23:18:03 +0900323 # Extract the Algorithm / Hash Algorithm / Salt / Prop info / Tree size from
324 # payload (i.e. an image signed with avbtool). For example,
Tao Bao1cd59f22019-03-15 15:13:01 -0700325 # Algorithm: SHA256_RSA4096
326 PAYLOAD_INFO_PATTERN = (
Jiyong Parka1887f32020-05-19 23:18:03 +0900327 r'^\s*(?P<key>Algorithm|Hash Algorithm|Salt|Prop|Tree Size)\:\s*(?P<value>.*?)$')
Tao Bao1cd59f22019-03-15 15:13:01 -0700328 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 Lozanob021b2a2020-07-28 09:31:06 -0400360 # Validation check.
Jiyong Parka1887f32020-05-19 23:18:03 +0900361 for key in ('Algorithm', 'Salt', 'apex.key', 'Hash Algorithm'):
Tao Bao1cd59f22019-03-15 15:13:01 -0700362 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 Baoe7354ba2019-05-09 16:54:15 -0700367
368
Nikita Ioffe36081482021-01-20 01:32:28 +0000369def SignUncompressedApex(avbtool, apex_file, payload_key, container_key,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000370 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200371 no_hashtree, signing_args=None, sign_tool=None,
372 is_sepolicy=False):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000373 """Signs the current uncompressed APEX with the given payload/container keys.
Tao Baoe7354ba2019-05-09 16:54:15 -0700374
375 Args:
Nikita Ioffe36081482021-01-20 01:32:28 +0000376 apex_file: Uncompressed APEX file.
Tao Baoe7354ba2019-05-09 16:54:15 -0700377 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 Xu88a759d2020-01-23 10:47:54 -0800380 apk_keys: A dict that holds the signing keys for apk files.
Tao Baoe7354ba2019-05-09 16:54:15 -0700381 codename_to_api_level_map: A dict that maps from codename to API level.
Tao Bao448004a2019-09-19 07:55:02 -0700382 no_hashtree: Don't include hashtree in the signed APEX.
Tao Baoe7354ba2019-05-09 16:54:15 -0700383 signing_args: Additional args to be passed to the payload signer.
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900384 sign_tool: A tool to sign the contents of the APEX.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200385 is_sepolicy: Indicates if the apex is a sepolicy.apex
Tao Baoe7354ba2019-05-09 16:54:15 -0700386
387 Returns:
388 The path to the signed APEX file.
389 """
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900390 # 1. Extract the apex payload image and sign the files (e.g. APKs). Repack
Tianjie Xu88a759d2020-01-23 10:47:54 -0800391 # the apex file after signing.
Tianjie Xu88a759d2020-01-23 10:47:54 -0800392 apk_signer = ApexApkSigner(apex_file, container_pw,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900393 codename_to_api_level_map,
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +0000394 avbtool, sign_tool)
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200395 apex_file = apk_signer.ProcessApexFile(
396 apk_keys, payload_key, signing_args, is_sepolicy)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800397
398 # 2a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given
Tao Baoe7354ba2019-05-09 16:54:15 -0700399 # 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 Uddin15881282019-08-25 12:01:44 -0700403 zip_items = apex_fd.namelist()
Tao Baoe7354ba2019-05-09 16:54:15 -0700404
Tao Bao1ac886e2019-06-26 11:58:22 -0700405 payload_info = ParseApexPayloadInfo(avbtool, payload_file)
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400406 if no_hashtree is None:
407 no_hashtree = payload_info.get("Tree Size", 0) == 0
Tao Baoe7354ba2019-05-09 16:54:15 -0700408 SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700409 avbtool,
Tao Baoe7354ba2019-05-09 16:54:15 -0700410 payload_file,
411 payload_key,
412 payload_info['apex.key'],
413 payload_info['Algorithm'],
414 payload_info['Salt'],
Jiyong Parka1887f32020-05-19 23:18:03 +0900415 payload_info['Hash Algorithm'],
Tao Bao448004a2019-09-19 07:55:02 -0700416 no_hashtree,
Tao Baoe7354ba2019-05-09 16:54:15 -0700417 signing_args)
418
Tianjie Xu88a759d2020-01-23 10:47:54 -0800419 # 2b. Update the embedded payload public key.
Tianjiec180a5d2020-03-23 18:14:09 -0700420 payload_public_key = common.ExtractAvbPublicKey(avbtool, payload_key)
Tao Baoe7354ba2019-05-09 16:54:15 -0700421 common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE)
Baligh Uddin15881282019-08-25 12:01:44 -0700422 if APEX_PUBKEY in zip_items:
423 common.ZipDelete(apex_file, APEX_PUBKEY)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400424 apex_zip = zipfile.ZipFile(apex_file, 'a', allowZip64=True)
Tao Baoe7354ba2019-05-09 16:54:15 -0700425 common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE)
426 common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY)
Kelvin Zhangf92f7f02023-04-14 21:32:54 +0000427 common.ZipClose(apex_zip)
Tao Baoe7354ba2019-05-09 16:54:15 -0700428
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900429 # 3. Sign the APEX container with container_key.
Tao Baoe7354ba2019-05-09 16:54:15 -0700430 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 Hanebe9afe2021-07-12 13:23:52 +0900434 extra_signapk_args.extend(['-a', '4096', '--align-file-size'])
Tao Baoe7354ba2019-05-09 16:54:15 -0700435
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000436 password = container_pw.get(container_key) if container_pw else None
Tao Baoe7354ba2019-05-09 16:54:15 -0700437 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900438 apex_file,
Tao Baoe7354ba2019-05-09 16:54:15 -0700439 signed_apex,
440 container_key,
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000441 password,
Tao Baoe7354ba2019-05-09 16:54:15 -0700442 codename_to_api_level_map=codename_to_api_level_map,
443 extra_signapk_args=extra_signapk_args)
444
445 return signed_apex
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000446
447
Nikita Ioffe36081482021-01-20 01:32:28 +0000448def SignCompressedApex(avbtool, apex_file, payload_key, container_key,
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400449 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200450 no_hashtree, signing_args=None, sign_tool=None,
451 is_sepolicy=False):
Nikita Ioffe36081482021-01-20 01:32:28 +0000452 """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 Zunigae0a977a2022-06-16 18:44:27 +0200463 is_sepolicy: Indicates if the apex is a sepolicy.apex
Nikita Ioffe36081482021-01-20 01:32:28 +0000464
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 Han0f5a41d2021-10-27 03:53:21 +0900489 signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200490 sign_tool,
491 is_sepolicy)
Nikita Ioffe36081482021-01-20 01:32:28 +0000492
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 Hanf9be5ee2021-07-22 18:21:25 +0900502 # 4. Sign the APEX container with container_key.
Nikita Ioffe36081482021-01-20 01:32:28 +0000503 signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.capex')
504
Nikita Ioffe36081482021-01-20 01:32:28 +0000505 password = container_pw.get(container_key) if container_pw else None
506 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900507 compressed_apex_file,
Nikita Ioffe36081482021-01-20 01:32:28 +0000508 signed_apex,
509 container_key,
510 password,
511 codename_to_api_level_map=codename_to_api_level_map,
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900512 extra_signapk_args=OPTIONS.extra_signapk_args)
Nikita Ioffe36081482021-01-20 01:32:28 +0000513
514 return signed_apex
515
516
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000517def SignApex(avbtool, apex_data, payload_key, container_key, container_pw,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200518 apk_keys, codename_to_api_level_map, no_hashtree,
519 signing_args=None, sign_tool=None, is_sepolicy=False):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000520 """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 Zunigae0a977a2022-06-16 18:44:27 +0200531 is_sepolicy: Indicates if the apex is a sepolicy.apex
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000532
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 Ioffe36081482021-01-20 01:32:28 +0000540 debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static')
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000541 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 Ioffe36081482021-01-20 01:32:28 +0000549 apex_file,
550 payload_key=payload_key,
551 container_key=container_key,
Kelvin Zhang137807d2022-09-14 15:02:46 -0700552 container_pw=container_pw,
Nikita Ioffe36081482021-01-20 01:32:28 +0000553 codename_to_api_level_map=codename_to_api_level_map,
554 no_hashtree=no_hashtree,
555 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900556 signing_args=signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200557 sign_tool=sign_tool,
558 is_sepolicy=is_sepolicy)
Nikita Ioffe36081482021-01-20 01:32:28 +0000559 elif apex_type == 'COMPRESSED':
560 return SignCompressedApex(
561 avbtool,
562 apex_file,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000563 payload_key=payload_key,
564 container_key=container_key,
Kelvin Zhang137807d2022-09-14 15:02:46 -0700565 container_pw=container_pw,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000566 codename_to_api_level_map=codename_to_api_level_map,
567 no_hashtree=no_hashtree,
568 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900569 signing_args=signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200570 sign_tool=sign_tool,
571 is_sepolicy=is_sepolicy)
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000572 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 Islam9fd58862021-01-06 13:33:25 +0000578 'Failed to get type for {}:\n{}'.format(apex_file, e))
579
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400580
Daniel Normane9af70a2021-04-15 16:39:22 -0700581def GetApexInfoFromTargetFiles(input_file, partition, compressed_only=True):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000582 """
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 Normane9af70a2021-04-15 16:39:22 -0700597 apex_subdir = os.path.join(partition.upper(), 'apex')
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000598 if os.path.isdir(input_file):
599 tmp_dir = input_file
600 else:
Daniel Normane9af70a2021-04-15 16:39:22 -0700601 tmp_dir = UnzipTemp(input_file, [os.path.join(apex_subdir, '*')])
602 target_dir = os.path.join(tmp_dir, apex_subdir)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000603
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800604 # 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 Normane9af70a2021-04-15 16:39:22 -0700607 logger.info('No APEX directory at path: %s', target_dir)
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800608 return []
609
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000610 apex_infos = []
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500611
612 debugfs_path = "debugfs"
613 if OPTIONS.search_path:
614 debugfs_path = os.path.join(OPTIONS.search_path, "bin", "debugfs_static")
Dennis Shenf58e5482022-10-10 21:19:46 +0000615
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500616 deapexer = 'deapexer'
617 if OPTIONS.search_path:
Kelvin Zhang05a3f682021-01-29 14:38:24 -0500618 deapexer_path = os.path.join(OPTIONS.search_path, "bin", "deapexer")
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500619 if os.path.isfile(deapexer_path):
620 deapexer = deapexer_path
Dennis Shenf58e5482022-10-10 21:19:46 +0000621
Håkan Kvist01e38192023-04-13 21:49:19 +0200622 for apex_filename in sorted(os.listdir(target_dir)):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000623 apex_filepath = os.path.join(target_dir, apex_filename)
624 if not os.path.isfile(apex_filepath) or \
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400625 not zipfile.is_zipfile(apex_filepath):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000626 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 Islam9fd58862021-01-06 13:33:25 +0000634 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 Normane9af70a2021-04-15 16:39:22 -0700654 if not compressed_only or apex_info.is_compressed:
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500655 apex_infos.append(apex_info)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000656
657 return apex_infos