blob: 527cddb94030ffa6e1d47ae81939e575dc65d41f [file] [log] [blame]
Inseob Kim135c1f12022-01-06 17:22:07 +09001#!/usr/bin/env python3
2#
3# Copyright 2022 Google Inc. All rights reserved.
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
17"""
18`fsverity_manifest_generator` generates build manifest APK file containing
19digests of target files. The APK file is signed so the manifest inside the APK
20can be trusted.
21"""
22
23import argparse
24import common
25import os
26import subprocess
27import sys
28from fsverity_digests_pb2 import FSVerityDigests
29
30HASH_ALGORITHM = 'sha256'
31
32def _digest(fsverity_path, input_file):
33 cmd = [fsverity_path, 'digest', input_file]
34 cmd.extend(['--compact'])
35 cmd.extend(['--hash-alg', HASH_ALGORITHM])
36 out = subprocess.check_output(cmd, universal_newlines=True).strip()
37 return bytes(bytearray.fromhex(out))
38
39if __name__ == '__main__':
40 p = argparse.ArgumentParser()
41 p.add_argument(
42 '--output',
43 help='Path to the output manifest APK',
44 required=True)
45 p.add_argument(
46 '--fsverity-path',
47 help='path to the fsverity program',
48 required=True)
49 p.add_argument(
50 '--aapt2-path',
51 help='path to the aapt2 program',
52 required=True)
53 p.add_argument(
Jiyong Parkefbb6ff2022-01-10 17:14:30 +090054 '--min-sdk-version',
55 help='minimum supported sdk version of the generated manifest apk',
56 required=True)
57 p.add_argument(
58 '--framework-res',
59 help='path to framework-res.apk',
60 required=True)
61 p.add_argument(
Inseob Kim135c1f12022-01-06 17:22:07 +090062 '--apksigner-path',
63 help='path to the apksigner program',
64 required=True)
65 p.add_argument(
66 '--apk-key-path',
67 help='path to the apk key',
68 required=True)
69 p.add_argument(
70 '--apk-manifest-path',
71 help='path to AndroidManifest.xml',
72 required=True)
73 p.add_argument(
74 '--base-dir',
75 help='directory to use as a relative root for the inputs',
76 required=True)
77 p.add_argument(
78 'inputs',
79 nargs='+',
80 help='input file for the build manifest')
81 args = p.parse_args(sys.argv[1:])
82
83 digests = FSVerityDigests()
84 for f in sorted(args.inputs):
85 # f is a full path for now; make it relative so it starts with {mount_point}/
86 digest = digests.digests[os.path.relpath(f, args.base_dir)]
87 digest.digest = _digest(args.fsverity_path, f)
88 digest.hash_alg = HASH_ALGORITHM
89
90 temp_dir = common.MakeTempDir()
91
92 os.mkdir(os.path.join(temp_dir, "assets"))
93 metadata_path = os.path.join(temp_dir, "assets", "build_manifest.pb")
94 with open(metadata_path, "wb") as f:
95 f.write(digests.SerializeToString())
96
97 common.RunAndCheckOutput([args.aapt2_path, "link",
98 "-A", os.path.join(temp_dir, "assets"),
99 "-o", args.output,
Jiyong Parkefbb6ff2022-01-10 17:14:30 +0900100 "--min-sdk-version", args.min_sdk_version,
101 "-I", args.framework_res,
Inseob Kim135c1f12022-01-06 17:22:07 +0900102 "--manifest", args.apk_manifest_path])
103 common.RunAndCheckOutput([args.apksigner_path, "sign", "--in", args.output,
104 "--cert", args.apk_key_path + ".x509.pem",
105 "--key", args.apk_key_path + ".pk8"])