Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 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 | |
Alice Wang | ac080d3 | 2023-11-02 13:12:25 +0000 | [diff] [blame] | 15 | //! Main executable of Service VM client for manual testing. |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 16 | |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 17 | use anyhow::{ensure, Context, Result}; |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 18 | use log::{error, info}; |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 19 | use std::panic; |
| 20 | use vm_payload::AttestationError; |
| 21 | |
| 22 | vm_payload::main!(main); |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 23 | |
| 24 | /// Entry point of the Service VM client. |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 25 | fn main() { |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 26 | android_logger::init_once( |
| 27 | android_logger::Config::default() |
| 28 | .with_tag("service_vm_client") |
Jeff Vander Stoep | d9dda0c | 2024-02-07 14:27:06 +0100 | [diff] [blame] | 29 | .with_max_level(log::LevelFilter::Debug), |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 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 | |
| 41 | fn try_main() -> Result<()> { |
| 42 | info!("Welcome to Service VM Client!"); |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 43 | |
| 44 | let too_big_challenge = &[0u8; 66]; |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 45 | let res = vm_payload::request_attestation(too_big_challenge); |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 46 | ensure!(res.is_err()); |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 47 | let error = res.unwrap_err(); |
| 48 | ensure!(error == AttestationError::InvalidChallenge, "Unexpected error: {error:?}"); |
| 49 | info!("Error: {error}"); |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 50 | |
Alice Wang | a410b64 | 2023-10-18 09:05:15 +0000 | [diff] [blame] | 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 | ]; |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 57 | let res = vm_payload::request_attestation(challenge).context("Unexpected attestation error")?; |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 58 | |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 59 | let cert_chain: Vec<_> = res.certificate_chain().collect(); |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 60 | info!("Attestation result certificateChain = {:?}", cert_chain); |
| 61 | |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 62 | let private_key = res.private_key(); |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 63 | info!("Attestation result privateKey = {:?}", private_key); |
| 64 | |
| 65 | let message = b"Hello from Service VM client"; |
| 66 | info!("Signing message: {:?}", message); |
Alan Stokes | 9fd57b0 | 2024-05-28 09:50:22 +0100 | [diff] [blame] | 67 | let signature = res.sign_message(message); |
Alice Wang | 4e3015d | 2023-10-10 09:35:37 +0000 | [diff] [blame] | 68 | info!("Signature: {:?}", signature); |
| 69 | |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame] | 70 | Ok(()) |
| 71 | } |