blob: 228225d9aa3d5630a35d65287529f2adc39d7a7a [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::{
24 CURRENT_ARTIFACTS_SUBDIR, ODREFRESH_OUTPUT_ROOT_DIR, PENDING_ARTIFACTS_SUBDIR,
25 TEST_ARTIFACTS_SUBDIR,
26};
27use compos_common::{
28 COMPOS_DATA_ROOT, IDSIG_FILE, IDSIG_MANIFEST_APK_FILE, INSTANCE_IMAGE_FILE,
29 PENDING_INSTANCE_DIR, TEST_INSTANCE_DIR,
30};
31use log::error;
32use std::fs::File;
33use std::io::Read;
34use std::panic;
35use std::path::Path;
36
37const MAX_FILE_SIZE_BYTES: u64 = 100 * 1024;
38
39fn main() {
40 android_logger::init_once(
41 android_logger::Config::default()
42 .with_tag("compos_verify")
43 .with_min_level(log::Level::Info),
44 );
45
46 // Redirect panic messages to logcat.
47 panic::set_hook(Box::new(|panic_info| {
48 error!("{}", panic_info);
49 }));
50
51 if let Err(e) = try_main() {
52 error!("{:?}", e);
53 std::process::exit(1)
54 }
55}
56
57fn try_main() -> Result<()> {
58 let matches = clap::App::new("compos_verify")
59 .arg(
60 clap::Arg::with_name("instance")
61 .long("instance")
62 .takes_value(true)
63 .required(true)
64 .possible_values(&["pending", "current", "test"]),
65 )
66 .arg(clap::Arg::with_name("debug").long("debug"))
67 .get_matches();
68
69 let debug_mode = matches.is_present("debug");
70 let (instance_dir, artifacts_dir) = match matches.value_of("instance").unwrap() {
71 "pending" => (PENDING_INSTANCE_DIR, PENDING_ARTIFACTS_SUBDIR),
72 "current" => (PENDING_INSTANCE_DIR, CURRENT_ARTIFACTS_SUBDIR),
73 "test" => (TEST_INSTANCE_DIR, TEST_ARTIFACTS_SUBDIR),
74 _ => unreachable!("Unexpected instance name"),
75 };
76
77 let instance_dir = Path::new(COMPOS_DATA_ROOT).join(instance_dir);
78 let artifacts_dir = Path::new(ODREFRESH_OUTPUT_ROOT_DIR).join(artifacts_dir);
79
80 if !instance_dir.is_dir() {
81 bail!("{:?} is not a directory", instance_dir);
82 }
83
84 let instance_image = instance_dir.join(INSTANCE_IMAGE_FILE);
85 let idsig = instance_dir.join(IDSIG_FILE);
86 let idsig_manifest_apk = instance_dir.join(IDSIG_MANIFEST_APK_FILE);
87
88 let instance_image = File::open(instance_image).context("Failed to open instance image")?;
89
90 let info = artifacts_dir.join("compos.info");
91 let signature = artifacts_dir.join("compos.info.signature");
92
93 let info = read_small_file(&info)?;
94 let signature = read_small_file(&signature)?;
95
96 // We need to start the thread pool to be able to receive Binder callbacks
97 ProcessState::start_thread_pool();
98
99 let virtualization_service = VmInstance::connect_to_virtualization_service()?;
100 let vm_instance = VmInstance::start(
101 &*virtualization_service,
102 instance_image,
103 &idsig,
104 &idsig_manifest_apk,
105 &VmParameters { debug_mode, ..Default::default() },
106 )?;
107 let service = vm_instance.get_service()?;
108
109 let public_key = service.getPublicKey().context("Getting public key")?;
110
111 if !compos_verify_native::verify(&public_key, &signature, &info) {
112 bail!("Signature verification failed");
113 }
114
115 Ok(())
116}
117
118fn read_small_file(file: &Path) -> Result<Vec<u8>> {
119 let mut file = File::open(file)?;
120 if file.metadata()?.len() > MAX_FILE_SIZE_BYTES {
121 bail!("File is too big");
122 }
123 let mut data = Vec::new();
124 file.read_to_end(&mut data)?;
125 Ok(data)
126}