Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 1 | // Copyright 2023 The Android Open Source Project |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 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 | // |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 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 | |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 15 | use crate::dice_driver::DiceDriver; |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 16 | use crate::instance::ApkData; |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 17 | use crate::{is_debuggable, MicrodroidData}; |
| 18 | use anyhow::{bail, Context, Result}; |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 19 | use ciborium::{cbor, Value}; |
| 20 | use coset::CborSerializable; |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 21 | use diced_open_dice::OwnedDiceArtifacts; |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 22 | use microdroid_metadata::PayloadMetadata; |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 23 | use openssl::sha::{sha512, Sha512}; |
| 24 | use std::iter::once; |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 25 | |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 26 | /// Perform an open DICE derivation for the payload. |
| 27 | pub fn dice_derivation( |
| 28 | dice: DiceDriver, |
| 29 | verified_data: &MicrodroidData, |
| 30 | payload_metadata: &PayloadMetadata, |
| 31 | ) -> Result<OwnedDiceArtifacts> { |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 32 | let subcomponents = build_subcomponent_list(verified_data); |
| 33 | |
| 34 | let config_descriptor = format_payload_config_descriptor(payload_metadata, &subcomponents) |
| 35 | .context("Building config descriptor")?; |
| 36 | |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 37 | // Calculate compound digests of code and authorities |
| 38 | let mut code_hash_ctx = Sha512::new(); |
| 39 | let mut authority_hash_ctx = Sha512::new(); |
| 40 | code_hash_ctx.update(verified_data.apk_data.root_hash.as_ref()); |
| 41 | authority_hash_ctx.update(verified_data.apk_data.pubkey.as_ref()); |
| 42 | for extra_apk in &verified_data.extra_apks_data { |
| 43 | code_hash_ctx.update(extra_apk.root_hash.as_ref()); |
| 44 | authority_hash_ctx.update(extra_apk.pubkey.as_ref()); |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 45 | } |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 46 | for apex in &verified_data.apex_data { |
| 47 | code_hash_ctx.update(apex.root_digest.as_ref()); |
| 48 | authority_hash_ctx.update(apex.public_key.as_ref()); |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 49 | } |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 50 | let code_hash = code_hash_ctx.finish(); |
| 51 | let authority_hash = authority_hash_ctx.finish(); |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 52 | |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 53 | // Check debuggability, conservatively assuming it is debuggable |
| 54 | let debuggable = is_debuggable()?; |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 55 | |
Alan Stokes | 1125e01 | 2023-10-13 12:31:10 +0100 | [diff] [blame] | 56 | // Send the details to diced |
| 57 | let hidden = verified_data.salt.clone().try_into().unwrap(); |
| 58 | dice.derive(code_hash, &config_descriptor, authority_hash, debuggable, hidden) |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 59 | } |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 60 | |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 61 | struct Subcomponent<'a> { |
| 62 | name: String, |
| 63 | version: u64, |
| 64 | code_hash: &'a [u8], |
| 65 | authority_hash: Box<[u8]>, |
| 66 | } |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 67 | |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 68 | impl<'a> Subcomponent<'a> { |
| 69 | fn to_value(&self) -> Result<Value> { |
| 70 | Ok(cbor!({ |
| 71 | 1 => self.name, |
| 72 | 2 => self.version, |
| 73 | 3 => self.code_hash, |
| 74 | 4 => self.authority_hash |
| 75 | })?) |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 76 | } |
Ludovic Barman | 93ee308 | 2023-06-20 12:18:43 +0000 | [diff] [blame] | 77 | |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 78 | fn for_apk(apk: &'a ApkData) -> Self { |
| 79 | Self { |
| 80 | name: format!("apk:{}", apk.package_name), |
| 81 | version: apk.version_code, |
| 82 | code_hash: &apk.root_hash, |
| 83 | authority_hash: |
| 84 | // TODO(b/305925597): Hash the certificate not the pubkey |
| 85 | Box::new(sha512(&apk.pubkey)), |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn build_subcomponent_list(verified_data: &MicrodroidData) -> Vec<Subcomponent> { |
| 91 | if !cfg!(dice_changes) { |
| 92 | return vec![]; |
| 93 | } |
| 94 | |
| 95 | once(&verified_data.apk_data) |
| 96 | .chain(&verified_data.extra_apks_data) |
| 97 | .map(Subcomponent::for_apk) |
| 98 | .collect() |
| 99 | } |
| 100 | |
| 101 | // Returns a configuration descriptor of the given payload. See vm_config.cddl for a definition |
| 102 | // of the format. |
| 103 | fn format_payload_config_descriptor( |
| 104 | payload: &PayloadMetadata, |
| 105 | subcomponents: &[Subcomponent], |
| 106 | ) -> Result<Vec<u8>> { |
| 107 | let mut map = Vec::new(); |
| 108 | map.push((cbor!(-70002)?, cbor!("Microdroid payload")?)); |
| 109 | map.push(match payload { |
| 110 | PayloadMetadata::ConfigPath(payload_config_path) => { |
| 111 | (cbor!(-71000)?, cbor!(payload_config_path)?) |
| 112 | } |
| 113 | PayloadMetadata::Config(payload_config) => { |
| 114 | (cbor!(-71001)?, cbor!({1 => payload_config.payload_binary_name})?) |
| 115 | } |
| 116 | _ => bail!("Failed to match the payload against a config type: {:?}", payload), |
| 117 | }); |
| 118 | |
| 119 | if !subcomponents.is_empty() { |
| 120 | let values = |
| 121 | subcomponents.iter().map(Subcomponent::to_value).collect::<Result<Vec<_>>>()?; |
| 122 | map.push((cbor!(-71002)?, cbor!(values)?)); |
| 123 | } |
| 124 | |
| 125 | Ok(Value::Map(map).to_vec()?) |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 128 | #[cfg(test)] |
| 129 | mod tests { |
| 130 | use super::*; |
| 131 | use microdroid_metadata::PayloadConfig; |
| 132 | |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 133 | const NO_SUBCOMPONENTS: [Subcomponent; 0] = []; |
| 134 | |
| 135 | fn assert_eq_bytes(expected: &[u8], actual: &[u8]) { |
| 136 | assert_eq!( |
| 137 | expected, |
| 138 | actual, |
| 139 | "Expected {}, got {}", |
| 140 | hex::encode(expected), |
| 141 | hex::encode(actual) |
| 142 | ) |
| 143 | } |
| 144 | |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 145 | #[test] |
| 146 | fn payload_metadata_with_path_formats_correctly() -> Result<()> { |
Ludovic Barman | 93ee308 | 2023-06-20 12:18:43 +0000 | [diff] [blame] | 147 | let payload_metadata = PayloadMetadata::ConfigPath("/config_path".to_string()); |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 148 | let config_descriptor = |
| 149 | format_payload_config_descriptor(&payload_metadata, &NO_SUBCOMPONENTS)?; |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 150 | static EXPECTED_CONFIG_DESCRIPTOR: &[u8] = &[ |
| 151 | 0xa2, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x72, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x64, 0x72, |
| 152 | 0x6f, 0x69, 0x64, 0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x3a, 0x00, 0x01, |
| 153 | 0x15, 0x57, 0x6c, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x5f, 0x70, 0x61, 0x74, |
| 154 | 0x68, |
| 155 | ]; |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 156 | assert_eq_bytes(EXPECTED_CONFIG_DESCRIPTOR, &config_descriptor); |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 157 | Ok(()) |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn payload_metadata_with_config_formats_correctly() -> Result<()> { |
| 162 | let payload_config = PayloadConfig { |
| 163 | payload_binary_name: "payload_binary".to_string(), |
| 164 | ..Default::default() |
| 165 | }; |
Ludovic Barman | 93ee308 | 2023-06-20 12:18:43 +0000 | [diff] [blame] | 166 | let payload_metadata = PayloadMetadata::Config(payload_config); |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 167 | let config_descriptor = |
| 168 | format_payload_config_descriptor(&payload_metadata, &NO_SUBCOMPONENTS)?; |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 169 | static EXPECTED_CONFIG_DESCRIPTOR: &[u8] = &[ |
| 170 | 0xa2, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x72, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x64, 0x72, |
| 171 | 0x6f, 0x69, 0x64, 0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x3a, 0x00, 0x01, |
| 172 | 0x15, 0x58, 0xa1, 0x01, 0x6e, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x62, |
| 173 | 0x69, 0x6e, 0x61, 0x72, 0x79, |
| 174 | ]; |
Alan Stokes | 9b8b8ec | 2023-10-13 15:58:11 +0100 | [diff] [blame^] | 175 | assert_eq_bytes(EXPECTED_CONFIG_DESCRIPTOR, &config_descriptor); |
| 176 | Ok(()) |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn payload_metadata_with_subcomponents_formats_correctly() -> Result<()> { |
| 181 | let payload_metadata = PayloadMetadata::ConfigPath("/config_path".to_string()); |
| 182 | let subcomponents = [ |
| 183 | Subcomponent { |
| 184 | name: "apk1".to_string(), |
| 185 | version: 1, |
| 186 | code_hash: &[42u8], |
| 187 | authority_hash: Box::new([17u8]), |
| 188 | }, |
| 189 | Subcomponent { |
| 190 | name: "apk2".to_string(), |
| 191 | version: 0x1000_0000_0001, |
| 192 | code_hash: &[43u8], |
| 193 | authority_hash: Box::new([19u8]), |
| 194 | }, |
| 195 | ]; |
| 196 | let config_descriptor = |
| 197 | format_payload_config_descriptor(&payload_metadata, &subcomponents)?; |
| 198 | // Verified using cbor.me. |
| 199 | static EXPECTED_CONFIG_DESCRIPTOR: &[u8] = &[ |
| 200 | 0xa3, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x72, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x64, 0x72, |
| 201 | 0x6f, 0x69, 0x64, 0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x3a, 0x00, 0x01, |
| 202 | 0x15, 0x57, 0x6c, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x5f, 0x70, 0x61, 0x74, |
| 203 | 0x68, 0x3a, 0x00, 0x01, 0x15, 0x59, 0x82, 0xa4, 0x01, 0x64, 0x61, 0x70, 0x6b, 0x31, |
| 204 | 0x02, 0x01, 0x03, 0x81, 0x18, 0x2a, 0x04, 0x81, 0x11, 0xa4, 0x01, 0x64, 0x61, 0x70, |
| 205 | 0x6b, 0x32, 0x02, 0x1b, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x81, |
| 206 | 0x18, 0x2b, 0x04, 0x81, 0x13, |
| 207 | ]; |
| 208 | assert_eq_bytes(EXPECTED_CONFIG_DESCRIPTOR, &config_descriptor); |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 209 | Ok(()) |
| 210 | } |
| 211 | } |