blob: 55fe1bd3eeff8a94e5e751bd0ec40cd16b0c722c [file] [log] [blame]
Alan Stokes16fb8552022-02-10 15:07:27 +00001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! A tool to verify a CompOS signature. It starts a CompOS VM as part of this to retrieve the
18//! public key. The tool is intended to be run by odsign during boot.
19
20use anyhow::{bail, Context, Result};
21use compos_aidl_interface::binder::ProcessState;
22use compos_common::compos_client::{VmInstance, VmParameters};
23use compos_common::odrefresh::{
Alan Stokes6542fdd2022-02-17 15:21:46 +000024 CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, TEST_ARTIFACTS_SUBDIR,
Alan Stokes16fb8552022-02-10 15:07:27 +000025};
26use compos_common::{
Alan Stokes6542fdd2022-02-17 15:21:46 +000027 COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE,
28 INSTANCE_IMAGE_FILE, TEST_INSTANCE_DIR,
Alan Stokes16fb8552022-02-10 15:07:27 +000029};
30use log::error;
31use std::fs::File;
32use std::io::Read;
33use std::panic;
34use std::path::Path;
35
36const MAX_FILE_SIZE_BYTES: u64 = 100 * 1024;
37
38fn main() {
39 android_logger::init_once(
40 android_logger::Config::default()
41 .with_tag("compos_verify")
42 .with_min_level(log::Level::Info),
43 );
44
45 // Redirect panic messages to logcat.
46 panic::set_hook(Box::new(|panic_info| {
47 error!("{}", panic_info);
48 }));
49
50 if let Err(e) = try_main() {
51 error!("{:?}", e);
52 std::process::exit(1)
53 }
54}
55
56fn try_main() -> Result<()> {
57 let matches = clap::App::new("compos_verify")
58 .arg(
59 clap::Arg::with_name("instance")
60 .long("instance")
61 .takes_value(true)
62 .required(true)
Alan Stokes6542fdd2022-02-17 15:21:46 +000063 .possible_values(&["current", "test"]),
Alan Stokes16fb8552022-02-10 15:07:27 +000064 )
65 .arg(clap::Arg::with_name("debug").long("debug"))
66 .get_matches();
67
68 let debug_mode = matches.is_present("debug");
69 let (instance_dir, artifacts_dir) = match matches.value_of("instance").unwrap() {
Alan Stokes6542fdd2022-02-17 15:21:46 +000070 "current" => (CURRENT_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR),
Alan Stokes16fb8552022-02-10 15:07:27 +000071 "test" => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR),
72 _ => unreachable!("Unexpected instance name"),
73 };
74
75 let instance_dir = Path::new(COMPOS_DATA_ROOT).join(instance_dir);
76 let artifacts_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(artifacts_dir);
77
78 if !instance_dir.is_dir() {
79 bail!("{:?} is not a directory", instance_dir);
80 }
81
82 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
83 let idsig = instance_dir.join(IDSIG_FILE);
84 let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE);
85
86 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
87
88 let info = artifacts_dir.join("compos.info");
89 let signature = artifacts_dir.join("compos.info.signature");
90
91 let info = read_small_file(&info)?;
92 let signature = read_small_file(&signature)?;
93
94 // We need to start the thread pool to be able to receive Binder callbacks
95 ProcessState::start_thread_pool();
96
97 let virtualization_service = VmInstance::connect_to_virtualization_service()?;
98 let vm_instance = VmInstance::start(
99 &*virtualization_service,
100 instance_image,
101 &idsig,
102 &idsig_manifest_apk,
103 &VmParameters { debug_mode, ..Default::default() },
104 )?;
105 let service = vm_instance.get_service()?;
106
107 let public_key = service.getPublicKey().context("Getting public key")?;
108
109 if !compos_verify_native::verify(&public_key, &signature, &info) {
110 bail!("Signature verification failed");
111 }
112
113 Ok(())
114}
115
116fn read_small_file(file: &Path) -> Result<Vec<u8>> {
117 let mut file = File::open(file)?;
118 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
119 bail!("File is too big");
120 }
121 let mut data = Vec::new();
122 file.read_to_end(&mut data)?;
123 Ok(data)
124}