1 // Copyright 2023, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Main executable of Service VM client for manual testing.
16
17 use anyhow::{ensure, Context, Result};
18 use log::{error, info};
19 use std::panic;
20 use vm_payload::AttestationError;
21
22 vm_payload::main!(main);
23
24 /// Entry point of the Service VM client.
main()25 fn main() {
26 android_logger::init_once(
27 android_logger::Config::default()
28 .with_tag("service_vm_client")
29 .with_max_level(log::LevelFilter::Debug),
30 );
31 // Redirect panic messages to logcat.
32 panic::set_hook(Box::new(|panic_info| {
33 error!("{}", panic_info);
34 }));
35 if let Err(e) = try_main() {
36 error!("failed with {:?}", e);
37 std::process::exit(1);
38 }
39 }
40
try_main() -> Result<()>41 fn try_main() -> Result<()> {
42 info!("Welcome to Service VM Client!");
43
44 let too_big_challenge = &[0u8; 66];
45 let res = vm_payload::request_attestation(too_big_challenge);
46 ensure!(res.is_err());
47 let error = res.unwrap_err();
48 ensure!(error == AttestationError::InvalidChallenge, "Unexpected error: {error:?}");
49 info!("Error: {error}");
50
51 // The data below is only a placeholder generated randomly with urandom
52 let challenge = &[
53 0x6c, 0xad, 0x52, 0x50, 0x15, 0xe7, 0xf4, 0x1d, 0xa5, 0x60, 0x7e, 0xd2, 0x7d, 0xf1, 0x51,
54 0x67, 0xc3, 0x3e, 0x73, 0x9b, 0x30, 0xbd, 0x04, 0x20, 0x2e, 0xde, 0x3b, 0x1d, 0xc8, 0x07,
55 0x11, 0x7b,
56 ];
57 let res = vm_payload::request_attestation(challenge).context("Unexpected attestation error")?;
58
59 let cert_chain: Vec<_> = res.certificate_chain().collect();
60 info!("Attestation result certificateChain = {:?}", cert_chain);
61
62 let private_key = res.private_key();
63 info!("Attestation result privateKey = {:?}", private_key);
64
65 let message = b"Hello from Service VM client";
66 info!("Signing message: {:?}", message);
67 let signature = res.sign_message(message);
68 info!("Signature: {:?}", signature);
69
70 Ok(())
71 }
72