David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 1 | // Copyright 2022, 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 | //! Integration test for Rialto. |
| 16 | |
| 17 | use android_system_virtualizationservice::{ |
| 18 | aidl::android::system::virtualizationservice::{ |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 19 | VirtualMachineConfig::VirtualMachineConfig, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 20 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 21 | }, |
| 22 | binder::{ParcelFileDescriptor, ProcessState}, |
| 23 | }; |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 24 | use anyhow::{bail, Context, Result}; |
Alice Wang | b76b66f | 2024-03-26 08:16:23 +0000 | [diff] [blame] | 25 | use bssl_avf::{rand_bytes, sha256, EcKey, PKey}; |
Alice Wang | de6bee5 | 2023-11-10 09:58:40 +0000 | [diff] [blame] | 26 | use client_vm_csr::generate_attestation_key_and_csr; |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 27 | use coset::{CborSerializable, CoseMac0, CoseSign}; |
Alice Wang | 68d1140 | 2024-01-02 13:59:44 +0000 | [diff] [blame] | 28 | use hwtrust::{rkp, session::Session}; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 29 | use log::info; |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 30 | use service_vm_comm::{ |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 31 | ClientVmAttestationParams, Csr, CsrPayload, EcdsaP256KeyPair, GenerateCertificateRequestParams, |
Alice Wang | d3a9640 | 2023-11-24 15:37:39 +0000 | [diff] [blame] | 32 | Request, RequestProcessingError, Response, VmType, |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 33 | }; |
Alice Wang | 1cc1350 | 2023-12-05 11:05:34 +0000 | [diff] [blame] | 34 | use service_vm_fake_chain::client_vm::{ |
| 35 | fake_client_vm_dice_artifacts, fake_sub_components, SubComponent, |
| 36 | }; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 37 | use service_vm_manager::ServiceVm; |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 38 | use std::fs; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 39 | use std::fs::File; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 40 | use std::panic; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 41 | use std::path::PathBuf; |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 42 | use std::str::FromStr; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 43 | use vmclient::VmInstance; |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 44 | use x509_cert::{ |
| 45 | certificate::{Certificate, Version}, |
| 46 | der::{self, asn1, Decode, Encode}, |
| 47 | name::Name, |
| 48 | spki::{AlgorithmIdentifier, ObjectIdentifier, SubjectPublicKeyInfo}, |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 49 | }; |
Alice Wang | 4e082c3 | 2023-07-11 07:41:50 +0000 | [diff] [blame] | 50 | |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 51 | const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin"; |
| 52 | const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img"; |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 53 | const TEST_CERT_CHAIN_PATH: &str = "testdata/rkp_cert_chain.der"; |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 54 | |
Alice Wang | 0472f46 | 2024-02-06 08:53:19 +0000 | [diff] [blame] | 55 | #[cfg(dice_changes)] |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 56 | #[test] |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 57 | fn process_requests_in_protected_vm() -> Result<()> { |
Alice Wang | 0472f46 | 2024-02-06 08:53:19 +0000 | [diff] [blame] | 58 | // The test is skipped if the feature flag |dice_changes| is not enabled, because when |
| 59 | // the flag is off, the DICE chain is truncated in the pvmfw, and the service VM cannot |
| 60 | // verify the chain due to the missing entries in the chain. |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 61 | check_processing_requests(VmType::ProtectedVm) |
Alice Wang | 9a8b39f | 2023-04-12 15:31:48 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 64 | #[test] |
| 65 | fn process_requests_in_non_protected_vm() -> Result<()> { |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 66 | check_processing_requests(VmType::NonProtectedVm) |
| 67 | } |
| 68 | |
| 69 | fn check_processing_requests(vm_type: VmType) -> Result<()> { |
| 70 | let mut vm = start_service_vm(vm_type)?; |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 71 | |
| 72 | check_processing_reverse_request(&mut vm)?; |
Alice Wang | 74eb78b | 2023-11-09 16:13:10 +0000 | [diff] [blame] | 73 | let key_pair = check_processing_generating_key_pair_request(&mut vm)?; |
Karuna Wadhera | 4b4a519 | 2024-06-11 19:45:30 +0000 | [diff] [blame^] | 74 | check_processing_generating_certificate_request(&mut vm, &key_pair.maced_public_key, vm_type)?; |
Alice Wang | d3a9640 | 2023-11-24 15:37:39 +0000 | [diff] [blame] | 75 | check_attestation_request(&mut vm, &key_pair, vm_type)?; |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 76 | Ok(()) |
| 77 | } |
| 78 | |
| 79 | fn check_processing_reverse_request(vm: &mut ServiceVm) -> Result<()> { |
Alice Wang | 0486e25 | 2023-10-06 14:30:49 +0000 | [diff] [blame] | 80 | let message = "abc".repeat(500); |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 81 | let request = Request::Reverse(message.as_bytes().to_vec()); |
| 82 | |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 83 | let response = vm.process_request(request)?; |
| 84 | info!("Received response: {response:?}."); |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 85 | |
| 86 | let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect(); |
| 87 | assert_eq!(Response::Reverse(expected_response), response); |
| 88 | Ok(()) |
| 89 | } |
| 90 | |
Alice Wang | 74eb78b | 2023-11-09 16:13:10 +0000 | [diff] [blame] | 91 | fn check_processing_generating_key_pair_request(vm: &mut ServiceVm) -> Result<EcdsaP256KeyPair> { |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 92 | let request = Request::GenerateEcdsaP256KeyPair; |
| 93 | |
| 94 | let response = vm.process_request(request)?; |
| 95 | info!("Received response: {response:?}."); |
| 96 | |
| 97 | match response { |
Alice Wang | 74eb78b | 2023-11-09 16:13:10 +0000 | [diff] [blame] | 98 | Response::GenerateEcdsaP256KeyPair(key_pair) => { |
| 99 | assert_array_has_nonzero(&key_pair.maced_public_key); |
| 100 | assert_array_has_nonzero(&key_pair.key_blob); |
| 101 | Ok(key_pair) |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 102 | } |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 103 | _ => bail!("Incorrect response type: {response:?}"), |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Alice Wang | a78d3f0 | 2023-09-13 12:39:16 +0000 | [diff] [blame] | 107 | fn assert_array_has_nonzero(v: &[u8]) { |
| 108 | assert!(v.iter().any(|&x| x != 0)) |
| 109 | } |
| 110 | |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 111 | fn check_processing_generating_certificate_request( |
| 112 | vm: &mut ServiceVm, |
Alice Wang | 74eb78b | 2023-11-09 16:13:10 +0000 | [diff] [blame] | 113 | maced_public_key: &[u8], |
Karuna Wadhera | 4b4a519 | 2024-06-11 19:45:30 +0000 | [diff] [blame^] | 114 | vm_type: VmType, |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 115 | ) -> Result<()> { |
| 116 | let params = GenerateCertificateRequestParams { |
Alice Wang | 74eb78b | 2023-11-09 16:13:10 +0000 | [diff] [blame] | 117 | keys_to_sign: vec![maced_public_key.to_vec()], |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 118 | challenge: vec![], |
| 119 | }; |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 120 | let request = Request::GenerateCertificateRequest(params); |
| 121 | |
| 122 | let response = vm.process_request(request)?; |
| 123 | info!("Received response: {response:?}."); |
| 124 | |
| 125 | match response { |
Karuna Wadhera | 4b4a519 | 2024-06-11 19:45:30 +0000 | [diff] [blame^] | 126 | Response::GenerateCertificateRequest(csr) => check_csr(csr, vm_type), |
Alice Wang | ff5592d | 2023-09-13 15:27:39 +0000 | [diff] [blame] | 127 | _ => bail!("Incorrect response type: {response:?}"), |
Alice Wang | 9646fb3 | 2023-09-08 10:01:31 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 131 | fn check_attestation_request( |
| 132 | vm: &mut ServiceVm, |
| 133 | remotely_provisioned_key_pair: &EcdsaP256KeyPair, |
Alice Wang | d3a9640 | 2023-11-24 15:37:39 +0000 | [diff] [blame] | 134 | vm_type: VmType, |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 135 | ) -> Result<()> { |
Alice Wang | de6bee5 | 2023-11-10 09:58:40 +0000 | [diff] [blame] | 136 | /// The following data was generated randomly with urandom. |
| 137 | const CHALLENGE: [u8; 16] = [ |
| 138 | 0x7d, 0x86, 0x58, 0x79, 0x3a, 0x09, 0xdf, 0x1c, 0xa5, 0x80, 0x80, 0x15, 0x2b, 0x13, 0x17, |
| 139 | 0x5c, |
| 140 | ]; |
Alice Wang | 4ac9c8b | 2023-12-05 16:23:14 +0000 | [diff] [blame] | 141 | let dice_artifacts = fake_client_vm_dice_artifacts()?; |
Alice Wang | de6bee5 | 2023-11-10 09:58:40 +0000 | [diff] [blame] | 142 | let attestation_data = generate_attestation_key_and_csr(&CHALLENGE, &dice_artifacts)?; |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 143 | let cert_chain = fs::read(TEST_CERT_CHAIN_PATH)?; |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 144 | // The certificate chain contains several certificates, but we only need the first one. |
| 145 | // Parsing the data with trailing data always fails with a `TrailingData` error. |
| 146 | let cert_len: usize = match Certificate::from_der(&cert_chain).unwrap_err().kind() { |
| 147 | der::ErrorKind::TrailingData { decoded, .. } => decoded.try_into().unwrap(), |
| 148 | e => bail!("Unexpected error: {e}"), |
| 149 | }; |
Alice Wang | de6bee5 | 2023-11-10 09:58:40 +0000 | [diff] [blame] | 150 | |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 151 | // Builds the mock parameters for the client VM attestation. |
| 152 | // The `csr` and `remotely_provisioned_key_blob` parameters are extracted from the same |
| 153 | // libraries as in production. |
| 154 | // The `remotely_provisioned_cert` parameter is an RKP certificate extracted from a test |
| 155 | // certificate chain retrieved from RKPD. |
Alice Wang | de6bee5 | 2023-11-10 09:58:40 +0000 | [diff] [blame] | 156 | let params = ClientVmAttestationParams { |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 157 | csr: attestation_data.csr.clone().into_cbor_vec()?, |
| 158 | remotely_provisioned_key_blob: remotely_provisioned_key_pair.key_blob.to_vec(), |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 159 | remotely_provisioned_cert: cert_chain[..cert_len].to_vec(), |
Alice Wang | de6bee5 | 2023-11-10 09:58:40 +0000 | [diff] [blame] | 160 | }; |
Alice Wang | 74eb78b | 2023-11-09 16:13:10 +0000 | [diff] [blame] | 161 | let request = Request::RequestClientVmAttestation(params); |
| 162 | |
| 163 | let response = vm.process_request(request)?; |
| 164 | info!("Received response: {response:?}."); |
| 165 | |
| 166 | match response { |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 167 | Response::RequestClientVmAttestation(certificate) => { |
Alice Wang | d3a9640 | 2023-11-24 15:37:39 +0000 | [diff] [blame] | 168 | // The end-to-end test for non-protected VM attestation works because both the service |
| 169 | // VM and the client VM use the same fake DICE chain. |
| 170 | assert_eq!(vm_type, VmType::NonProtectedVm); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 171 | check_certificate_for_client_vm( |
| 172 | &certificate, |
| 173 | &remotely_provisioned_key_pair.maced_public_key, |
| 174 | &attestation_data.csr, |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 175 | &Certificate::from_der(&cert_chain[..cert_len]).unwrap(), |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 176 | )?; |
| 177 | Ok(()) |
| 178 | } |
Alice Wang | d3a9640 | 2023-11-24 15:37:39 +0000 | [diff] [blame] | 179 | Response::Err(RequestProcessingError::InvalidDiceChain) => { |
| 180 | // The end-to-end test for protected VM attestation doesn't work because the service VM |
| 181 | // compares the fake DICE chain in the CSR with the real DICE chain. |
| 182 | // We cannot generate a valid DICE chain with the same payloads up to pvmfw. |
| 183 | assert_eq!(vm_type, VmType::ProtectedVm); |
| 184 | Ok(()) |
| 185 | } |
Alice Wang | 74eb78b | 2023-11-09 16:13:10 +0000 | [diff] [blame] | 186 | _ => bail!("Incorrect response type: {response:?}"), |
| 187 | } |
| 188 | } |
| 189 | |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 190 | fn check_vm_components(vm_components: &asn1::SequenceOf<asn1::Any, 4>) -> Result<()> { |
Alice Wang | 1cc1350 | 2023-12-05 11:05:34 +0000 | [diff] [blame] | 191 | let expected_components = fake_sub_components(); |
| 192 | assert_eq!(expected_components.len(), vm_components.len()); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 193 | for (i, expected_component) in expected_components.iter().enumerate() { |
| 194 | check_vm_component(vm_components.get(i).unwrap(), expected_component)?; |
Alice Wang | 1cc1350 | 2023-12-05 11:05:34 +0000 | [diff] [blame] | 195 | } |
| 196 | Ok(()) |
| 197 | } |
| 198 | |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 199 | fn check_vm_component(vm_component: &asn1::Any, expected_component: &SubComponent) -> Result<()> { |
| 200 | let vm_component = vm_component.decode_as::<asn1::SequenceOf<asn1::Any, 4>>().unwrap(); |
Alice Wang | 1cc1350 | 2023-12-05 11:05:34 +0000 | [diff] [blame] | 201 | assert_eq!(4, vm_component.len()); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 202 | let name = vm_component.get(0).unwrap().decode_as::<asn1::Utf8StringRef>().unwrap(); |
| 203 | assert_eq!(expected_component.name, name.as_ref()); |
| 204 | let version = vm_component.get(1).unwrap().decode_as::<u64>().unwrap(); |
| 205 | assert_eq!(expected_component.version, version); |
| 206 | let code_hash = vm_component.get(2).unwrap().decode_as::<asn1::OctetString>().unwrap(); |
| 207 | assert_eq!(expected_component.code_hash, code_hash.as_bytes()); |
| 208 | let authority_hash = vm_component.get(3).unwrap().decode_as::<asn1::OctetString>().unwrap(); |
| 209 | assert_eq!(expected_component.authority_hash, authority_hash.as_bytes()); |
Alice Wang | 1cc1350 | 2023-12-05 11:05:34 +0000 | [diff] [blame] | 210 | Ok(()) |
| 211 | } |
| 212 | |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 213 | fn check_certificate_for_client_vm( |
| 214 | certificate: &[u8], |
| 215 | maced_public_key: &[u8], |
| 216 | csr: &Csr, |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 217 | parent_certificate: &Certificate, |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 218 | ) -> Result<()> { |
| 219 | let cose_mac = CoseMac0::from_slice(maced_public_key)?; |
Alice Wang | be7a4b1 | 2023-12-01 11:53:36 +0000 | [diff] [blame] | 220 | let authority_public_key = |
| 221 | EcKey::from_cose_public_key_slice(&cose_mac.payload.unwrap()).unwrap(); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 222 | let cert = Certificate::from_der(certificate).unwrap(); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 223 | |
| 224 | // Checks the certificate signature against the authority public key. |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 225 | const ECDSA_WITH_SHA_256: ObjectIdentifier = |
| 226 | ObjectIdentifier::new_unwrap("1.2.840.10045.4.3.2"); |
| 227 | let expected_algorithm = AlgorithmIdentifier { oid: ECDSA_WITH_SHA_256, parameters: None }; |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 228 | assert_eq!(expected_algorithm, cert.signature_algorithm); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 229 | let tbs_cert = cert.tbs_certificate; |
| 230 | let digest = sha256(&tbs_cert.to_der().unwrap()).unwrap(); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 231 | authority_public_key |
Alan Stokes | b2f52fb | 2024-05-09 10:12:55 +0100 | [diff] [blame] | 232 | .ecdsa_verify_der(cert.signature.raw_bytes(), &digest) |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 233 | .expect("Failed to verify the certificate signature with the authority public key"); |
| 234 | |
| 235 | // Checks that the certificate's subject public key is equal to the key in the CSR. |
| 236 | let cose_sign = CoseSign::from_slice(&csr.signed_csr_payload)?; |
| 237 | let csr_payload = |
| 238 | cose_sign.payload.as_ref().and_then(|v| CsrPayload::from_cbor_slice(v).ok()).unwrap(); |
Alice Wang | be7a4b1 | 2023-12-01 11:53:36 +0000 | [diff] [blame] | 239 | let subject_public_key = EcKey::from_cose_public_key_slice(&csr_payload.public_key).unwrap(); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 240 | let expected_spki_data = |
Alice Wang | eb77f7d | 2023-12-01 09:13:58 +0000 | [diff] [blame] | 241 | PKey::try_from(subject_public_key).unwrap().subject_public_key_info().unwrap(); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 242 | let expected_spki = SubjectPublicKeyInfo::from_der(&expected_spki_data).unwrap(); |
| 243 | assert_eq!(expected_spki, tbs_cert.subject_public_key_info); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 244 | |
| 245 | // Checks the certificate extension. |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 246 | const ATTESTATION_EXTENSION_OID: ObjectIdentifier = |
| 247 | ObjectIdentifier::new_unwrap("1.3.6.1.4.1.11129.2.1.29.1"); |
| 248 | let extensions = tbs_cert.extensions.unwrap(); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 249 | assert_eq!(1, extensions.len()); |
| 250 | let extension = &extensions[0]; |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 251 | assert_eq!(ATTESTATION_EXTENSION_OID, extension.extn_id); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 252 | assert!(!extension.critical); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 253 | let attestation_ext = |
| 254 | asn1::SequenceOf::<asn1::Any, 3>::from_der(extension.extn_value.as_bytes()).unwrap(); |
Alice Wang | 1cc1350 | 2023-12-05 11:05:34 +0000 | [diff] [blame] | 255 | assert_eq!(3, attestation_ext.len()); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 256 | let challenge = attestation_ext.get(0).unwrap().decode_as::<asn1::OctetString>().unwrap(); |
| 257 | assert_eq!(csr_payload.challenge, challenge.as_bytes()); |
| 258 | let is_vm_secure = attestation_ext.get(1).unwrap().decode_as::<bool>().unwrap(); |
Alice Wang | d3a9640 | 2023-11-24 15:37:39 +0000 | [diff] [blame] | 259 | assert!( |
| 260 | !is_vm_secure, |
| 261 | "The VM shouldn't be secure as the last payload added in the test is in Debug mode" |
| 262 | ); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 263 | let vm_components = |
| 264 | attestation_ext.get(2).unwrap().decode_as::<asn1::SequenceOf<asn1::Any, 4>>().unwrap(); |
| 265 | check_vm_components(&vm_components)?; |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 266 | |
| 267 | // Checks other fields on the certificate |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 268 | assert_eq!(Version::V3, tbs_cert.version); |
| 269 | assert_eq!(parent_certificate.tbs_certificate.validity, tbs_cert.validity); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 270 | assert_eq!( |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 271 | Name::from_str("CN=Android Protected Virtual Machine Key").unwrap(), |
| 272 | tbs_cert.subject |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 273 | ); |
Alice Wang | 6a504ef | 2023-12-21 15:37:55 +0000 | [diff] [blame] | 274 | assert_eq!(parent_certificate.tbs_certificate.subject, tbs_cert.issuer); |
Alice Wang | 20b8ebc | 2023-11-17 09:54:47 +0000 | [diff] [blame] | 275 | |
| 276 | Ok(()) |
| 277 | } |
| 278 | |
Karuna Wadhera | 4b4a519 | 2024-06-11 19:45:30 +0000 | [diff] [blame^] | 279 | fn check_csr(csr: Vec<u8>, vm_type: VmType) -> Result<()> { |
| 280 | let mut session = Session::default(); |
| 281 | |
| 282 | // Allow any mode for non-protected VMs because they use a fake DICE chain with the mode set to |
| 283 | // debug. |
| 284 | session.set_allow_any_mode(vm_type == VmType::NonProtectedVm); |
| 285 | |
| 286 | let _csr = rkp::Csr::from_cbor(&session, &csr[..]).context("Failed to parse CSR")?; |
Alice Wang | f7c0f94 | 2023-09-14 09:33:04 +0000 | [diff] [blame] | 287 | Ok(()) |
| 288 | } |
| 289 | |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 290 | fn start_service_vm(vm_type: VmType) -> Result<ServiceVm> { |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 291 | android_logger::init_once( |
Jeff Vander Stoep | d9dda0c | 2024-02-07 14:27:06 +0100 | [diff] [blame] | 292 | android_logger::Config::default() |
| 293 | .with_tag("rialto") |
| 294 | .with_max_level(log::LevelFilter::Debug), |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 295 | ); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 296 | // Redirect panic messages to logcat. |
| 297 | panic::set_hook(Box::new(|panic_info| { |
| 298 | log::error!("{}", panic_info); |
| 299 | })); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 300 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 301 | ProcessState::start_thread_pool(); |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 302 | ServiceVm::start_vm(vm_instance(vm_type)?, vm_type) |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Alice Wang | e910b90 | 2023-09-07 10:35:12 +0000 | [diff] [blame] | 305 | fn vm_instance(vm_type: VmType) -> Result<VmInstance> { |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 306 | match vm_type { |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame] | 307 | VmType::ProtectedVm => { |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 308 | service_vm_manager::protected_vm_instance(PathBuf::from(INSTANCE_IMG_PATH)) |
Alice Wang | 1d9a587 | 2023-09-06 14:32:36 +0000 | [diff] [blame] | 309 | } |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 310 | VmType::NonProtectedVm => nonprotected_vm_instance(), |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | fn nonprotected_vm_instance() -> Result<VmInstance> { |
| 315 | let rialto = File::open(UNSIGNED_RIALTO_PATH).context("Failed to open Rialto kernel binary")?; |
Alice Wang | b76b66f | 2024-03-26 08:16:23 +0000 | [diff] [blame] | 316 | // Do not use `#allocateInstanceId` to generate the instance ID because the method |
| 317 | // also adds an instance ID to the database it manages. |
| 318 | // This is not necessary for this test. |
| 319 | let mut instance_id = [0u8; 64]; |
| 320 | rand_bytes(&mut instance_id).unwrap(); |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 321 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 322 | name: String::from("Non protected rialto"), |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 323 | bootloader: Some(ParcelFileDescriptor::new(rialto)), |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 324 | protectedVm: false, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 325 | memoryMib: 300, |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 326 | platformVersion: "~1.0".to_string(), |
Alice Wang | b76b66f | 2024-03-26 08:16:23 +0000 | [diff] [blame] | 327 | instanceId: instance_id, |
Inseob Kim | 6ef8097 | 2023-07-20 17:23:36 +0900 | [diff] [blame] | 328 | ..Default::default() |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 329 | }); |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 330 | let console = Some(service_vm_manager::android_log_fd()?); |
| 331 | let log = Some(service_vm_manager::android_log_fd()?); |
| 332 | let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?; |
| 333 | let service = virtmgr.connect().context("Failed to connect to VirtMgr")?; |
| 334 | info!("Connected to VirtMgr for service VM"); |
| 335 | VmInstance::create(service.as_ref(), &config, console, /* consoleIn */ None, log, None) |
| 336 | .context("Failed to create VM") |
David Brazdil | 66fc120 | 2022-07-04 21:48:45 +0100 | [diff] [blame] | 337 | } |