blob: bcc056ddaf05b850582cf8b7a8040615d5426c63 [file] [log] [blame]
Alice Wangc2fec932023-02-23 16:24:02 +00001// 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
Alice Wangac080d32023-11-02 13:12:25 +000015//! Main executable of Service VM client for manual testing.
Alice Wangc2fec932023-02-23 16:24:02 +000016
Alan Stokes9fd57b02024-05-28 09:50:22 +010017use anyhow::{ensure, Context, Result};
Alice Wangc2fec932023-02-23 16:24:02 +000018use log::{error, info};
Alan Stokes9fd57b02024-05-28 09:50:22 +010019use vm_payload::AttestationError;
20
21vm_payload::main!(main);
Alice Wangc2fec932023-02-23 16:24:02 +000022
23/// Entry point of the Service VM client.
Alan Stokes9fd57b02024-05-28 09:50:22 +010024fn main() {
Alice Wangc2fec932023-02-23 16:24:02 +000025 android_logger::init_once(
26 android_logger::Config::default()
27 .with_tag("service_vm_client")
Jeff Vander Stoepd9dda0c2024-02-07 14:27:06 +010028 .with_max_level(log::LevelFilter::Debug),
Alice Wangc2fec932023-02-23 16:24:02 +000029 );
Alice Wangc2fec932023-02-23 16:24:02 +000030 if let Err(e) = try_main() {
31 error!("failed with {:?}", e);
32 std::process::exit(1);
33 }
34}
35
36fn try_main() -> Result<()> {
37 info!("Welcome to Service VM Client!");
Alice Wang4e3015d2023-10-10 09:35:37 +000038
39 let too_big_challenge = &[0u8; 66];
Alan Stokes9fd57b02024-05-28 09:50:22 +010040 let res = vm_payload::request_attestation(too_big_challenge);
Alice Wang4e3015d2023-10-10 09:35:37 +000041 ensure!(res.is_err());
Alan Stokes9fd57b02024-05-28 09:50:22 +010042 let error = res.unwrap_err();
43 ensure!(error == AttestationError::InvalidChallenge, "Unexpected error: {error:?}");
44 info!("Error: {error}");
Alice Wang4e3015d2023-10-10 09:35:37 +000045
Alice Wanga410b642023-10-18 09:05:15 +000046 // The data below is only a placeholder generated randomly with urandom
47 let challenge = &[
48 0x6c, 0xad, 0x52, 0x50, 0x15, 0xe7, 0xf4, 0x1d, 0xa5, 0x60, 0x7e, 0xd2, 0x7d, 0xf1, 0x51,
49 0x67, 0xc3, 0x3e, 0x73, 0x9b, 0x30, 0xbd, 0x04, 0x20, 0x2e, 0xde, 0x3b, 0x1d, 0xc8, 0x07,
50 0x11, 0x7b,
51 ];
Alan Stokes9fd57b02024-05-28 09:50:22 +010052 let res = vm_payload::request_attestation(challenge).context("Unexpected attestation error")?;
Alice Wang4e3015d2023-10-10 09:35:37 +000053
Alan Stokes9fd57b02024-05-28 09:50:22 +010054 let cert_chain: Vec<_> = res.certificate_chain().collect();
Alice Wang4e3015d2023-10-10 09:35:37 +000055 info!("Attestation result certificateChain = {:?}", cert_chain);
56
Alan Stokes9fd57b02024-05-28 09:50:22 +010057 let private_key = res.private_key();
Alice Wang4e3015d2023-10-10 09:35:37 +000058 info!("Attestation result privateKey = {:?}", private_key);
59
60 let message = b"Hello from Service VM client";
61 info!("Signing message: {:?}", message);
Alan Stokes9fd57b02024-05-28 09:50:22 +010062 let signature = res.sign_message(message);
Alice Wang4e3015d2023-10-10 09:35:37 +000063 info!("Signature: {:?}", signature);
64
Alice Wangc2fec932023-02-23 16:24:02 +000065 Ok(())
66}