• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 //! Provides functions to build fake DICE artifacts for client VM in tests.
18 
19 use crate::service_vm;
20 use alloc::vec;
21 use alloc::vec::Vec;
22 use ciborium::{cbor, value::Value};
23 use core::result;
24 use coset::CborSerializable;
25 use diced_open_dice::{
26     hash, retry_bcc_format_config_descriptor, retry_bcc_main_flow, Config, DiceArtifacts,
27     DiceConfigValues, DiceError, DiceMode, InputValues, OwnedDiceArtifacts, Result, HASH_SIZE,
28     HIDDEN_SIZE,
29 };
30 use log::error;
31 use microdroid_kernel_hashes::OS_HASHES;
32 
33 type CborResult<T> = result::Result<T, ciborium::value::Error>;
34 
35 /// All the following data are generated with urandom.
36 const CODE_HASH_PAYLOAD: [u8; HASH_SIZE] = [
37     0x08, 0x78, 0xc2, 0x5b, 0xe7, 0xea, 0x3d, 0x62, 0x70, 0x22, 0xd9, 0x1c, 0x4f, 0x3c, 0x2e, 0x2f,
38     0x0f, 0x97, 0xa4, 0x6f, 0x6d, 0xd5, 0xe6, 0x4a, 0x6d, 0xbe, 0x34, 0x2e, 0x56, 0x04, 0xaf, 0xef,
39     0x74, 0x3f, 0xec, 0xb8, 0x44, 0x11, 0xf4, 0x2f, 0x05, 0xb2, 0x06, 0xa3, 0x0e, 0x75, 0xb7, 0x40,
40     0x9a, 0x4c, 0x58, 0xab, 0x96, 0xe7, 0x07, 0x97, 0x07, 0x86, 0x5c, 0xa1, 0x42, 0x12, 0xf0, 0x34,
41 ];
42 const AUTHORITY_HASH_PAYLOAD: [u8; HASH_SIZE] = [
43     0xc7, 0x97, 0x5b, 0xa9, 0x9e, 0xbf, 0x0b, 0xeb, 0xe7, 0x7f, 0x69, 0x8f, 0x8e, 0xcf, 0x04, 0x7d,
44     0x2c, 0x0f, 0x4d, 0xbe, 0xcb, 0xf5, 0xf1, 0x4c, 0x1d, 0x1c, 0xb7, 0x44, 0xdf, 0xf8, 0x40, 0x90,
45     0x09, 0x65, 0xab, 0x01, 0x34, 0x3e, 0xc2, 0xc4, 0xf7, 0xa2, 0x3a, 0x5c, 0x4e, 0x76, 0x4f, 0x42,
46     0xa8, 0x6c, 0xc9, 0xf1, 0x7b, 0x12, 0x80, 0xa4, 0xef, 0xa2, 0x4d, 0x72, 0xa1, 0x21, 0xe2, 0x47,
47 ];
48 const APK1_CODE_HASH: &[u8] = &[
49     0x41, 0x92, 0x0d, 0xd0, 0xf5, 0x60, 0xe3, 0x69, 0x26, 0x7f, 0xb8, 0xbc, 0x12, 0x3a, 0xd1, 0x95,
50     0x1d, 0xb8, 0x9a, 0x9c, 0x3a, 0x3f, 0x01, 0xbf, 0xa8, 0xd9, 0x6d, 0xe9, 0x90, 0x30, 0x1d, 0x0b,
51 ];
52 const APK1_AUTHORITY_HASH: &[u8] = &[
53     0xe3, 0xd9, 0x1c, 0xf5, 0x6f, 0xee, 0x73, 0x40, 0x3d, 0x95, 0x59, 0x67, 0xea, 0x5d, 0x01, 0xfd,
54     0x25, 0x9d, 0x5c, 0x88, 0x94, 0x3a, 0xc6, 0xd7, 0xa9, 0xdc, 0x4c, 0x60, 0x81, 0xbe, 0x2b, 0x74,
55 ];
56 const APEX1_CODE_HASH: &[u8] = &[
57     0x52, 0x93, 0x2b, 0xb0, 0x8d, 0xec, 0xdf, 0x54, 0x1f, 0x5c, 0x10, 0x9d, 0x17, 0xce, 0x7f, 0xac,
58     0xb0, 0x2b, 0xe2, 0x99, 0x05, 0x7d, 0xa3, 0x9b, 0xa6, 0x3e, 0xf9, 0x99, 0xa2, 0xea, 0xd4, 0xd9,
59 ];
60 const APEX1_AUTHORITY_HASH: &[u8] = &[
61     0xd1, 0xfc, 0x3d, 0x5f, 0xa0, 0x5f, 0x02, 0xd0, 0x83, 0x9b, 0x0e, 0x32, 0xc2, 0x27, 0x09, 0x12,
62     0xcc, 0xfc, 0x42, 0xf6, 0x0d, 0xf4, 0x7d, 0xc8, 0x80, 0x1a, 0x64, 0x25, 0xa7, 0xfa, 0x4a, 0x37,
63 ];
64 
65 #[allow(missing_docs)]
66 #[derive(Debug, Clone, Eq, PartialEq)]
67 pub struct SubComponent {
68     pub name: String,
69     pub version: u64,
70     pub code_hash: Vec<u8>,
71     pub authority_hash: Vec<u8>,
72 }
73 
74 impl SubComponent {
to_value(&self) -> CborResult<Value>75     fn to_value(&self) -> CborResult<Value> {
76         Ok(cbor!({
77            1 => self.name,
78            2 => self.version,
79            3 => Value::Bytes(self.code_hash.clone()),
80            4 => Value::Bytes(self.authority_hash.clone()),
81         })?)
82     }
83 }
84 
85 /// Generates fake DICE artifacts for client VM with a DICE chain up to the certificate
86 /// describing the Microdroid payload.
87 ///
88 /// The fake DICE chain has the following nodes:
89 /// Root public key -> pvmfw certificate -> Microdroid kernel certificate
90 /// -> Microdroid payload certificate
fake_client_vm_dice_artifacts() -> Result<OwnedDiceArtifacts>91 pub fn fake_client_vm_dice_artifacts() -> Result<OwnedDiceArtifacts> {
92     // Client VM DICE chain has the same prefix as the service VM DICE chain up to
93     // the pvmfw entry.
94     let (cdi_values, dice_chain) = service_vm::fake_dice_artifacts_up_to_pvmfw()?;
95 
96     // Adds an entry describing the Microdroid kernel.
97     let config_values = DiceConfigValues {
98         component_name: Some(c"vm_entry"),
99         component_version: Some(12),
100         resettable: true,
101         security_version: Some(13),
102         ..Default::default()
103     };
104     let config_descriptor = retry_bcc_format_config_descriptor(&config_values)?;
105     // The Microdroid kernel is signed with the same key as the one used for the service VM,
106     // so the authority hash is the same.
107     let authority_hash = service_vm::AUTHORITY_HASH_SERVICE_VM;
108     let input_values = InputValues::new(
109         kernel_code_hash()?,
110         Config::Descriptor(config_descriptor.as_slice()),
111         authority_hash,
112         DiceMode::kDiceModeDebug,
113         [0; HIDDEN_SIZE], // No hidden.
114     );
115     let dice_artifacts = retry_bcc_main_flow(
116         &cdi_values.cdi_attest,
117         &cdi_values.cdi_seal,
118         &dice_chain,
119         &input_values,
120     )
121     .map_err(|e| {
122         error!("Failed to run the Microdroid kernel BCC main flow: {e}");
123         e
124     })?;
125 
126     // Adds an entry describing the Microdroid payload.
127     let config_descriptor = fake_microdroid_payload_config_descriptor().map_err(|e| {
128         error!("Failed to generate config descriptor for Microdroid: {e}");
129         DiceError::InvalidInput
130     })?;
131     let input_values = InputValues::new(
132         CODE_HASH_PAYLOAD,
133         Config::Descriptor(config_descriptor.as_slice()),
134         AUTHORITY_HASH_PAYLOAD,
135         DiceMode::kDiceModeDebug,
136         [0u8; HIDDEN_SIZE], // hidden
137     );
138     retry_bcc_main_flow(
139         dice_artifacts.cdi_attest(),
140         dice_artifacts.cdi_seal(),
141         dice_artifacts.bcc().unwrap(),
142         &input_values,
143     )
144     .map_err(|e| {
145         error!("Failed to run the Microdroid payload BCC main flow: {e}");
146         e
147     })
148 }
149 
fake_microdroid_payload_config_descriptor() -> CborResult<Vec<u8>>150 fn fake_microdroid_payload_config_descriptor() -> CborResult<Vec<u8>> {
151     let mut map = Vec::new();
152     map.push((cbor!(-70002)?, cbor!("Microdroid payload")?));
153     map.push((cbor!(-71000)?, cbor!("/config_path")?));
154     let components =
155         fake_sub_components().iter().map(|c| c.to_value()).collect::<CborResult<_>>()?;
156     map.push((cbor!(-71002)?, Value::Array(components)));
157     Ok(Value::Map(map).to_vec().unwrap())
158 }
159 
160 /// Generates a list of fake subcomponents as the Microdroid payload.
fake_sub_components() -> Vec<SubComponent>161 pub fn fake_sub_components() -> Vec<SubComponent> {
162     vec![
163         SubComponent {
164             name: "apk:com.android.apk.apk1".to_string(),
165             version: 1,
166             code_hash: APK1_CODE_HASH.to_vec(),
167             authority_hash: APK1_AUTHORITY_HASH.to_vec(),
168         },
169         SubComponent {
170             name: "apex:com.android.apex.apex1".to_string(),
171             version: 1,
172             code_hash: APEX1_CODE_HASH.to_vec(),
173             authority_hash: APEX1_AUTHORITY_HASH.to_vec(),
174         },
175     ]
176 }
177 
kernel_code_hash() -> Result<[u8; HASH_SIZE]>178 fn kernel_code_hash() -> Result<[u8; HASH_SIZE]> {
179     let os_hashes = &OS_HASHES[0];
180     let code_hash = [os_hashes.kernel, os_hashes.initrd_debug].concat();
181     hash(&code_hash)
182 }
183