1#!/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( 54 '--min-sdk-version', 55 help='minimum supported sdk version of the generated manifest apk', 56 required=True) 57 p.add_argument( 58 '--version-code', 59 help='version code for the generated manifest apk', 60 required=True) 61 p.add_argument( 62 '--version-name', 63 help='version name for the generated manifest apk', 64 required=True) 65 p.add_argument( 66 '--framework-res', 67 help='path to framework-res.apk', 68 required=True) 69 p.add_argument( 70 '--apksigner-path', 71 help='path to the apksigner program', 72 required=True) 73 p.add_argument( 74 '--apk-key-path', 75 help='path to the apk key', 76 required=True) 77 p.add_argument( 78 '--apk-manifest-path', 79 help='path to AndroidManifest.xml', 80 required=True) 81 p.add_argument( 82 '--base-dir', 83 help='directory to use as a relative root for the inputs', 84 required=True) 85 p.add_argument( 86 'inputs', 87 nargs='+', 88 help='input file for the build manifest') 89 args = p.parse_args(sys.argv[1:]) 90 91 digests = FSVerityDigests() 92 for f in sorted(args.inputs): 93 # f is a full path for now; make it relative so it starts with {mount_point}/ 94 digest = digests.digests[os.path.relpath(f, args.base_dir)] 95 digest.digest = _digest(args.fsverity_path, f) 96 digest.hash_alg = HASH_ALGORITHM 97 98 temp_dir = common.MakeTempDir() 99 100 os.mkdir(os.path.join(temp_dir, "assets")) 101 metadata_path = os.path.join(temp_dir, "assets", "build_manifest.pb") 102 with open(metadata_path, "wb") as f: 103 f.write(digests.SerializeToString()) 104 105 common.RunAndCheckOutput([args.aapt2_path, "link", 106 "-A", os.path.join(temp_dir, "assets"), 107 "-o", args.output, 108 "--min-sdk-version", args.min_sdk_version, 109 "--version-code", args.version_code, 110 "--version-name", args.version_name, 111 "-I", args.framework_res, 112 "--manifest", args.apk_manifest_path]) 113 common.RunAndCheckOutput([args.apksigner_path, "sign", "--in", args.output, 114 "--cert", args.apk_key_path + ".x509.pem", 115 "--key", args.apk_key_path + ".pk8"]) 116