blob: e782bd28f63c7c2c0bf8b3670d80f93d5f15cefb [file] [log] [blame]
Jooyung Han504105f2021-10-26 15:54:50 +09001#!/usr/bin/env python
2#
3# Copyright (C) 2021 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"""sign_virt_apex is a command line tool for sign the Virt APEX file.
17
Jooyung Han98498f42022-02-07 15:23:08 +090018Typical usage:
19 sign_virt_apex [-v] [--avbtool path_to_avbtool] [--signing_args args] payload_key payload_dir
Jooyung Han504105f2021-10-26 15:54:50 +090020
21sign_virt_apex uses external tools which are assumed to be available via PATH.
22- avbtool (--avbtool can override the tool)
23- lpmake, lpunpack, simg2img, img2simg
24"""
25import argparse
Jooyung Han02dceed2021-11-08 17:50:22 +090026import glob
27import hashlib
Jooyung Han504105f2021-10-26 15:54:50 +090028import os
29import re
Jooyung Han98498f42022-02-07 15:23:08 +090030import shlex
Jooyung Han504105f2021-10-26 15:54:50 +090031import shutil
32import subprocess
33import sys
34import tempfile
35
36
37def ParseArgs(argv):
38 parser = argparse.ArgumentParser(description='Sign the Virt APEX')
Jooyung Han02dceed2021-11-08 17:50:22 +090039 parser.add_argument('--verify', action='store_true',
40 help='Verify the Virt APEX')
Jooyung Han504105f2021-10-26 15:54:50 +090041 parser.add_argument(
42 '-v', '--verbose',
43 action='store_true',
44 help='verbose execution')
45 parser.add_argument(
46 '--avbtool',
47 default='avbtool',
48 help='Optional flag that specifies the AVB tool to use. Defaults to `avbtool`.')
49 parser.add_argument(
Jooyung Han98498f42022-02-07 15:23:08 +090050 '--signing_args',
51 help='the extra signing arguments passed to avbtool.'
52 )
53 parser.add_argument(
Jooyung Han1c3d2fa2022-02-24 02:35:59 +090054 '--key_override',
55 metavar="filename=key",
56 action='append',
57 help='Overrides a signing key for a file e.g. microdroid_bootloader=mykey (for testing)')
58 parser.add_argument(
Jooyung Han504105f2021-10-26 15:54:50 +090059 'key',
60 help='path to the private key file.')
61 parser.add_argument(
62 'input_dir',
63 help='the directory having files to be packaged')
Jooyung Han1c3d2fa2022-02-24 02:35:59 +090064 args = parser.parse_args(argv)
65 # preprocess --key_override into a map
66 args.key_overrides = dict()
67 if args.key_override:
68 for pair in args.key_override:
69 name, key = pair.split('=')
70 args.key_overrides[name] = key
71 return args
Jooyung Han504105f2021-10-26 15:54:50 +090072
73
74def RunCommand(args, cmd, env=None, expected_return_values={0}):
75 env = env or {}
76 env.update(os.environ.copy())
77
78 # TODO(b/193504286): we need a way to find other tool (cmd[0]) in various contexts
79 # e.g. sign_apex.py, sign_target_files_apk.py
80 if cmd[0] == 'avbtool':
81 cmd[0] = args.avbtool
82
83 if args.verbose:
84 print('Running: ' + ' '.join(cmd))
85 p = subprocess.Popen(
86 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, universal_newlines=True)
87 output, _ = p.communicate()
88
89 if args.verbose or p.returncode not in expected_return_values:
90 print(output.rstrip())
91
92 assert p.returncode in expected_return_values, (
93 '%d Failed to execute: ' + ' '.join(cmd)) % p.returncode
94 return (output, p.returncode)
95
96
97def ReadBytesSize(value):
98 return int(value.removesuffix(' bytes'))
99
100
Jooyung Han832d11d2021-11-08 12:51:47 +0900101def ExtractAvbPubkey(args, key, output):
102 RunCommand(args, ['avbtool', 'extract_public_key',
103 '--key', key, '--output', output])
104
105
106def AvbInfo(args, image_path):
Jooyung Han504105f2021-10-26 15:54:50 +0900107 """Parses avbtool --info image output
108
109 Args:
110 args: program arguments.
111 image_path: The path to the image.
112 descriptor_name: Descriptor name of interest.
113
114 Returns:
115 A pair of
116 - a dict that contains VBMeta info. None if there's no VBMeta info.
Jooyung Han832d11d2021-11-08 12:51:47 +0900117 - a list of descriptors.
Jooyung Han504105f2021-10-26 15:54:50 +0900118 """
119 if not os.path.exists(image_path):
120 raise ValueError('Failed to find image: {}'.format(image_path))
121
122 output, ret_code = RunCommand(
123 args, ['avbtool', 'info_image', '--image', image_path], expected_return_values={0, 1})
124 if ret_code == 1:
125 return None, None
126
Jooyung Han832d11d2021-11-08 12:51:47 +0900127 info, descriptors = {}, []
Jooyung Han504105f2021-10-26 15:54:50 +0900128
129 # Read `avbtool info_image` output as "key:value" lines
130 matcher = re.compile(r'^(\s*)([^:]+):\s*(.*)$')
131
132 def IterateLine(output):
133 for line in output.split('\n'):
134 line_info = matcher.match(line)
135 if not line_info:
136 continue
137 yield line_info.group(1), line_info.group(2), line_info.group(3)
138
139 gen = IterateLine(output)
Jooyung Han832d11d2021-11-08 12:51:47 +0900140
141 def ReadDescriptors(cur_indent, cur_name, cur_value):
142 descriptor = cur_value if cur_name == 'Prop' else {}
143 descriptors.append((cur_name, descriptor))
144 for indent, key, value in gen:
145 if indent <= cur_indent:
146 # read descriptors recursively to pass the read key as descriptor name
147 ReadDescriptors(indent, key, value)
148 break
149 descriptor[key] = value
150
Jooyung Han504105f2021-10-26 15:54:50 +0900151 # Read VBMeta info
152 for _, key, value in gen:
153 if key == 'Descriptors':
Jooyung Han832d11d2021-11-08 12:51:47 +0900154 ReadDescriptors(*next(gen))
Jooyung Han504105f2021-10-26 15:54:50 +0900155 break
156 info[key] = value
157
Jooyung Han832d11d2021-11-08 12:51:47 +0900158 return info, descriptors
Jooyung Han504105f2021-10-26 15:54:50 +0900159
Jooyung Han832d11d2021-11-08 12:51:47 +0900160
161# Look up a list of (key, value) with a key. Returns the value of the first matching pair.
162def LookUp(pairs, key):
163 for k, v in pairs:
164 if key == k:
165 return v
166 return None
Jooyung Han504105f2021-10-26 15:54:50 +0900167
168
169def AddHashFooter(args, key, image_path):
Jooyung Han1c3d2fa2022-02-24 02:35:59 +0900170 if os.path.basename(image_path) in args.key_overrides:
171 key = args.key_overrides[os.path.basename(image_path)]
Jooyung Han832d11d2021-11-08 12:51:47 +0900172 info, descriptors = AvbInfo(args, image_path)
Jooyung Han504105f2021-10-26 15:54:50 +0900173 if info:
Jooyung Han832d11d2021-11-08 12:51:47 +0900174 descriptor = LookUp(descriptors, 'Hash descriptor')
Jooyung Han504105f2021-10-26 15:54:50 +0900175 image_size = ReadBytesSize(info['Image size'])
176 algorithm = info['Algorithm']
177 partition_name = descriptor['Partition Name']
178 partition_size = str(image_size)
179
180 cmd = ['avbtool', 'add_hash_footer',
181 '--key', key,
182 '--algorithm', algorithm,
183 '--partition_name', partition_name,
184 '--partition_size', partition_size,
185 '--image', image_path]
Jooyung Han98498f42022-02-07 15:23:08 +0900186 if args.signing_args:
187 cmd.extend(shlex.split(args.signing_args))
Jooyung Han504105f2021-10-26 15:54:50 +0900188 RunCommand(args, cmd)
189
190
191def AddHashTreeFooter(args, key, image_path):
Jooyung Han1c3d2fa2022-02-24 02:35:59 +0900192 if os.path.basename(image_path) in args.key_overrides:
193 key = args.key_overrides[os.path.basename(image_path)]
Jooyung Han832d11d2021-11-08 12:51:47 +0900194 info, descriptors = AvbInfo(args, image_path)
Jooyung Han504105f2021-10-26 15:54:50 +0900195 if info:
Jooyung Han832d11d2021-11-08 12:51:47 +0900196 descriptor = LookUp(descriptors, 'Hashtree descriptor')
Jooyung Han504105f2021-10-26 15:54:50 +0900197 image_size = ReadBytesSize(info['Image size'])
198 algorithm = info['Algorithm']
199 partition_name = descriptor['Partition Name']
200 partition_size = str(image_size)
201
202 cmd = ['avbtool', 'add_hashtree_footer',
203 '--key', key,
204 '--algorithm', algorithm,
205 '--partition_name', partition_name,
206 '--partition_size', partition_size,
207 '--do_not_generate_fec',
208 '--image', image_path]
Jooyung Han98498f42022-02-07 15:23:08 +0900209 if args.signing_args:
210 cmd.extend(shlex.split(args.signing_args))
Jooyung Han504105f2021-10-26 15:54:50 +0900211 RunCommand(args, cmd)
212
213
Jooyung Han832d11d2021-11-08 12:51:47 +0900214def MakeVbmetaImage(args, key, vbmeta_img, images=None, chained_partitions=None):
Jooyung Han1c3d2fa2022-02-24 02:35:59 +0900215 if os.path.basename(vbmeta_img) in args.key_overrides:
216 key = args.key_overrides[os.path.basename(vbmeta_img)]
Jooyung Han832d11d2021-11-08 12:51:47 +0900217 info, descriptors = AvbInfo(args, vbmeta_img)
218 if info is None:
219 return
220
221 with TempDirectory() as work_dir:
Jooyung Han504105f2021-10-26 15:54:50 +0900222 algorithm = info['Algorithm']
223 rollback_index = info['Rollback Index']
224 rollback_index_location = info['Rollback Index Location']
225
226 cmd = ['avbtool', 'make_vbmeta_image',
227 '--key', key,
228 '--algorithm', algorithm,
229 '--rollback_index', rollback_index,
230 '--rollback_index_location', rollback_index_location,
231 '--output', vbmeta_img]
Jooyung Han832d11d2021-11-08 12:51:47 +0900232 if images:
233 for img in images:
234 cmd.extend(['--include_descriptors_from_image', img])
235
236 # replace pubkeys of chained_partitions as well
237 for name, descriptor in descriptors:
238 if name == 'Chain Partition descriptor':
239 part_name = descriptor['Partition Name']
240 ril = descriptor['Rollback Index Location']
241 part_key = chained_partitions[part_name]
242 avbpubkey = os.path.join(work_dir, part_name + '.avbpubkey')
243 ExtractAvbPubkey(args, part_key, avbpubkey)
244 cmd.extend(['--chain_partition', '%s:%s:%s' %
245 (part_name, ril, avbpubkey)])
246
Jooyung Han98498f42022-02-07 15:23:08 +0900247 if args.signing_args:
248 cmd.extend(shlex.split(args.signing_args))
249
Jooyung Han504105f2021-10-26 15:54:50 +0900250 RunCommand(args, cmd)
251 # libavb expects to be able to read the maximum vbmeta size, so we must provide a partition
252 # which matches this or the read will fail.
Jooyung Handcb0b492022-02-26 09:04:17 +0900253 with open(vbmeta_img, 'a') as f:
254 f.truncate(65536)
Jooyung Han504105f2021-10-26 15:54:50 +0900255
256
257class TempDirectory(object):
258
259 def __enter__(self):
260 self.name = tempfile.mkdtemp()
261 return self.name
262
263 def __exit__(self, *unused):
264 shutil.rmtree(self.name)
265
266
267def MakeSuperImage(args, partitions, output):
268 with TempDirectory() as work_dir:
269 cmd = ['lpmake', '--device-size=auto', '--metadata-slots=2', # A/B
270 '--metadata-size=65536', '--sparse', '--output=' + output]
271
272 for part, img in partitions.items():
273 tmp_img = os.path.join(work_dir, part)
274 RunCommand(args, ['img2simg', img, tmp_img])
275
276 image_arg = '--image=%s=%s' % (part, img)
277 partition_arg = '--partition=%s:readonly:%d:default' % (
278 part, os.path.getsize(img))
279 cmd.extend([image_arg, partition_arg])
280
281 RunCommand(args, cmd)
282
283
Jooyung Han31b1c2b2021-10-27 03:35:42 +0900284def ReplaceBootloaderPubkey(args, key, bootloader, bootloader_pubkey):
Jooyung Han1c3d2fa2022-02-24 02:35:59 +0900285 if os.path.basename(bootloader) in args.key_overrides:
286 key = args.key_overrides[os.path.basename(bootloader)]
Jooyung Han31b1c2b2021-10-27 03:35:42 +0900287 # read old pubkey before replacement
288 with open(bootloader_pubkey, 'rb') as f:
289 old_pubkey = f.read()
290
Jooyung Han832d11d2021-11-08 12:51:47 +0900291 # replace bootloader pubkey (overwrite the old one with the new one)
292 ExtractAvbPubkey(args, key, bootloader_pubkey)
Jooyung Han31b1c2b2021-10-27 03:35:42 +0900293
294 # read new pubkey
295 with open(bootloader_pubkey, 'rb') as f:
296 new_pubkey = f.read()
297
298 assert len(old_pubkey) == len(new_pubkey)
299
300 # replace pubkey embedded in bootloader
301 with open(bootloader, 'r+b') as bl_f:
302 pos = bl_f.read().find(old_pubkey)
303 assert pos != -1
304 bl_f.seek(pos)
305 bl_f.write(new_pubkey)
306
307
Jooyung Han504105f2021-10-26 15:54:50 +0900308def SignVirtApex(args):
309 key = args.key
310 input_dir = args.input_dir
311
312 # target files in the Virt APEX
Jooyung Han832d11d2021-11-08 12:51:47 +0900313 bootloader_pubkey = os.path.join(
314 input_dir, 'etc', 'microdroid_bootloader.avbpubkey')
Jooyung Han504105f2021-10-26 15:54:50 +0900315 bootloader = os.path.join(input_dir, 'etc', 'microdroid_bootloader')
316 boot_img = os.path.join(input_dir, 'etc', 'fs', 'microdroid_boot-5.10.img')
317 vendor_boot_img = os.path.join(
318 input_dir, 'etc', 'fs', 'microdroid_vendor_boot-5.10.img')
Devin Mooredc9158e2022-01-10 18:51:12 +0000319 init_boot_img = os.path.join(
320 input_dir, 'etc', 'fs', 'microdroid_init_boot.img')
Jooyung Han504105f2021-10-26 15:54:50 +0900321 super_img = os.path.join(input_dir, 'etc', 'fs', 'microdroid_super.img')
322 vbmeta_img = os.path.join(input_dir, 'etc', 'fs', 'microdroid_vbmeta.img')
Jooyung Han832d11d2021-11-08 12:51:47 +0900323 vbmeta_bootconfig_img = os.path.join(
324 input_dir, 'etc', 'fs', 'microdroid_vbmeta_bootconfig.img')
325 bootconfig_normal = os.path.join(
326 input_dir, 'etc', 'microdroid_bootconfig.normal')
327 bootconfig_app_debuggable = os.path.join(
328 input_dir, 'etc', 'microdroid_bootconfig.app_debuggable')
329 bootconfig_full_debuggable = os.path.join(
330 input_dir, 'etc', 'microdroid_bootconfig.full_debuggable')
Jiyong Park34ad9182022-01-28 21:29:48 +0900331 uboot_env_img = os.path.join(
332 input_dir, 'etc', 'uboot_env.img')
Jooyung Han504105f2021-10-26 15:54:50 +0900333
Jooyung Han31b1c2b2021-10-27 03:35:42 +0900334 # Key(pubkey) for bootloader should match with the one used to make VBmeta below
335 # while it's okay to use different keys for other image files.
336 ReplaceBootloaderPubkey(args, key, bootloader, bootloader_pubkey)
337
Devin Mooredc9158e2022-01-10 18:51:12 +0000338 # re-sign bootloader, boot.img, vendor_boot.img, and init_boot.img
Jooyung Han504105f2021-10-26 15:54:50 +0900339 AddHashFooter(args, key, bootloader)
340 AddHashFooter(args, key, boot_img)
341 AddHashFooter(args, key, vendor_boot_img)
Devin Mooredc9158e2022-01-10 18:51:12 +0000342 AddHashFooter(args, key, init_boot_img)
Jooyung Han504105f2021-10-26 15:54:50 +0900343
344 # re-sign super.img
345 with TempDirectory() as work_dir:
346 # unpack super.img
347 tmp_super_img = os.path.join(work_dir, 'super.img')
348 RunCommand(args, ['simg2img', super_img, tmp_super_img])
349 RunCommand(args, ['lpunpack', tmp_super_img, work_dir])
350
351 system_a_img = os.path.join(work_dir, 'system_a.img')
352 vendor_a_img = os.path.join(work_dir, 'vendor_a.img')
353 partitions = {"system_a": system_a_img, "vendor_a": vendor_a_img}
354
355 # re-sign partitions in super.img
356 for img in partitions.values():
357 AddHashTreeFooter(args, key, img)
358
359 # re-pack super.img
360 MakeSuperImage(args, partitions, super_img)
361
Devin Mooredc9158e2022-01-10 18:51:12 +0000362 # re-generate vbmeta from re-signed {boot, vendor_boot, init_boot, system_a, vendor_a}.img
Jooyung Han504105f2021-10-26 15:54:50 +0900363 # Ideally, making VBmeta should be done out of TempDirectory block. But doing it here
364 # to avoid unpacking re-signed super.img for system/vendor images which are available
365 # in this block.
Jooyung Han832d11d2021-11-08 12:51:47 +0900366 MakeVbmetaImage(args, key, vbmeta_img, images=[
Devin Mooredc9158e2022-01-10 18:51:12 +0000367 boot_img, vendor_boot_img, init_boot_img, system_a_img, vendor_a_img])
Jooyung Han504105f2021-10-26 15:54:50 +0900368
Jiyong Park34ad9182022-01-28 21:29:48 +0900369 # Re-sign bootconfigs and the uboot_env with the same key
Jooyung Han832d11d2021-11-08 12:51:47 +0900370 bootconfig_sign_key = key
371 AddHashFooter(args, bootconfig_sign_key, bootconfig_normal)
372 AddHashFooter(args, bootconfig_sign_key, bootconfig_app_debuggable)
373 AddHashFooter(args, bootconfig_sign_key, bootconfig_full_debuggable)
Jiyong Park34ad9182022-01-28 21:29:48 +0900374 AddHashFooter(args, bootconfig_sign_key, uboot_env_img)
Jooyung Han832d11d2021-11-08 12:51:47 +0900375
Jiyong Park34ad9182022-01-28 21:29:48 +0900376 # Re-sign vbmeta_bootconfig with chained_partitions to "bootconfig" and
377 # "uboot_env". Note that, for now, `key` and `bootconfig_sign_key` are the
378 # same, but technically they can be different. Vbmeta records pubkeys which
379 # signed chained partitions.
Jooyung Han832d11d2021-11-08 12:51:47 +0900380 MakeVbmetaImage(args, key, vbmeta_bootconfig_img, chained_partitions={
Jiyong Park34ad9182022-01-28 21:29:48 +0900381 'bootconfig': bootconfig_sign_key,
382 'uboot_env': bootconfig_sign_key,
383 })
Jooyung Han832d11d2021-11-08 12:51:47 +0900384
Jooyung Han504105f2021-10-26 15:54:50 +0900385
Jooyung Han02dceed2021-11-08 17:50:22 +0900386def VerifyVirtApex(args):
387 # Generator to emit avbtool-signed items along with its pubkey digest.
388 # This supports lpmake-packed images as well.
389 def Recur(target_dir):
390 for file in glob.glob(os.path.join(target_dir, 'etc', '**', '*'), recursive=True):
391 cur_item = os.path.relpath(file, target_dir)
392
393 if not os.path.isfile(file):
394 continue
395
396 # avbpubkey
397 if cur_item == 'etc/microdroid_bootloader.avbpubkey':
398 with open(file, 'rb') as f:
399 yield (cur_item, hashlib.sha1(f.read()).hexdigest())
400 continue
401
402 # avbtool signed
403 info, _ = AvbInfo(args, file)
404 if info:
405 yield (cur_item, info['Public key (sha1)'])
406 continue
407
408 # logical partition
409 with TempDirectory() as tmp_dir:
410 unsparsed = os.path.join(tmp_dir, os.path.basename(file))
411 _, rc = RunCommand(
412 # exit with 255 if it's not sparsed
413 args, ['simg2img', file, unsparsed], expected_return_values={0, 255})
414 if rc == 0:
415 with TempDirectory() as unpack_dir:
416 # exit with 64 if it's not a logical partition.
417 _, rc = RunCommand(
418 args, ['lpunpack', unsparsed, unpack_dir], expected_return_values={0, 64})
419 if rc == 0:
420 nested_items = list(Recur(unpack_dir))
421 if len(nested_items) > 0:
422 for (item, key) in nested_items:
423 yield ('%s!/%s' % (cur_item, item), key)
424 continue
425 # Read pubkey digest
426 with TempDirectory() as tmp_dir:
427 pubkey_file = os.path.join(tmp_dir, 'avbpubkey')
428 ExtractAvbPubkey(args, args.key, pubkey_file)
429 with open(pubkey_file, 'rb') as f:
430 pubkey_digest = hashlib.sha1(f.read()).hexdigest()
431
432 # Check every avbtool-signed item against the input key
433 for (item, pubkey) in Recur(args.input_dir):
434 assert pubkey == pubkey_digest, '%s: key mismatch: %s != %s' % (
435 item, pubkey, pubkey_digest)
436
437
Jooyung Han504105f2021-10-26 15:54:50 +0900438def main(argv):
439 try:
440 args = ParseArgs(argv)
Jooyung Han02dceed2021-11-08 17:50:22 +0900441 if args.verify:
442 VerifyVirtApex(args)
443 else:
444 SignVirtApex(args)
Jooyung Han504105f2021-10-26 15:54:50 +0900445 except Exception as e:
446 print(e)
447 sys.exit(1)
448
449
450if __name__ == '__main__':
451 main(sys.argv[1:])