blob: 224cde77b114761210b26310396acf0169a605c1 [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
Alan Stokes98a964c2022-02-23 11:37:46 +000020use android_logger::LogId;
Alan Stokes16fb8552022-02-10 15:07:27 +000021use anyhow::{bail, Context, Result};
22use compos_aidl_interface::binder::ProcessState;
Andrew Walbrand0ef4002022-05-16 16:14:10 +000023use compos_common::compos_client::{ComposClient, VmParameters};
Alan Stokes16fb8552022-02-10 15:07:27 +000024use compos_common::odrefresh::{
Alan Stokes32d8fa52022-03-14 14:59:42 +000025 CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, PENDING_ARTIFACTS_SUBDIR,
26 TEST_ARTIFACTS_SUBDIR,
Alan Stokes16fb8552022-02-10 15:07:27 +000027};
28use compos_common::{
Alan Stokes6542fdd2022-02-17 15:21:46 +000029 COMPOS_DATA_ROOT, CURRENT_INSTANCE_DIR, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE,
30 INSTANCE_IMAGE_FILE, TEST_INSTANCE_DIR,
Alan Stokes16fb8552022-02-10 15:07:27 +000031};
32use log::error;
33use std::fs::File;
34use std::io::Read;
35use std::panic;
36use std::path::Path;
37
38const MAX_FILE_SIZE_BYTES: u64 = 100 * 1024;
39
40fn main() {
41 android_logger::init_once(
42 android_logger::Config::default()
43 .with_tag("compos_verify")
Alan Stokes98a964c2022-02-23 11:37:46 +000044 .with_min_level(log::Level::Info)
45 .with_log_id(LogId::System), // Needed to log successfully early in boot
Alan Stokes16fb8552022-02-10 15:07:27 +000046 );
47
48 // Redirect panic messages to logcat.
49 panic::set_hook(Box::new(|panic_info| {
50 error!("{}", panic_info);
51 }));
52
53 if let Err(e) = try_main() {
54 error!("{:?}", e);
55 std::process::exit(1)
56 }
57}
58
59fn try_main() -> Result<()> {
60 let matches = clap::App::new("compos_verify")
61 .arg(
62 clap::Arg::with_name("instance")
63 .long("instance")
64 .takes_value(true)
65 .required(true)
Alan Stokes32d8fa52022-03-14 14:59:42 +000066 .possible_values(&["current", "pending", "test"]),
Alan Stokes16fb8552022-02-10 15:07:27 +000067 )
68 .arg(clap::Arg::with_name("debug").long("debug"))
69 .get_matches();
70
71 let debug_mode = matches.is_present("debug");
72 let (instance_dir, artifacts_dir) = match matches.value_of("instance").unwrap() {
Alan Stokes6542fdd2022-02-17 15:21:46 +000073 "current" => (CURRENT_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR),
Alan Stokes32d8fa52022-03-14 14:59:42 +000074 "pending" => (CURRENT_INSTANCE_DIR, PENDING_ARTIFACTS_SUBDIR),
Alan Stokes16fb8552022-02-10 15:07:27 +000075 "test" => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR),
76 _ => unreachable!("Unexpected instance name"),
77 };
78
79 let instance_dir = Path::new(COMPOS_DATA_ROOT).join(instance_dir);
80 let artifacts_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(artifacts_dir);
81
82 if !instance_dir.is_dir() {
83 bail!("{:?} is not a directory", instance_dir);
84 }
85
86 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
87 let idsig = instance_dir.join(IDSIG_FILE);
88 let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE);
89
90 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
91
92 let info = artifacts_dir.join("compos.info");
93 let signature = artifacts_dir.join("compos.info.signature");
94
Alan Stokesdd8dfe82022-03-10 15:30:49 +000095 let info = read_small_file(&info).context("Failed to read compos.info")?;
96 let signature = read_small_file(&signature).context("Failed to read compos.info signature")?;
Alan Stokes16fb8552022-02-10 15:07:27 +000097
98 // We need to start the thread pool to be able to receive Binder callbacks
99 ProcessState::start_thread_pool();
100
Andrew Walbrand0ef4002022-05-16 16:14:10 +0000101 let virtualization_service = vmclient::connect()?;
102 let vm_instance = ComposClient::start(
Alan Stokes16fb8552022-02-10 15:07:27 +0000103 &*virtualization_service,
104 instance_image,
105 &idsig,
106 &idsig_manifest_apk,
Alan Stokes0df48502022-06-15 15:24:37 +0100107 &VmParameters { debug_mode, ..Default::default() },
Alan Stokes16fb8552022-02-10 15:07:27 +0000108 )?;
Alan Stokes16fb8552022-02-10 15:07:27 +0000109
Alan Stokes71403772022-06-21 14:56:28 +0100110 let service = vm_instance.connect_service()?;
111 let public_key = service.getPublicKey().context("Getting public key");
Alan Stokes16fb8552022-02-10 15:07:27 +0000112
Alan Stokes0fc6ce52022-08-02 17:01:48 +0100113 vm_instance.shutdown(service);
Alan Stokes71403772022-06-21 14:56:28 +0100114
115 if !compos_verify_native::verify(&public_key?, &signature, &info) {
Alan Stokes16fb8552022-02-10 15:07:27 +0000116 bail!("Signature verification failed");
117 }
118
119 Ok(())
120}
121
122fn read_small_file(file: &Path) -> Result<Vec<u8>> {
123 let mut file = File::open(file)?;
124 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
125 bail!("File is too big");
126 }
127 let mut data = Vec::new();
128 file.read_to_end(&mut data)?;
129 Ok(data)
130}