blob: d7b0ba259b5dc4d10c7e6a807f4581aa12d394c7 [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")
Jooyung Han0f5a41d2021-10-27 03:53:21 +090066 self.avbtool = avbtool if avbtool else "avbtool"
67 self.sign_tool = sign_tool
Tianjie Xu88a759d2020-01-23 10:47:54 -080068
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020069 def ProcessApexFile(self, apk_keys, payload_key, signing_args=None, is_sepolicy=False):
Jooyung Han0f5a41d2021-10-27 03:53:21 +090070 """Scans and signs the payload files and repack the apex
Tianjie Xu88a759d2020-01-23 10:47:54 -080071
72 Args:
73 apk_keys: A dict that holds the signing keys for apk files.
Tianjie Xu88a759d2020-01-23 10:47:54 -080074
75 Returns:
76 The repacked apex file containing the signed apk files.
77 """
Kelvin Zhangdd833dc2020-08-21 14:13:13 -040078 if not os.path.exists(self.debugfs_path):
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040079 raise ApexSigningError(
80 "Couldn't find location of debugfs_static: " +
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +000081 "Path {} does not exist. ".format(self.debugfs_path) +
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040082 "Make sure bin/debugfs_static can be found in -p <path>")
83 list_cmd = ['deapexer', '--debugfs_path',
Kelvin Zhangdd833dc2020-08-21 14:13:13 -040084 self.debugfs_path, 'list', self.apex_path]
Tianjie Xu88a759d2020-01-23 10:47:54 -080085 entries_names = common.RunAndCheckOutput(list_cmd).split()
86 apk_entries = [name for name in entries_names if name.endswith('.apk')]
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020087 sepolicy_entries = []
88 if is_sepolicy:
89 sepolicy_entries = [name for name in entries_names if
90 name.startswith('./etc/SEPolicy') and name.endswith('.zip')]
Tianjie Xu88a759d2020-01-23 10:47:54 -080091
92 # No need to sign and repack, return the original apex path.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +020093 if not apk_entries and not sepolicy_entries and self.sign_tool is None:
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +000094 logger.info('No apk file to sign in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -080095 return self.apex_path
96
97 for entry in apk_entries:
98 apk_name = os.path.basename(entry)
99 if apk_name not in apk_keys:
100 raise ApexSigningError('Failed to find signing keys for apk file {} in'
101 ' apex {}. Use "-e <apkname>=" to specify a key'
102 .format(entry, self.apex_path))
103 if not any(dirname in entry for dirname in ['app/', 'priv-app/',
104 'overlay/']):
105 logger.warning('Apk path does not contain the intended directory name:'
106 ' %s', entry)
107
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +0000108 payload_dir, has_signed_content = self.ExtractApexPayloadAndSignContents(
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200109 apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900110 if not has_signed_content:
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200111 logger.info('No contents have been signed in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800112 return self.apex_path
113
Baligh Uddin639b3b72020-03-25 20:50:23 -0700114 return self.RepackApexPayload(payload_dir, payload_key, signing_args)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800115
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200116 def ExtractApexPayloadAndSignContents(self, apk_entries, sepolicy_entries, apk_keys, payload_key, signing_args):
Tianjie Xu88a759d2020-01-23 10:47:54 -0800117 """Extracts the payload image and signs the containing apk files."""
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400118 if not os.path.exists(self.debugfs_path):
119 raise ApexSigningError(
120 "Couldn't find location of debugfs_static: " +
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000121 "Path {} does not exist. ".format(self.debugfs_path) +
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400122 "Make sure bin/debugfs_static can be found in -p <path>")
Tianjie Xu88a759d2020-01-23 10:47:54 -0800123 payload_dir = common.MakeTempDir()
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400124 extract_cmd = ['deapexer', '--debugfs_path',
125 self.debugfs_path, 'extract', self.apex_path, payload_dir]
Tianjie Xu88a759d2020-01-23 10:47:54 -0800126 common.RunAndCheckOutput(extract_cmd)
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200127 assert os.path.exists(self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800128
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900129 has_signed_content = False
Tianjie Xu88a759d2020-01-23 10:47:54 -0800130 for entry in apk_entries:
131 apk_path = os.path.join(payload_dir, entry)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800132
133 key_name = apk_keys.get(os.path.basename(entry))
134 if key_name in common.SPECIAL_CERT_STRINGS:
135 logger.info('Not signing: %s due to special cert string', apk_path)
136 continue
137
138 logger.info('Signing apk file %s in apex %s', apk_path, self.apex_path)
139 # Rename the unsigned apk and overwrite the original apk path with the
140 # signed apk file.
141 unsigned_apk = common.MakeTempFile()
142 os.rename(apk_path, unsigned_apk)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000143 common.SignFile(
144 unsigned_apk, apk_path, key_name, self.key_passwords.get(key_name),
145 codename_to_api_level_map=self.codename_to_api_level_map)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900146 has_signed_content = True
147
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200148 for entry in sepolicy_entries:
149 sepolicy_path = os.path.join(payload_dir, entry)
150
151 if not 'etc' in entry:
152 logger.warning('Sepolicy path does not contain the intended directory name etc:'
153 ' %s', entry)
154
155 key_name = apk_keys.get(os.path.basename(entry))
156 if key_name is None:
157 logger.warning('Failed to find signing keys for {} in'
158 ' apex {}, payload key will be used instead.'
159 ' Use "-e <name>=" to specify a key'
160 .format(entry, self.apex_path))
161 key_name = payload_key
162
163 if key_name in common.SPECIAL_CERT_STRINGS:
164 logger.info('Not signing: %s due to special cert string', sepolicy_path)
165 continue
166
167 if OPTIONS.sign_sepolicy_path is not None:
168 sig_path = os.path.join(payload_dir, sepolicy_path + '.sig')
169 fsv_sig_path = os.path.join(payload_dir, sepolicy_path + '.fsv_sig')
170 old_sig = common.MakeTempFile()
171 old_fsv_sig = common.MakeTempFile()
172 os.rename(sig_path, old_sig)
173 os.rename(fsv_sig_path, old_fsv_sig)
174
175 logger.info('Signing sepolicy file %s in apex %s', sepolicy_path, self.apex_path)
176 if common.SignSePolicy(sepolicy_path, key_name, self.key_passwords.get(key_name)):
177 has_signed_content = True
178
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900179 if self.sign_tool:
Jooyung Han8caba5e2021-10-27 03:58:09 +0900180 logger.info('Signing payload contents in apex %s with %s', self.apex_path, self.sign_tool)
Jooyung Han39259ec2022-02-07 15:56:53 +0900181 # Pass avbtool to the custom signing tool
182 cmd = [self.sign_tool, '--avbtool', self.avbtool]
183 # Pass signing_args verbatim which will be forwarded to avbtool (e.g. --signing_helper=...)
184 if signing_args:
185 cmd.extend(['--signing_args', '"{}"'.format(signing_args)])
186 cmd.extend([payload_key, payload_dir])
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900187 common.RunAndCheckOutput(cmd)
188 has_signed_content = True
189
190 return payload_dir, has_signed_content
Tianjie Xu88a759d2020-01-23 10:47:54 -0800191
Baligh Uddin639b3b72020-03-25 20:50:23 -0700192 def RepackApexPayload(self, payload_dir, payload_key, signing_args=None):
Tianjie Xu88a759d2020-01-23 10:47:54 -0800193 """Rebuilds the apex file with the updated payload directory."""
194 apex_dir = common.MakeTempDir()
195 # Extract the apex file and reuse its meta files as repack parameters.
196 common.UnzipToDir(self.apex_path, apex_dir)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800197 arguments_dict = {
198 'manifest': os.path.join(apex_dir, 'apex_manifest.pb'),
199 'build_info': os.path.join(apex_dir, 'apex_build_info.pb'),
Tianjie Xu88a759d2020-01-23 10:47:54 -0800200 'key': payload_key,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800201 }
202 for filename in arguments_dict.values():
203 assert os.path.exists(filename), 'file {} not found'.format(filename)
204
205 # The repack process will add back these files later in the payload image.
206 for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']:
207 path = os.path.join(payload_dir, name)
208 if os.path.isfile(path):
209 os.remove(path)
210 elif os.path.isdir(path):
Baligh Uddinbe2d7d02022-02-26 02:34:06 +0000211 shutil.rmtree(path, ignore_errors=True)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800212
Tianjiec180a5d2020-03-23 18:14:09 -0700213 # TODO(xunchang) the signing process can be improved by using
214 # '--unsigned_payload_only'. But we need to parse the vbmeta earlier for
215 # the signing arguments, e.g. algorithm, salt, etc.
216 payload_img = os.path.join(apex_dir, APEX_PAYLOAD_IMAGE)
217 generate_image_cmd = ['apexer', '--force', '--payload_only',
218 '--do_not_check_keyname', '--apexer_tool_path',
219 os.getenv('PATH')]
Tianjie Xu88a759d2020-01-23 10:47:54 -0800220 for key, val in arguments_dict.items():
Tianjiec180a5d2020-03-23 18:14:09 -0700221 generate_image_cmd.extend(['--' + key, val])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700222
223 # Add quote to the signing_args as we will pass
224 # --signing_args "--signing_helper_with_files=%path" to apexer
225 if signing_args:
Kelvin Zhangd6b799a2020-08-19 14:54:42 -0400226 generate_image_cmd.extend(
227 ['--signing_args', '"{}"'.format(signing_args)])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700228
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800229 # optional arguments for apex repacking
Tianjie Xu88a759d2020-01-23 10:47:54 -0800230 manifest_json = os.path.join(apex_dir, 'apex_manifest.json')
231 if os.path.exists(manifest_json):
Tianjiec180a5d2020-03-23 18:14:09 -0700232 generate_image_cmd.extend(['--manifest_json', manifest_json])
233 generate_image_cmd.extend([payload_dir, payload_img])
Tianjie Xucea6ad12020-01-30 17:12:05 -0800234 if OPTIONS.verbose:
Tianjiec180a5d2020-03-23 18:14:09 -0700235 generate_image_cmd.append('-v')
236 common.RunAndCheckOutput(generate_image_cmd)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800237
Tianjiec180a5d2020-03-23 18:14:09 -0700238 # Add the payload image back to the apex file.
239 common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400240 with zipfile.ZipFile(self.apex_path, 'a', allowZip64=True) as output_apex:
Tianjiec180a5d2020-03-23 18:14:09 -0700241 common.ZipWrite(output_apex, payload_img, APEX_PAYLOAD_IMAGE,
242 compress_type=zipfile.ZIP_STORED)
243 return self.apex_path
Tianjie Xu88a759d2020-01-23 10:47:54 -0800244
245
Tao Bao1ac886e2019-06-26 11:58:22 -0700246def SignApexPayload(avbtool, payload_file, payload_key_path, payload_key_name,
Jiyong Parka1887f32020-05-19 23:18:03 +0900247 algorithm, salt, hash_algorithm, no_hashtree, signing_args=None):
Tao Bao1cd59f22019-03-15 15:13:01 -0700248 """Signs a given payload_file with the payload key."""
249 # Add the new footer. Old footer, if any, will be replaced by avbtool.
Tao Bao1ac886e2019-06-26 11:58:22 -0700250 cmd = [avbtool, 'add_hashtree_footer',
Tao Bao1cd59f22019-03-15 15:13:01 -0700251 '--do_not_generate_fec',
252 '--algorithm', algorithm,
253 '--key', payload_key_path,
254 '--prop', 'apex.key:{}'.format(payload_key_name),
255 '--image', payload_file,
Jiyong Parka1887f32020-05-19 23:18:03 +0900256 '--salt', salt,
257 '--hash_algorithm', hash_algorithm]
Tao Bao448004a2019-09-19 07:55:02 -0700258 if no_hashtree:
259 cmd.append('--no_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700260 if signing_args:
261 cmd.extend(shlex.split(signing_args))
262
263 try:
264 common.RunAndCheckOutput(cmd)
265 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700266 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700267 'Failed to sign APEX payload {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700268 payload_file, payload_key_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700269
270 # Verify the signed payload image with specified public key.
271 logger.info('Verifying %s', payload_file)
Tao Bao448004a2019-09-19 07:55:02 -0700272 VerifyApexPayload(avbtool, payload_file, payload_key_path, no_hashtree)
Tao Bao1cd59f22019-03-15 15:13:01 -0700273
274
Tao Bao448004a2019-09-19 07:55:02 -0700275def VerifyApexPayload(avbtool, payload_file, payload_key, no_hashtree=False):
Tao Bao1cd59f22019-03-15 15:13:01 -0700276 """Verifies the APEX payload signature with the given key."""
Tao Bao1ac886e2019-06-26 11:58:22 -0700277 cmd = [avbtool, 'verify_image', '--image', payload_file,
Tao Bao1cd59f22019-03-15 15:13:01 -0700278 '--key', payload_key]
Tao Bao448004a2019-09-19 07:55:02 -0700279 if no_hashtree:
280 cmd.append('--accept_zeroed_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700281 try:
282 common.RunAndCheckOutput(cmd)
283 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700284 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700285 'Failed to validate payload signing for {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700286 payload_file, payload_key, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700287
288
Tao Bao1ac886e2019-06-26 11:58:22 -0700289def ParseApexPayloadInfo(avbtool, payload_path):
Tao Bao1cd59f22019-03-15 15:13:01 -0700290 """Parses the APEX payload info.
291
292 Args:
Tao Bao1ac886e2019-06-26 11:58:22 -0700293 avbtool: The AVB tool to use.
Tao Bao1cd59f22019-03-15 15:13:01 -0700294 payload_path: The path to the payload image.
295
296 Raises:
297 ApexInfoError on parsing errors.
298
299 Returns:
300 A dict that contains payload property-value pairs. The dict should at least
Tao Bao448004a2019-09-19 07:55:02 -0700301 contain Algorithm, Salt, Tree Size and apex.key.
Tao Bao1cd59f22019-03-15 15:13:01 -0700302 """
303 if not os.path.exists(payload_path):
304 raise ApexInfoError('Failed to find image: {}'.format(payload_path))
305
Tao Bao1ac886e2019-06-26 11:58:22 -0700306 cmd = [avbtool, 'info_image', '--image', payload_path]
Tao Bao1cd59f22019-03-15 15:13:01 -0700307 try:
308 output = common.RunAndCheckOutput(cmd)
309 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700310 raise ApexInfoError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700311 'Failed to get APEX payload info for {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700312 payload_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700313
Jiyong Parka1887f32020-05-19 23:18:03 +0900314 # Extract the Algorithm / Hash Algorithm / Salt / Prop info / Tree size from
315 # payload (i.e. an image signed with avbtool). For example,
Tao Bao1cd59f22019-03-15 15:13:01 -0700316 # Algorithm: SHA256_RSA4096
317 PAYLOAD_INFO_PATTERN = (
Jiyong Parka1887f32020-05-19 23:18:03 +0900318 r'^\s*(?P<key>Algorithm|Hash Algorithm|Salt|Prop|Tree Size)\:\s*(?P<value>.*?)$')
Tao Bao1cd59f22019-03-15 15:13:01 -0700319 payload_info_matcher = re.compile(PAYLOAD_INFO_PATTERN)
320
321 payload_info = {}
322 for line in output.split('\n'):
323 line_info = payload_info_matcher.match(line)
324 if not line_info:
325 continue
326
327 key, value = line_info.group('key'), line_info.group('value')
328
329 if key == 'Prop':
330 # Further extract the property key-value pair, from a 'Prop:' line. For
331 # example,
332 # Prop: apex.key -> 'com.android.runtime'
333 # Note that avbtool writes single or double quotes around values.
334 PROPERTY_DESCRIPTOR_PATTERN = r'^\s*(?P<key>.*?)\s->\s*(?P<value>.*?)$'
335
336 prop_matcher = re.compile(PROPERTY_DESCRIPTOR_PATTERN)
337 prop = prop_matcher.match(value)
338 if not prop:
339 raise ApexInfoError(
340 'Failed to parse prop string {}'.format(value))
341
342 prop_key, prop_value = prop.group('key'), prop.group('value')
343 if prop_key == 'apex.key':
344 # avbtool dumps the prop value with repr(), which contains single /
345 # double quotes that we don't want.
346 payload_info[prop_key] = prop_value.strip('\"\'')
347
348 else:
349 payload_info[key] = value
350
Ivan Lozanob021b2a2020-07-28 09:31:06 -0400351 # Validation check.
Jiyong Parka1887f32020-05-19 23:18:03 +0900352 for key in ('Algorithm', 'Salt', 'apex.key', 'Hash Algorithm'):
Tao Bao1cd59f22019-03-15 15:13:01 -0700353 if key not in payload_info:
354 raise ApexInfoError(
355 'Failed to find {} prop in {}'.format(key, payload_path))
356
357 return payload_info
Tao Baoe7354ba2019-05-09 16:54:15 -0700358
359
Nikita Ioffe36081482021-01-20 01:32:28 +0000360def SignUncompressedApex(avbtool, apex_file, payload_key, container_key,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000361 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200362 no_hashtree, signing_args=None, sign_tool=None,
363 is_sepolicy=False):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000364 """Signs the current uncompressed APEX with the given payload/container keys.
Tao Baoe7354ba2019-05-09 16:54:15 -0700365
366 Args:
Nikita Ioffe36081482021-01-20 01:32:28 +0000367 apex_file: Uncompressed APEX file.
Tao Baoe7354ba2019-05-09 16:54:15 -0700368 payload_key: The path to payload signing key (w/ extension).
369 container_key: The path to container signing key (w/o extension).
370 container_pw: The matching password of the container_key, or None.
Tianjie Xu88a759d2020-01-23 10:47:54 -0800371 apk_keys: A dict that holds the signing keys for apk files.
Tao Baoe7354ba2019-05-09 16:54:15 -0700372 codename_to_api_level_map: A dict that maps from codename to API level.
Tao Bao448004a2019-09-19 07:55:02 -0700373 no_hashtree: Don't include hashtree in the signed APEX.
Tao Baoe7354ba2019-05-09 16:54:15 -0700374 signing_args: Additional args to be passed to the payload signer.
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900375 sign_tool: A tool to sign the contents of the APEX.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200376 is_sepolicy: Indicates if the apex is a sepolicy.apex
Tao Baoe7354ba2019-05-09 16:54:15 -0700377
378 Returns:
379 The path to the signed APEX file.
380 """
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900381 # 1. Extract the apex payload image and sign the files (e.g. APKs). Repack
Tianjie Xu88a759d2020-01-23 10:47:54 -0800382 # the apex file after signing.
Tianjie Xu88a759d2020-01-23 10:47:54 -0800383 apk_signer = ApexApkSigner(apex_file, container_pw,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900384 codename_to_api_level_map,
Melisa Carranza Zúñiga8e3198a2022-04-13 16:23:45 +0000385 avbtool, sign_tool)
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200386 apex_file = apk_signer.ProcessApexFile(
387 apk_keys, payload_key, signing_args, is_sepolicy)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800388
389 # 2a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given
Tao Baoe7354ba2019-05-09 16:54:15 -0700390 # payload_key.
391 payload_dir = common.MakeTempDir(prefix='apex-payload-')
392 with zipfile.ZipFile(apex_file) as apex_fd:
393 payload_file = apex_fd.extract(APEX_PAYLOAD_IMAGE, payload_dir)
Baligh Uddin15881282019-08-25 12:01:44 -0700394 zip_items = apex_fd.namelist()
Tao Baoe7354ba2019-05-09 16:54:15 -0700395
Tao Bao1ac886e2019-06-26 11:58:22 -0700396 payload_info = ParseApexPayloadInfo(avbtool, payload_file)
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400397 if no_hashtree is None:
398 no_hashtree = payload_info.get("Tree Size", 0) == 0
Tao Baoe7354ba2019-05-09 16:54:15 -0700399 SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700400 avbtool,
Tao Baoe7354ba2019-05-09 16:54:15 -0700401 payload_file,
402 payload_key,
403 payload_info['apex.key'],
404 payload_info['Algorithm'],
405 payload_info['Salt'],
Jiyong Parka1887f32020-05-19 23:18:03 +0900406 payload_info['Hash Algorithm'],
Tao Bao448004a2019-09-19 07:55:02 -0700407 no_hashtree,
Tao Baoe7354ba2019-05-09 16:54:15 -0700408 signing_args)
409
Tianjie Xu88a759d2020-01-23 10:47:54 -0800410 # 2b. Update the embedded payload public key.
Tianjiec180a5d2020-03-23 18:14:09 -0700411 payload_public_key = common.ExtractAvbPublicKey(avbtool, payload_key)
Tao Baoe7354ba2019-05-09 16:54:15 -0700412 common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE)
Baligh Uddin15881282019-08-25 12:01:44 -0700413 if APEX_PUBKEY in zip_items:
414 common.ZipDelete(apex_file, APEX_PUBKEY)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400415 apex_zip = zipfile.ZipFile(apex_file, 'a', allowZip64=True)
Tao Baoe7354ba2019-05-09 16:54:15 -0700416 common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE)
417 common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY)
Kelvin Zhangf92f7f02023-04-14 21:32:54 +0000418 common.ZipClose(apex_zip)
Tao Baoe7354ba2019-05-09 16:54:15 -0700419
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900420 # 3. Sign the APEX container with container_key.
Tao Baoe7354ba2019-05-09 16:54:15 -0700421 signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex')
422
423 # Specify the 4K alignment when calling SignApk.
424 extra_signapk_args = OPTIONS.extra_signapk_args[:]
Jooyung Hanebe9afe2021-07-12 13:23:52 +0900425 extra_signapk_args.extend(['-a', '4096', '--align-file-size'])
Tao Baoe7354ba2019-05-09 16:54:15 -0700426
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000427 password = container_pw.get(container_key) if container_pw else None
Tao Baoe7354ba2019-05-09 16:54:15 -0700428 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900429 apex_file,
Tao Baoe7354ba2019-05-09 16:54:15 -0700430 signed_apex,
431 container_key,
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000432 password,
Tao Baoe7354ba2019-05-09 16:54:15 -0700433 codename_to_api_level_map=codename_to_api_level_map,
434 extra_signapk_args=extra_signapk_args)
435
436 return signed_apex
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000437
438
Nikita Ioffe36081482021-01-20 01:32:28 +0000439def SignCompressedApex(avbtool, apex_file, payload_key, container_key,
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400440 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200441 no_hashtree, signing_args=None, sign_tool=None,
442 is_sepolicy=False):
Nikita Ioffe36081482021-01-20 01:32:28 +0000443 """Signs the current compressed APEX with the given payload/container keys.
444
445 Args:
446 apex_file: Raw uncompressed APEX data.
447 payload_key: The path to payload signing key (w/ extension).
448 container_key: The path to container signing key (w/o extension).
449 container_pw: The matching password of the container_key, or None.
450 apk_keys: A dict that holds the signing keys for apk files.
451 codename_to_api_level_map: A dict that maps from codename to API level.
452 no_hashtree: Don't include hashtree in the signed APEX.
453 signing_args: Additional args to be passed to the payload signer.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200454 is_sepolicy: Indicates if the apex is a sepolicy.apex
Nikita Ioffe36081482021-01-20 01:32:28 +0000455
456 Returns:
457 The path to the signed APEX file.
458 """
459 debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static')
460
461 # 1. Decompress original_apex inside compressed apex.
462 original_apex_file = common.MakeTempFile(prefix='original-apex-',
463 suffix='.apex')
464 # Decompression target path should not exist
465 os.remove(original_apex_file)
466 common.RunAndCheckOutput(['deapexer', '--debugfs_path', debugfs_path,
467 'decompress', '--input', apex_file,
468 '--output', original_apex_file])
469
470 # 2. Sign original_apex
471 signed_original_apex_file = SignUncompressedApex(
472 avbtool,
473 original_apex_file,
474 payload_key,
475 container_key,
476 container_pw,
477 apk_keys,
478 codename_to_api_level_map,
479 no_hashtree,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900480 signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200481 sign_tool,
482 is_sepolicy)
Nikita Ioffe36081482021-01-20 01:32:28 +0000483
484 # 3. Compress signed original apex.
485 compressed_apex_file = common.MakeTempFile(prefix='apex-container-',
486 suffix='.capex')
487 common.RunAndCheckOutput(['apex_compression_tool',
488 'compress',
489 '--apex_compression_tool_path', os.getenv('PATH'),
490 '--input', signed_original_apex_file,
491 '--output', compressed_apex_file])
492
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900493 # 4. Sign the APEX container with container_key.
Nikita Ioffe36081482021-01-20 01:32:28 +0000494 signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.capex')
495
Nikita Ioffe36081482021-01-20 01:32:28 +0000496 password = container_pw.get(container_key) if container_pw else None
497 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900498 compressed_apex_file,
Nikita Ioffe36081482021-01-20 01:32:28 +0000499 signed_apex,
500 container_key,
501 password,
502 codename_to_api_level_map=codename_to_api_level_map,
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900503 extra_signapk_args=OPTIONS.extra_signapk_args)
Nikita Ioffe36081482021-01-20 01:32:28 +0000504
505 return signed_apex
506
507
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000508def SignApex(avbtool, apex_data, payload_key, container_key, container_pw,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200509 apk_keys, codename_to_api_level_map, no_hashtree,
510 signing_args=None, sign_tool=None, is_sepolicy=False):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000511 """Signs the current APEX with the given payload/container keys.
512
513 Args:
514 apex_file: Path to apex file path.
515 payload_key: The path to payload signing key (w/ extension).
516 container_key: The path to container signing key (w/o extension).
517 container_pw: The matching password of the container_key, or None.
518 apk_keys: A dict that holds the signing keys for apk files.
519 codename_to_api_level_map: A dict that maps from codename to API level.
520 no_hashtree: Don't include hashtree in the signed APEX.
521 signing_args: Additional args to be passed to the payload signer.
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200522 is_sepolicy: Indicates if the apex is a sepolicy.apex
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000523
524 Returns:
525 The path to the signed APEX file.
526 """
527 apex_file = common.MakeTempFile(prefix='apex-container-', suffix='.apex')
528 with open(apex_file, 'wb') as output_fp:
529 output_fp.write(apex_data)
530
Nikita Ioffe36081482021-01-20 01:32:28 +0000531 debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static')
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000532 cmd = ['deapexer', '--debugfs_path', debugfs_path,
533 'info', '--print-type', apex_file]
534
535 try:
536 apex_type = common.RunAndCheckOutput(cmd).strip()
537 if apex_type == 'UNCOMPRESSED':
538 return SignUncompressedApex(
539 avbtool,
Nikita Ioffe36081482021-01-20 01:32:28 +0000540 apex_file,
541 payload_key=payload_key,
542 container_key=container_key,
Kelvin Zhang137807d2022-09-14 15:02:46 -0700543 container_pw=container_pw,
Nikita Ioffe36081482021-01-20 01:32:28 +0000544 codename_to_api_level_map=codename_to_api_level_map,
545 no_hashtree=no_hashtree,
546 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900547 signing_args=signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200548 sign_tool=sign_tool,
549 is_sepolicy=is_sepolicy)
Nikita Ioffe36081482021-01-20 01:32:28 +0000550 elif apex_type == 'COMPRESSED':
551 return SignCompressedApex(
552 avbtool,
553 apex_file,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000554 payload_key=payload_key,
555 container_key=container_key,
Kelvin Zhang137807d2022-09-14 15:02:46 -0700556 container_pw=container_pw,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000557 codename_to_api_level_map=codename_to_api_level_map,
558 no_hashtree=no_hashtree,
559 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900560 signing_args=signing_args,
Melisa Carranza Zunigae0a977a2022-06-16 18:44:27 +0200561 sign_tool=sign_tool,
562 is_sepolicy=is_sepolicy)
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000563 else:
564 # TODO(b/172912232): support signing compressed apex
565 raise ApexInfoError('Unsupported apex type {}'.format(apex_type))
566
567 except common.ExternalError as e:
568 raise ApexInfoError(
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000569 'Failed to get type for {}:\n{}'.format(apex_file, e))
570
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400571
Daniel Normane9af70a2021-04-15 16:39:22 -0700572def GetApexInfoFromTargetFiles(input_file, partition, compressed_only=True):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000573 """
574 Get information about system APEX stored in the input_file zip
575
576 Args:
577 input_file: The filename of the target build target-files zip or directory.
578
579 Return:
580 A list of ota_metadata_pb2.ApexInfo() populated using the APEX stored in
581 /system partition of the input_file
582 """
583
584 # Extract the apex files so that we can run checks on them
585 if not isinstance(input_file, str):
586 raise RuntimeError("must pass filepath to target-files zip or directory")
587
Daniel Normane9af70a2021-04-15 16:39:22 -0700588 apex_subdir = os.path.join(partition.upper(), 'apex')
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000589 if os.path.isdir(input_file):
590 tmp_dir = input_file
591 else:
Daniel Normane9af70a2021-04-15 16:39:22 -0700592 tmp_dir = UnzipTemp(input_file, [os.path.join(apex_subdir, '*')])
593 target_dir = os.path.join(tmp_dir, apex_subdir)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000594
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800595 # Partial target-files packages for vendor-only builds may not contain
596 # a system apex directory.
597 if not os.path.exists(target_dir):
Daniel Normane9af70a2021-04-15 16:39:22 -0700598 logger.info('No APEX directory at path: %s', target_dir)
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800599 return []
600
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000601 apex_infos = []
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500602
603 debugfs_path = "debugfs"
604 if OPTIONS.search_path:
605 debugfs_path = os.path.join(OPTIONS.search_path, "bin", "debugfs_static")
606 deapexer = 'deapexer'
607 if OPTIONS.search_path:
Kelvin Zhang05a3f682021-01-29 14:38:24 -0500608 deapexer_path = os.path.join(OPTIONS.search_path, "bin", "deapexer")
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500609 if os.path.isfile(deapexer_path):
610 deapexer = deapexer_path
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000611 for apex_filename in os.listdir(target_dir):
612 apex_filepath = os.path.join(target_dir, apex_filename)
613 if not os.path.isfile(apex_filepath) or \
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400614 not zipfile.is_zipfile(apex_filepath):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000615 logger.info("Skipping %s because it's not a zipfile", apex_filepath)
616 continue
617 apex_info = ota_metadata_pb2.ApexInfo()
618 # Open the apex file to retrieve information
619 manifest = apex_manifest.fromApex(apex_filepath)
620 apex_info.package_name = manifest.name
621 apex_info.version = manifest.version
622 # Check if the file is compressed or not
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000623 apex_type = RunAndCheckOutput([
624 deapexer, "--debugfs_path", debugfs_path,
625 'info', '--print-type', apex_filepath]).rstrip()
626 if apex_type == 'COMPRESSED':
627 apex_info.is_compressed = True
628 elif apex_type == 'UNCOMPRESSED':
629 apex_info.is_compressed = False
630 else:
631 raise RuntimeError('Not an APEX file: ' + apex_type)
632
633 # Decompress compressed APEX to determine its size
634 if apex_info.is_compressed:
635 decompressed_file_path = MakeTempFile(prefix="decompressed-",
636 suffix=".apex")
637 # Decompression target path should not exist
638 os.remove(decompressed_file_path)
639 RunAndCheckOutput([deapexer, 'decompress', '--input', apex_filepath,
640 '--output', decompressed_file_path])
641 apex_info.decompressed_size = os.path.getsize(decompressed_file_path)
642
Daniel Normane9af70a2021-04-15 16:39:22 -0700643 if not compressed_only or apex_info.is_compressed:
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500644 apex_infos.append(apex_info)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000645
646 return apex_infos