blob: 69d6c137f1eb5381cf6733239f6e1defacc24c43 [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 Zuniga46930d72022-02-11 13:43:18 +010057 def __init__(self, apex_path, key_passwords, codename_to_api_level_map, avbtool=None, sign_tool=None, fsverity_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
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010068 self.fsverity_tool = fsverity_tool if fsverity_tool else "fsverity"
Tianjie Xu88a759d2020-01-23 10:47:54 -080069
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010070 def ProcessApexFile(self, apk_keys, payload_key, signing_args=None, is_sepolicy=False, sepolicy_key=None, sepolicy_cert=None):
Jooyung Han0f5a41d2021-10-27 03:53:21 +090071 """Scans and signs the payload files and repack the apex
Tianjie Xu88a759d2020-01-23 10:47:54 -080072
73 Args:
74 apk_keys: A dict that holds the signing keys for apk files.
Tianjie Xu88a759d2020-01-23 10:47:54 -080075
76 Returns:
77 The repacked apex file containing the signed apk files.
78 """
Kelvin Zhangdd833dc2020-08-21 14:13:13 -040079 if not os.path.exists(self.debugfs_path):
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040080 raise ApexSigningError(
81 "Couldn't find location of debugfs_static: " +
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +000082 "Path {} does not exist. ".format(self.debugfs_path) +
Kelvin Zhangd6b799a2020-08-19 14:54:42 -040083 "Make sure bin/debugfs_static can be found in -p <path>")
84 list_cmd = ['deapexer', '--debugfs_path',
Kelvin Zhangdd833dc2020-08-21 14:13:13 -040085 self.debugfs_path, 'list', self.apex_path]
Tianjie Xu88a759d2020-01-23 10:47:54 -080086 entries_names = common.RunAndCheckOutput(list_cmd).split()
87 apk_entries = [name for name in entries_names if name.endswith('.apk')]
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010088 sepolicy_entries = []
89 if is_sepolicy:
90 sepolicy_entries = [name for name in entries_names if
91 name.startswith('./etc/SEPolicy') and name.endswith('.zip')]
Tianjie Xu88a759d2020-01-23 10:47:54 -080092
93 # No need to sign and repack, return the original apex path.
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +010094 if not apk_entries and not sepolicy_entries and self.sign_tool is None:
95 logger.info('No payload (apk or zip) file to sign in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -080096 return self.apex_path
97
98 for entry in apk_entries:
99 apk_name = os.path.basename(entry)
100 if apk_name not in apk_keys:
101 raise ApexSigningError('Failed to find signing keys for apk file {} in'
102 ' apex {}. Use "-e <apkname>=" to specify a key'
103 .format(entry, self.apex_path))
104 if not any(dirname in entry for dirname in ['app/', 'priv-app/',
105 'overlay/']):
106 logger.warning('Apk path does not contain the intended directory name:'
107 ' %s', entry)
108
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100109 payload_dir, has_signed_content = self.ExtractApexPayloadAndSignContents(apk_entries,
110 apk_keys, payload_key, sepolicy_entries, sepolicy_key, sepolicy_cert, signing_args)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900111 if not has_signed_content:
112 logger.info('No contents has been signed in %s', self.apex_path)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800113 return self.apex_path
114
Baligh Uddin639b3b72020-03-25 20:50:23 -0700115 return self.RepackApexPayload(payload_dir, payload_key, signing_args)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800116
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100117 def ExtractApexPayloadAndSignContents(self, apk_entries, apk_keys, payload_key,
118 sepolicy_entries, sepolicy_key, sepolicy_cert, 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>")
Tianjie Xu88a759d2020-01-23 10:47:54 -0800125 payload_dir = common.MakeTempDir()
Kelvin Zhangdd833dc2020-08-21 14:13:13 -0400126 extract_cmd = ['deapexer', '--debugfs_path',
127 self.debugfs_path, 'extract', self.apex_path, payload_dir]
Tianjie Xu88a759d2020-01-23 10:47:54 -0800128 common.RunAndCheckOutput(extract_cmd)
129
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900130 has_signed_content = False
Tianjie Xu88a759d2020-01-23 10:47:54 -0800131 for entry in apk_entries:
132 apk_path = os.path.join(payload_dir, entry)
133 assert os.path.exists(self.apex_path)
134
135 key_name = apk_keys.get(os.path.basename(entry))
136 if key_name in common.SPECIAL_CERT_STRINGS:
137 logger.info('Not signing: %s due to special cert string', apk_path)
138 continue
139
140 logger.info('Signing apk file %s in apex %s', apk_path, self.apex_path)
141 # Rename the unsigned apk and overwrite the original apk path with the
142 # signed apk file.
143 unsigned_apk = common.MakeTempFile()
144 os.rename(apk_path, unsigned_apk)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000145 common.SignFile(
146 unsigned_apk, apk_path, key_name, self.key_passwords.get(key_name),
147 codename_to_api_level_map=self.codename_to_api_level_map)
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900148 has_signed_content = True
149
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100150 for entry in sepolicy_entries:
151 sepolicy_key = sepolicy_key if sepolicy_key else payload_key
152 self.SignSePolicy(payload_dir, entry, sepolicy_key, sepolicy_cert)
153 has_signed_content = True
154
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900155 if self.sign_tool:
Jooyung Han8caba5e2021-10-27 03:58:09 +0900156 logger.info('Signing payload contents in apex %s with %s', self.apex_path, self.sign_tool)
Jooyung Han39259ec2022-02-07 15:56:53 +0900157 # Pass avbtool to the custom signing tool
158 cmd = [self.sign_tool, '--avbtool', self.avbtool]
159 # Pass signing_args verbatim which will be forwarded to avbtool (e.g. --signing_helper=...)
160 if signing_args:
161 cmd.extend(['--signing_args', '"{}"'.format(signing_args)])
162 cmd.extend([payload_key, payload_dir])
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900163 common.RunAndCheckOutput(cmd)
164 has_signed_content = True
165
166 return payload_dir, has_signed_content
Tianjie Xu88a759d2020-01-23 10:47:54 -0800167
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100168 def SignSePolicy(self, payload_dir, sepolicy_zip, sepolicy_key, sepolicy_cert):
169 sepolicy_sig = sepolicy_zip + '.sig'
170 sepolicy_fsv_sig = sepolicy_zip + '.fsv_sig'
171
172 policy_zip_path = os.path.join(payload_dir, sepolicy_zip)
173 sig_out_path = os.path.join(payload_dir, sepolicy_sig)
174 sig_old = sig_out_path + '.old'
175 if os.path.exists(sig_out_path):
176 os.rename(sig_out_path, sig_old)
177 sign_cmd = ['openssl', 'dgst', '-sign', sepolicy_key, '-keyform', 'PEM', '-sha256',
178 '-out', sig_out_path, '-binary', policy_zip_path]
179 common.RunAndCheckOutput(sign_cmd)
180 if os.path.exists(sig_old):
181 os.remove(sig_old)
182
183 if not sepolicy_cert:
184 logger.info('No cert provided for SEPolicy, skipping fsverity sign')
185 return
186
187 fsv_sig_out_path = os.path.join(payload_dir, sepolicy_fsv_sig)
188 fsv_sig_old = fsv_sig_out_path + '.old'
189 if os.path.exists(fsv_sig_out_path):
190 os.rename(fsv_sig_out_path, fsv_sig_old)
191
192 fsverity_cmd = [self.fsverity_tool, 'sign', policy_zip_path, fsv_sig_out_path,
193 '--key=' + sepolicy_key, '--cert=' + sepolicy_cert]
194 common.RunAndCheckOutput(fsverity_cmd)
195 if os.path.exists(fsv_sig_old):
196 os.remove(fsv_sig_old)
197
Baligh Uddin639b3b72020-03-25 20:50:23 -0700198 def RepackApexPayload(self, payload_dir, payload_key, signing_args=None):
Tianjie Xu88a759d2020-01-23 10:47:54 -0800199 """Rebuilds the apex file with the updated payload directory."""
200 apex_dir = common.MakeTempDir()
201 # Extract the apex file and reuse its meta files as repack parameters.
202 common.UnzipToDir(self.apex_path, apex_dir)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800203 arguments_dict = {
204 'manifest': os.path.join(apex_dir, 'apex_manifest.pb'),
205 'build_info': os.path.join(apex_dir, 'apex_build_info.pb'),
Tianjie Xu88a759d2020-01-23 10:47:54 -0800206 'key': payload_key,
Tianjie Xu88a759d2020-01-23 10:47:54 -0800207 }
208 for filename in arguments_dict.values():
209 assert os.path.exists(filename), 'file {} not found'.format(filename)
210
211 # The repack process will add back these files later in the payload image.
212 for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']:
213 path = os.path.join(payload_dir, name)
214 if os.path.isfile(path):
215 os.remove(path)
216 elif os.path.isdir(path):
217 shutil.rmtree(path)
218
Tianjiec180a5d2020-03-23 18:14:09 -0700219 # TODO(xunchang) the signing process can be improved by using
220 # '--unsigned_payload_only'. But we need to parse the vbmeta earlier for
221 # the signing arguments, e.g. algorithm, salt, etc.
222 payload_img = os.path.join(apex_dir, APEX_PAYLOAD_IMAGE)
223 generate_image_cmd = ['apexer', '--force', '--payload_only',
224 '--do_not_check_keyname', '--apexer_tool_path',
225 os.getenv('PATH')]
Tianjie Xu88a759d2020-01-23 10:47:54 -0800226 for key, val in arguments_dict.items():
Tianjiec180a5d2020-03-23 18:14:09 -0700227 generate_image_cmd.extend(['--' + key, val])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700228
229 # Add quote to the signing_args as we will pass
230 # --signing_args "--signing_helper_with_files=%path" to apexer
231 if signing_args:
Kelvin Zhangd6b799a2020-08-19 14:54:42 -0400232 generate_image_cmd.extend(
233 ['--signing_args', '"{}"'.format(signing_args)])
Baligh Uddin639b3b72020-03-25 20:50:23 -0700234
Tianjie Xu83bd55c2020-01-29 11:37:43 -0800235 # optional arguments for apex repacking
Tianjie Xu88a759d2020-01-23 10:47:54 -0800236 manifest_json = os.path.join(apex_dir, 'apex_manifest.json')
237 if os.path.exists(manifest_json):
Tianjiec180a5d2020-03-23 18:14:09 -0700238 generate_image_cmd.extend(['--manifest_json', manifest_json])
239 generate_image_cmd.extend([payload_dir, payload_img])
Tianjie Xucea6ad12020-01-30 17:12:05 -0800240 if OPTIONS.verbose:
Tianjiec180a5d2020-03-23 18:14:09 -0700241 generate_image_cmd.append('-v')
242 common.RunAndCheckOutput(generate_image_cmd)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800243
Tianjiec180a5d2020-03-23 18:14:09 -0700244 # Add the payload image back to the apex file.
245 common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400246 with zipfile.ZipFile(self.apex_path, 'a', allowZip64=True) as output_apex:
Tianjiec180a5d2020-03-23 18:14:09 -0700247 common.ZipWrite(output_apex, payload_img, APEX_PAYLOAD_IMAGE,
248 compress_type=zipfile.ZIP_STORED)
249 return self.apex_path
Tianjie Xu88a759d2020-01-23 10:47:54 -0800250
251
Tao Bao1ac886e2019-06-26 11:58:22 -0700252def SignApexPayload(avbtool, payload_file, payload_key_path, payload_key_name,
Jiyong Parka1887f32020-05-19 23:18:03 +0900253 algorithm, salt, hash_algorithm, no_hashtree, signing_args=None):
Tao Bao1cd59f22019-03-15 15:13:01 -0700254 """Signs a given payload_file with the payload key."""
255 # Add the new footer. Old footer, if any, will be replaced by avbtool.
Tao Bao1ac886e2019-06-26 11:58:22 -0700256 cmd = [avbtool, 'add_hashtree_footer',
Tao Bao1cd59f22019-03-15 15:13:01 -0700257 '--do_not_generate_fec',
258 '--algorithm', algorithm,
259 '--key', payload_key_path,
260 '--prop', 'apex.key:{}'.format(payload_key_name),
261 '--image', payload_file,
Jiyong Parka1887f32020-05-19 23:18:03 +0900262 '--salt', salt,
263 '--hash_algorithm', hash_algorithm]
Tao Bao448004a2019-09-19 07:55:02 -0700264 if no_hashtree:
265 cmd.append('--no_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700266 if signing_args:
267 cmd.extend(shlex.split(signing_args))
268
269 try:
270 common.RunAndCheckOutput(cmd)
271 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700272 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700273 'Failed to sign APEX payload {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700274 payload_file, payload_key_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700275
276 # Verify the signed payload image with specified public key.
277 logger.info('Verifying %s', payload_file)
Tao Bao448004a2019-09-19 07:55:02 -0700278 VerifyApexPayload(avbtool, payload_file, payload_key_path, no_hashtree)
Tao Bao1cd59f22019-03-15 15:13:01 -0700279
280
Tao Bao448004a2019-09-19 07:55:02 -0700281def VerifyApexPayload(avbtool, payload_file, payload_key, no_hashtree=False):
Tao Bao1cd59f22019-03-15 15:13:01 -0700282 """Verifies the APEX payload signature with the given key."""
Tao Bao1ac886e2019-06-26 11:58:22 -0700283 cmd = [avbtool, 'verify_image', '--image', payload_file,
Tao Bao1cd59f22019-03-15 15:13:01 -0700284 '--key', payload_key]
Tao Bao448004a2019-09-19 07:55:02 -0700285 if no_hashtree:
286 cmd.append('--accept_zeroed_hashtree')
Tao Bao1cd59f22019-03-15 15:13:01 -0700287 try:
288 common.RunAndCheckOutput(cmd)
289 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700290 raise ApexSigningError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700291 'Failed to validate payload signing for {} with {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700292 payload_file, payload_key, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700293
294
Tao Bao1ac886e2019-06-26 11:58:22 -0700295def ParseApexPayloadInfo(avbtool, payload_path):
Tao Bao1cd59f22019-03-15 15:13:01 -0700296 """Parses the APEX payload info.
297
298 Args:
Tao Bao1ac886e2019-06-26 11:58:22 -0700299 avbtool: The AVB tool to use.
Tao Bao1cd59f22019-03-15 15:13:01 -0700300 payload_path: The path to the payload image.
301
302 Raises:
303 ApexInfoError on parsing errors.
304
305 Returns:
306 A dict that contains payload property-value pairs. The dict should at least
Tao Bao448004a2019-09-19 07:55:02 -0700307 contain Algorithm, Salt, Tree Size and apex.key.
Tao Bao1cd59f22019-03-15 15:13:01 -0700308 """
309 if not os.path.exists(payload_path):
310 raise ApexInfoError('Failed to find image: {}'.format(payload_path))
311
Tao Bao1ac886e2019-06-26 11:58:22 -0700312 cmd = [avbtool, 'info_image', '--image', payload_path]
Tao Bao1cd59f22019-03-15 15:13:01 -0700313 try:
314 output = common.RunAndCheckOutput(cmd)
315 except common.ExternalError as e:
Tao Bao86b529a2019-06-19 17:03:37 -0700316 raise ApexInfoError(
Tao Bao1cd59f22019-03-15 15:13:01 -0700317 'Failed to get APEX payload info for {}:\n{}'.format(
Tao Bao86b529a2019-06-19 17:03:37 -0700318 payload_path, e))
Tao Bao1cd59f22019-03-15 15:13:01 -0700319
Jiyong Parka1887f32020-05-19 23:18:03 +0900320 # Extract the Algorithm / Hash Algorithm / Salt / Prop info / Tree size from
321 # payload (i.e. an image signed with avbtool). For example,
Tao Bao1cd59f22019-03-15 15:13:01 -0700322 # Algorithm: SHA256_RSA4096
323 PAYLOAD_INFO_PATTERN = (
Jiyong Parka1887f32020-05-19 23:18:03 +0900324 r'^\s*(?P<key>Algorithm|Hash Algorithm|Salt|Prop|Tree Size)\:\s*(?P<value>.*?)$')
Tao Bao1cd59f22019-03-15 15:13:01 -0700325 payload_info_matcher = re.compile(PAYLOAD_INFO_PATTERN)
326
327 payload_info = {}
328 for line in output.split('\n'):
329 line_info = payload_info_matcher.match(line)
330 if not line_info:
331 continue
332
333 key, value = line_info.group('key'), line_info.group('value')
334
335 if key == 'Prop':
336 # Further extract the property key-value pair, from a 'Prop:' line. For
337 # example,
338 # Prop: apex.key -> 'com.android.runtime'
339 # Note that avbtool writes single or double quotes around values.
340 PROPERTY_DESCRIPTOR_PATTERN = r'^\s*(?P<key>.*?)\s->\s*(?P<value>.*?)$'
341
342 prop_matcher = re.compile(PROPERTY_DESCRIPTOR_PATTERN)
343 prop = prop_matcher.match(value)
344 if not prop:
345 raise ApexInfoError(
346 'Failed to parse prop string {}'.format(value))
347
348 prop_key, prop_value = prop.group('key'), prop.group('value')
349 if prop_key == 'apex.key':
350 # avbtool dumps the prop value with repr(), which contains single /
351 # double quotes that we don't want.
352 payload_info[prop_key] = prop_value.strip('\"\'')
353
354 else:
355 payload_info[key] = value
356
Ivan Lozanob021b2a2020-07-28 09:31:06 -0400357 # Validation check.
Jiyong Parka1887f32020-05-19 23:18:03 +0900358 for key in ('Algorithm', 'Salt', 'apex.key', 'Hash Algorithm'):
Tao Bao1cd59f22019-03-15 15:13:01 -0700359 if key not in payload_info:
360 raise ApexInfoError(
361 'Failed to find {} prop in {}'.format(key, payload_path))
362
363 return payload_info
Tao Baoe7354ba2019-05-09 16:54:15 -0700364
365
Nikita Ioffe36081482021-01-20 01:32:28 +0000366def SignUncompressedApex(avbtool, apex_file, payload_key, container_key,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000367 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100368 no_hashtree, signing_args=None, sign_tool=None,
369 is_sepolicy=False, sepolicy_key=None, sepolicy_cert=None,
370 fsverity_tool=None):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000371 """Signs the current uncompressed APEX with the given payload/container keys.
Tao Baoe7354ba2019-05-09 16:54:15 -0700372
373 Args:
Nikita Ioffe36081482021-01-20 01:32:28 +0000374 apex_file: Uncompressed APEX file.
Tao Baoe7354ba2019-05-09 16:54:15 -0700375 payload_key: The path to payload signing key (w/ extension).
376 container_key: The path to container signing key (w/o extension).
377 container_pw: The matching password of the container_key, or None.
Tianjie Xu88a759d2020-01-23 10:47:54 -0800378 apk_keys: A dict that holds the signing keys for apk files.
Tao Baoe7354ba2019-05-09 16:54:15 -0700379 codename_to_api_level_map: A dict that maps from codename to API level.
Tao Bao448004a2019-09-19 07:55:02 -0700380 no_hashtree: Don't include hashtree in the signed APEX.
Tao Baoe7354ba2019-05-09 16:54:15 -0700381 signing_args: Additional args to be passed to the payload signer.
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900382 sign_tool: A tool to sign the contents of the APEX.
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100383 is_sepolicy: Indicates if the apex is a sepolicy.apex
384 sepolicy_key: Key to sign a sepolicy zip.
385 sepolicy_cert: Cert to sign a sepolicy zip.
386 fsverity_tool: fsverity path to sign sepolicy zip.
Tao Baoe7354ba2019-05-09 16:54:15 -0700387
388 Returns:
389 The path to the signed APEX file.
390 """
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900391 # 1. Extract the apex payload image and sign the files (e.g. APKs). Repack
Tianjie Xu88a759d2020-01-23 10:47:54 -0800392 # the apex file after signing.
Tianjie Xu88a759d2020-01-23 10:47:54 -0800393 apk_signer = ApexApkSigner(apex_file, container_pw,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900394 codename_to_api_level_map,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100395 avbtool, sign_tool, fsverity_tool)
396 apex_file = apk_signer.ProcessApexFile(
397 apk_keys, payload_key, signing_args, is_sepolicy, sepolicy_key, sepolicy_cert)
Tianjie Xu88a759d2020-01-23 10:47:54 -0800398
399 # 2a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given
Tao Baoe7354ba2019-05-09 16:54:15 -0700400 # payload_key.
401 payload_dir = common.MakeTempDir(prefix='apex-payload-')
402 with zipfile.ZipFile(apex_file) as apex_fd:
403 payload_file = apex_fd.extract(APEX_PAYLOAD_IMAGE, payload_dir)
Baligh Uddin15881282019-08-25 12:01:44 -0700404 zip_items = apex_fd.namelist()
Tao Baoe7354ba2019-05-09 16:54:15 -0700405
Tao Bao1ac886e2019-06-26 11:58:22 -0700406 payload_info = ParseApexPayloadInfo(avbtool, payload_file)
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400407 if no_hashtree is None:
408 no_hashtree = payload_info.get("Tree Size", 0) == 0
Tao Baoe7354ba2019-05-09 16:54:15 -0700409 SignApexPayload(
Tao Bao1ac886e2019-06-26 11:58:22 -0700410 avbtool,
Tao Baoe7354ba2019-05-09 16:54:15 -0700411 payload_file,
412 payload_key,
413 payload_info['apex.key'],
414 payload_info['Algorithm'],
415 payload_info['Salt'],
Jiyong Parka1887f32020-05-19 23:18:03 +0900416 payload_info['Hash Algorithm'],
Tao Bao448004a2019-09-19 07:55:02 -0700417 no_hashtree,
Tao Baoe7354ba2019-05-09 16:54:15 -0700418 signing_args)
419
Tianjie Xu88a759d2020-01-23 10:47:54 -0800420 # 2b. Update the embedded payload public key.
Tianjiec180a5d2020-03-23 18:14:09 -0700421 payload_public_key = common.ExtractAvbPublicKey(avbtool, payload_key)
Tao Baoe7354ba2019-05-09 16:54:15 -0700422 common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE)
Baligh Uddin15881282019-08-25 12:01:44 -0700423 if APEX_PUBKEY in zip_items:
424 common.ZipDelete(apex_file, APEX_PUBKEY)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400425 apex_zip = zipfile.ZipFile(apex_file, 'a', allowZip64=True)
Tao Baoe7354ba2019-05-09 16:54:15 -0700426 common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE)
427 common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY)
428 common.ZipClose(apex_zip)
429
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900430 # 3. Sign the APEX container with container_key.
Tao Baoe7354ba2019-05-09 16:54:15 -0700431 signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex')
432
433 # Specify the 4K alignment when calling SignApk.
434 extra_signapk_args = OPTIONS.extra_signapk_args[:]
Jooyung Hanebe9afe2021-07-12 13:23:52 +0900435 extra_signapk_args.extend(['-a', '4096', '--align-file-size'])
Tao Baoe7354ba2019-05-09 16:54:15 -0700436
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000437 password = container_pw.get(container_key) if container_pw else None
Tao Baoe7354ba2019-05-09 16:54:15 -0700438 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900439 apex_file,
Tao Baoe7354ba2019-05-09 16:54:15 -0700440 signed_apex,
441 container_key,
Nikita Ioffec3fdfed2021-01-11 23:50:31 +0000442 password,
Tao Baoe7354ba2019-05-09 16:54:15 -0700443 codename_to_api_level_map=codename_to_api_level_map,
444 extra_signapk_args=extra_signapk_args)
445
446 return signed_apex
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000447
448
Nikita Ioffe36081482021-01-20 01:32:28 +0000449def SignCompressedApex(avbtool, apex_file, payload_key, container_key,
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400450 container_pw, apk_keys, codename_to_api_level_map,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100451 no_hashtree, signing_args=None, sign_tool=None,
452 is_sepolicy=False, sepolicy_key=None, sepolicy_cert=None,
453 fsverity_tool=None):
Nikita Ioffe36081482021-01-20 01:32:28 +0000454 """Signs the current compressed APEX with the given payload/container keys.
455
456 Args:
457 apex_file: Raw uncompressed APEX data.
458 payload_key: The path to payload signing key (w/ extension).
459 container_key: The path to container signing key (w/o extension).
460 container_pw: The matching password of the container_key, or None.
461 apk_keys: A dict that holds the signing keys for apk files.
462 codename_to_api_level_map: A dict that maps from codename to API level.
463 no_hashtree: Don't include hashtree in the signed APEX.
464 signing_args: Additional args to be passed to the payload signer.
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100465 is_sepolicy: Indicates if the apex is a sepolicy.apex
466 sepolicy_key: Key to sign a sepolicy zip.
467 sepolicy_cert: Cert to sign a sepolicy zip.
468 fsverity_tool: fsverity path to sign sepolicy zip.
Nikita Ioffe36081482021-01-20 01:32:28 +0000469
470 Returns:
471 The path to the signed APEX file.
472 """
473 debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static')
474
475 # 1. Decompress original_apex inside compressed apex.
476 original_apex_file = common.MakeTempFile(prefix='original-apex-',
477 suffix='.apex')
478 # Decompression target path should not exist
479 os.remove(original_apex_file)
480 common.RunAndCheckOutput(['deapexer', '--debugfs_path', debugfs_path,
481 'decompress', '--input', apex_file,
482 '--output', original_apex_file])
483
484 # 2. Sign original_apex
485 signed_original_apex_file = SignUncompressedApex(
486 avbtool,
487 original_apex_file,
488 payload_key,
489 container_key,
490 container_pw,
491 apk_keys,
492 codename_to_api_level_map,
493 no_hashtree,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900494 signing_args,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100495 sign_tool,
496 is_sepolicy,
497 sepolicy_key,
498 sepolicy_cert,
499 fsverity_tool)
Nikita Ioffe36081482021-01-20 01:32:28 +0000500
501 # 3. Compress signed original apex.
502 compressed_apex_file = common.MakeTempFile(prefix='apex-container-',
503 suffix='.capex')
504 common.RunAndCheckOutput(['apex_compression_tool',
505 'compress',
506 '--apex_compression_tool_path', os.getenv('PATH'),
507 '--input', signed_original_apex_file,
508 '--output', compressed_apex_file])
509
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900510 # 4. Sign the APEX container with container_key.
Nikita Ioffe36081482021-01-20 01:32:28 +0000511 signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.capex')
512
Nikita Ioffe36081482021-01-20 01:32:28 +0000513 password = container_pw.get(container_key) if container_pw else None
514 common.SignFile(
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900515 compressed_apex_file,
Nikita Ioffe36081482021-01-20 01:32:28 +0000516 signed_apex,
517 container_key,
518 password,
519 codename_to_api_level_map=codename_to_api_level_map,
Jooyung Hanf9be5ee2021-07-22 18:21:25 +0900520 extra_signapk_args=OPTIONS.extra_signapk_args)
Nikita Ioffe36081482021-01-20 01:32:28 +0000521
522 return signed_apex
523
524
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000525def SignApex(avbtool, apex_data, payload_key, container_key, container_pw,
526 apk_keys, codename_to_api_level_map,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100527 no_hashtree, signing_args=None, sign_tool=None,
528 is_sepolicy=False, sepolicy_key=None, sepolicy_cert=None, fsverity_tool=None):
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000529 """Signs the current APEX with the given payload/container keys.
530
531 Args:
532 apex_file: Path to apex file path.
533 payload_key: The path to payload signing key (w/ extension).
534 container_key: The path to container signing key (w/o extension).
535 container_pw: The matching password of the container_key, or None.
536 apk_keys: A dict that holds the signing keys for apk files.
537 codename_to_api_level_map: A dict that maps from codename to API level.
538 no_hashtree: Don't include hashtree in the signed APEX.
539 signing_args: Additional args to be passed to the payload signer.
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100540 sepolicy_key: Key to sign a sepolicy zip.
541 sepolicy_cert: Cert to sign a sepolicy zip.
542 fsverity_tool: fsverity path to sign sepolicy zip.
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000543
544 Returns:
545 The path to the signed APEX file.
546 """
547 apex_file = common.MakeTempFile(prefix='apex-container-', suffix='.apex')
548 with open(apex_file, 'wb') as output_fp:
549 output_fp.write(apex_data)
550
Nikita Ioffe36081482021-01-20 01:32:28 +0000551 debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static')
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000552 cmd = ['deapexer', '--debugfs_path', debugfs_path,
553 'info', '--print-type', apex_file]
554
555 try:
556 apex_type = common.RunAndCheckOutput(cmd).strip()
557 if apex_type == 'UNCOMPRESSED':
558 return SignUncompressedApex(
559 avbtool,
Nikita Ioffe36081482021-01-20 01:32:28 +0000560 apex_file,
561 payload_key=payload_key,
562 container_key=container_key,
563 container_pw=None,
564 codename_to_api_level_map=codename_to_api_level_map,
565 no_hashtree=no_hashtree,
566 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900567 signing_args=signing_args,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100568 sign_tool=sign_tool,
569 is_sepolicy=is_sepolicy,
570 sepolicy_key=sepolicy_key,
571 sepolicy_cert=sepolicy_cert,
572 fsverity_tool=fsverity_tool)
Nikita Ioffe36081482021-01-20 01:32:28 +0000573 elif apex_type == 'COMPRESSED':
574 return SignCompressedApex(
575 avbtool,
576 apex_file,
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000577 payload_key=payload_key,
578 container_key=container_key,
579 container_pw=None,
580 codename_to_api_level_map=codename_to_api_level_map,
581 no_hashtree=no_hashtree,
582 apk_keys=apk_keys,
Jooyung Han0f5a41d2021-10-27 03:53:21 +0900583 signing_args=signing_args,
Melisa Carranza Zuniga46930d72022-02-11 13:43:18 +0100584 sign_tool=sign_tool,
585 is_sepolicy=is_sepolicy,
586 sepolicy_key=sepolicy_key,
587 sepolicy_cert=sepolicy_cert,
588 fsverity_tool=fsverity_tool)
Nikita Ioffe6068e8d2021-01-12 00:03:02 +0000589 else:
590 # TODO(b/172912232): support signing compressed apex
591 raise ApexInfoError('Unsupported apex type {}'.format(apex_type))
592
593 except common.ExternalError as e:
594 raise ApexInfoError(
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000595 'Failed to get type for {}:\n{}'.format(apex_file, e))
596
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400597
Daniel Normane9af70a2021-04-15 16:39:22 -0700598def GetApexInfoFromTargetFiles(input_file, partition, compressed_only=True):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000599 """
600 Get information about system APEX stored in the input_file zip
601
602 Args:
603 input_file: The filename of the target build target-files zip or directory.
604
605 Return:
606 A list of ota_metadata_pb2.ApexInfo() populated using the APEX stored in
607 /system partition of the input_file
608 """
609
610 # Extract the apex files so that we can run checks on them
611 if not isinstance(input_file, str):
612 raise RuntimeError("must pass filepath to target-files zip or directory")
613
Daniel Normane9af70a2021-04-15 16:39:22 -0700614 apex_subdir = os.path.join(partition.upper(), 'apex')
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000615 if os.path.isdir(input_file):
616 tmp_dir = input_file
617 else:
Daniel Normane9af70a2021-04-15 16:39:22 -0700618 tmp_dir = UnzipTemp(input_file, [os.path.join(apex_subdir, '*')])
619 target_dir = os.path.join(tmp_dir, apex_subdir)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000620
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800621 # Partial target-files packages for vendor-only builds may not contain
622 # a system apex directory.
623 if not os.path.exists(target_dir):
Daniel Normane9af70a2021-04-15 16:39:22 -0700624 logger.info('No APEX directory at path: %s', target_dir)
Daniel Normanb4b07ab2021-02-17 13:22:21 -0800625 return []
626
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000627 apex_infos = []
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500628
629 debugfs_path = "debugfs"
630 if OPTIONS.search_path:
631 debugfs_path = os.path.join(OPTIONS.search_path, "bin", "debugfs_static")
632 deapexer = 'deapexer'
633 if OPTIONS.search_path:
Kelvin Zhang05a3f682021-01-29 14:38:24 -0500634 deapexer_path = os.path.join(OPTIONS.search_path, "bin", "deapexer")
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500635 if os.path.isfile(deapexer_path):
636 deapexer = deapexer_path
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000637 for apex_filename in os.listdir(target_dir):
638 apex_filepath = os.path.join(target_dir, apex_filename)
639 if not os.path.isfile(apex_filepath) or \
Kelvin Zhang7cab7502021-08-02 19:58:14 -0400640 not zipfile.is_zipfile(apex_filepath):
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000641 logger.info("Skipping %s because it's not a zipfile", apex_filepath)
642 continue
643 apex_info = ota_metadata_pb2.ApexInfo()
644 # Open the apex file to retrieve information
645 manifest = apex_manifest.fromApex(apex_filepath)
646 apex_info.package_name = manifest.name
647 apex_info.version = manifest.version
648 # Check if the file is compressed or not
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000649 apex_type = RunAndCheckOutput([
650 deapexer, "--debugfs_path", debugfs_path,
651 'info', '--print-type', apex_filepath]).rstrip()
652 if apex_type == 'COMPRESSED':
653 apex_info.is_compressed = True
654 elif apex_type == 'UNCOMPRESSED':
655 apex_info.is_compressed = False
656 else:
657 raise RuntimeError('Not an APEX file: ' + apex_type)
658
659 # Decompress compressed APEX to determine its size
660 if apex_info.is_compressed:
661 decompressed_file_path = MakeTempFile(prefix="decompressed-",
662 suffix=".apex")
663 # Decompression target path should not exist
664 os.remove(decompressed_file_path)
665 RunAndCheckOutput([deapexer, 'decompress', '--input', apex_filepath,
666 '--output', decompressed_file_path])
667 apex_info.decompressed_size = os.path.getsize(decompressed_file_path)
668
Daniel Normane9af70a2021-04-15 16:39:22 -0700669 if not compressed_only or apex_info.is_compressed:
Kelvin Zhangc72718c2021-01-27 14:17:14 -0500670 apex_infos.append(apex_info)
Mohammad Samiul Islam9fd58862021-01-06 13:33:25 +0000671
672 return apex_infos