Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [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 | //! Logic for handling the DICE values and boot operations. |
| 16 | |
Alice Wang | 6a22be9 | 2023-02-15 10:08:36 +0000 | [diff] [blame] | 17 | use anyhow::{anyhow, bail, Context, Error, Result}; |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 18 | use byteorder::{NativeEndian, ReadBytesExt}; |
Alice Wang | a04890b | 2023-03-01 11:45:09 +0000 | [diff] [blame] | 19 | use ciborium::{cbor, ser}; |
Alice Wang | f4b8b00 | 2023-02-08 08:53:23 +0000 | [diff] [blame] | 20 | use diced_open_dice::{ |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 21 | bcc_handover_parse, retry_bcc_main_flow, BccHandover, Config, DiceArtifacts, DiceMode, Hash, |
| 22 | Hidden, InputValues, OwnedDiceArtifacts, |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 23 | }; |
| 24 | use keystore2_crypto::ZVec; |
| 25 | use libc::{c_void, mmap, munmap, MAP_FAILED, MAP_PRIVATE, PROT_READ}; |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 26 | use microdroid_metadata::PayloadMetadata; |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 27 | use openssl::hkdf::hkdf; |
| 28 | use openssl::md::Md; |
| 29 | use std::fs; |
| 30 | use std::os::unix::io::AsRawFd; |
| 31 | use std::path::{Path, PathBuf}; |
| 32 | use std::ptr::null_mut; |
| 33 | use std::slice; |
| 34 | |
Alice Wang | 62f7e64 | 2023-02-10 09:55:13 +0000 | [diff] [blame] | 35 | /// Derives a sealing key from the DICE sealing CDI. |
| 36 | pub fn derive_sealing_key( |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 37 | dice_artifacts: &dyn DiceArtifacts, |
Alice Wang | 62f7e64 | 2023-02-10 09:55:13 +0000 | [diff] [blame] | 38 | salt: &[u8], |
| 39 | info: &[u8], |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 40 | key: &mut [u8], |
| 41 | ) -> Result<()> { |
| 42 | Ok(hkdf(key, Md::sha256(), dice_artifacts.cdi_seal(), salt, info)?) |
Shikha Panwar | 566c967 | 2022-11-15 14:39:58 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 45 | /// Artifacts that are mapped into the process address space from the driver. |
| 46 | pub enum DiceDriver<'a> { |
| 47 | Real { |
| 48 | driver_path: PathBuf, |
| 49 | mmap_addr: *mut c_void, |
| 50 | mmap_size: usize, |
Alice Wang | 6a22be9 | 2023-02-15 10:08:36 +0000 | [diff] [blame] | 51 | bcc_handover: BccHandover<'a>, |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 52 | }, |
Alice Wang | 62f7e64 | 2023-02-10 09:55:13 +0000 | [diff] [blame] | 53 | Fake(OwnedDiceArtifacts), |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | impl DiceDriver<'_> { |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 57 | fn dice_artifacts(&self) -> &dyn DiceArtifacts { |
| 58 | match self { |
| 59 | Self::Real { bcc_handover, .. } => bcc_handover, |
| 60 | Self::Fake(owned_dice_artifacts) => owned_dice_artifacts, |
| 61 | } |
| 62 | } |
| 63 | |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 64 | pub fn new(driver_path: &Path) -> Result<Self> { |
| 65 | if driver_path.exists() { |
| 66 | log::info!("Using DICE values from driver"); |
| 67 | } else if super::is_strict_boot() { |
| 68 | bail!("Strict boot requires DICE value from driver but none were found"); |
| 69 | } else { |
| 70 | log::warn!("Using sample DICE values"); |
Alice Wang | f4b8b00 | 2023-02-08 08:53:23 +0000 | [diff] [blame] | 71 | let dice_artifacts = diced_sample_inputs::make_sample_bcc_and_cdis() |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 72 | .expect("Failed to create sample dice artifacts."); |
Alice Wang | 62f7e64 | 2023-02-10 09:55:13 +0000 | [diff] [blame] | 73 | return Ok(Self::Fake(dice_artifacts)); |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | let mut file = fs::File::open(driver_path) |
| 77 | .map_err(|error| Error::new(error).context("Opening driver"))?; |
| 78 | let mmap_size = |
| 79 | file.read_u64::<NativeEndian>() |
| 80 | .map_err(|error| Error::new(error).context("Reading driver"))? as usize; |
| 81 | // It's safe to map the driver as the service will only create a single |
| 82 | // mapping per process. |
| 83 | let mmap_addr = unsafe { |
| 84 | let fd = file.as_raw_fd(); |
| 85 | mmap(null_mut(), mmap_size, PROT_READ, MAP_PRIVATE, fd, 0) |
| 86 | }; |
| 87 | if mmap_addr == MAP_FAILED { |
| 88 | bail!("Failed to mmap {:?}", driver_path); |
| 89 | } |
| 90 | // The slice is created for the region of memory that was just |
| 91 | // successfully mapped into the process address space so it will be |
| 92 | // accessible and not referenced from anywhere else. |
| 93 | let mmap_buf = |
| 94 | unsafe { slice::from_raw_parts((mmap_addr as *const u8).as_ref().unwrap(), mmap_size) }; |
Alice Wang | 6a22be9 | 2023-02-15 10:08:36 +0000 | [diff] [blame] | 95 | let bcc_handover = |
| 96 | bcc_handover_parse(mmap_buf).map_err(|_| anyhow!("Failed to parse Bcc Handover"))?; |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 97 | Ok(Self::Real { |
| 98 | driver_path: driver_path.to_path_buf(), |
| 99 | mmap_addr, |
| 100 | mmap_size, |
Alice Wang | 6a22be9 | 2023-02-15 10:08:36 +0000 | [diff] [blame] | 101 | bcc_handover, |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 102 | }) |
| 103 | } |
| 104 | |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 105 | /// Derives a sealing key of `key_length` bytes from the DICE sealing CDI. |
| 106 | pub fn get_sealing_key(&self, identifier: &[u8], key_length: usize) -> Result<ZVec> { |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 107 | // Deterministically derive a key to use for sealing data, rather than using the CDI |
| 108 | // directly, so we have the chance to rotate the key if needed. A salt isn't needed as the |
| 109 | // input key material is already cryptographically strong. |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 110 | let mut key = ZVec::new(key_length)?; |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 111 | let salt = &[]; |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 112 | derive_sealing_key(self.dice_artifacts(), salt, identifier, &mut key)?; |
| 113 | Ok(key) |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | pub fn derive( |
| 117 | self, |
Alice Wang | 5aeed33 | 2023-02-02 09:42:21 +0000 | [diff] [blame] | 118 | code_hash: Hash, |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 119 | config_desc: &[u8], |
Alice Wang | 5aeed33 | 2023-02-02 09:42:21 +0000 | [diff] [blame] | 120 | authority_hash: Hash, |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 121 | debug: bool, |
Alice Wang | 5aeed33 | 2023-02-02 09:42:21 +0000 | [diff] [blame] | 122 | hidden: Hidden, |
Alice Wang | 62f7e64 | 2023-02-10 09:55:13 +0000 | [diff] [blame] | 123 | ) -> Result<OwnedDiceArtifacts> { |
Alice Wang | a777366 | 2023-02-03 09:37:17 +0000 | [diff] [blame] | 124 | let input_values = InputValues::new( |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 125 | code_hash, |
| 126 | Config::Descriptor(config_desc), |
| 127 | authority_hash, |
Alice Wang | 3122613 | 2023-01-31 12:44:39 +0000 | [diff] [blame] | 128 | if debug { DiceMode::kDiceModeDebug } else { DiceMode::kDiceModeNormal }, |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 129 | hidden, |
| 130 | ); |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 131 | let current_dice_artifacts = self.dice_artifacts(); |
| 132 | let next_dice_artifacts = retry_bcc_main_flow( |
| 133 | current_dice_artifacts.cdi_attest(), |
| 134 | current_dice_artifacts.cdi_seal(), |
| 135 | current_dice_artifacts.bcc().ok_or_else(|| anyhow!("bcc is none"))?, |
| 136 | &input_values, |
| 137 | ) |
| 138 | .context("DICE derive from driver")?; |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 139 | if let Self::Real { driver_path, .. } = &self { |
| 140 | // Writing to the device wipes the artifacts. The string is ignored by the driver but |
| 141 | // included for documentation. |
| 142 | fs::write(driver_path, "wipe") |
| 143 | .map_err(|err| Error::new(err).context("Wiping driver"))?; |
| 144 | } |
Alice Wang | 7e6c935 | 2023-02-15 15:44:13 +0000 | [diff] [blame] | 145 | Ok(next_dice_artifacts) |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | impl Drop for DiceDriver<'_> { |
| 150 | fn drop(&mut self) { |
| 151 | if let &mut Self::Real { mmap_addr, mmap_size, .. } = self { |
| 152 | // All references to the mapped region have the same lifetime as self. Since self is |
| 153 | // being dropped, so are all the references to the mapped region meaning its safe to |
| 154 | // unmap. |
| 155 | let ret = unsafe { munmap(mmap_addr, mmap_size) }; |
| 156 | if ret != 0 { |
| 157 | log::warn!("Failed to munmap ({})", ret); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 162 | |
| 163 | /// Returns a configuration descriptor of the given payload following the BCC's specification: |
| 164 | /// https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/security/rkp/aidl/android/hardware/security/keymint/ProtectedData.aidl |
| 165 | /// { |
| 166 | /// -70002: "Microdroid payload", |
| 167 | /// ? -71000: tstr // payload_config_path |
| 168 | /// ? -71001: PayloadConfig |
| 169 | /// } |
| 170 | /// PayloadConfig = { |
| 171 | /// 1: tstr // payload_binary_name |
| 172 | /// } |
| 173 | pub fn format_payload_config_descriptor(payload_metadata: &PayloadMetadata) -> Result<Vec<u8>> { |
Alice Wang | a04890b | 2023-03-01 11:45:09 +0000 | [diff] [blame] | 174 | const MICRODROID_PAYLOAD_COMPONENT_NAME: &str = "Microdroid payload"; |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 175 | |
Alice Wang | a04890b | 2023-03-01 11:45:09 +0000 | [diff] [blame] | 176 | let config_descriptor_cbor_value = match payload_metadata { |
| 177 | PayloadMetadata::config_path(payload_config_path) => cbor!({ |
| 178 | -70002 => MICRODROID_PAYLOAD_COMPONENT_NAME, |
| 179 | -71000 => payload_config_path |
| 180 | }), |
| 181 | PayloadMetadata::config(payload_config) => cbor!({ |
| 182 | -70002 => MICRODROID_PAYLOAD_COMPONENT_NAME, |
| 183 | -71001 => {1 => payload_config.payload_binary_name} |
| 184 | }), |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 185 | } |
Alice Wang | a04890b | 2023-03-01 11:45:09 +0000 | [diff] [blame] | 186 | .context("Failed to build a CBOR Value from payload metadata")?; |
| 187 | let mut config_descriptor = Vec::new(); |
| 188 | ser::into_writer(&config_descriptor_cbor_value, &mut config_descriptor)?; |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 189 | Ok(config_descriptor) |
| 190 | } |
| 191 | |
Alice Wang | 285a3d2 | 2023-03-01 11:36:29 +0000 | [diff] [blame] | 192 | #[cfg(test)] |
| 193 | mod tests { |
| 194 | use super::*; |
| 195 | use microdroid_metadata::PayloadConfig; |
| 196 | |
| 197 | #[test] |
| 198 | fn payload_metadata_with_path_formats_correctly() -> Result<()> { |
| 199 | let payload_metadata = PayloadMetadata::config_path("/config_path".to_string()); |
| 200 | let config_descriptor = format_payload_config_descriptor(&payload_metadata)?; |
| 201 | static EXPECTED_CONFIG_DESCRIPTOR: &[u8] = &[ |
| 202 | 0xa2, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x72, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x64, 0x72, |
| 203 | 0x6f, 0x69, 0x64, 0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x3a, 0x00, 0x01, |
| 204 | 0x15, 0x57, 0x6c, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x5f, 0x70, 0x61, 0x74, |
| 205 | 0x68, |
| 206 | ]; |
| 207 | assert_eq!(EXPECTED_CONFIG_DESCRIPTOR, &config_descriptor); |
| 208 | Ok(()) |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn payload_metadata_with_config_formats_correctly() -> Result<()> { |
| 213 | let payload_config = PayloadConfig { |
| 214 | payload_binary_name: "payload_binary".to_string(), |
| 215 | ..Default::default() |
| 216 | }; |
| 217 | let payload_metadata = PayloadMetadata::config(payload_config); |
| 218 | let config_descriptor = format_payload_config_descriptor(&payload_metadata)?; |
| 219 | static EXPECTED_CONFIG_DESCRIPTOR: &[u8] = &[ |
| 220 | 0xa2, 0x3a, 0x00, 0x01, 0x11, 0x71, 0x72, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x64, 0x72, |
| 221 | 0x6f, 0x69, 0x64, 0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x3a, 0x00, 0x01, |
| 222 | 0x15, 0x58, 0xa1, 0x01, 0x6e, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x62, |
| 223 | 0x69, 0x6e, 0x61, 0x72, 0x79, |
| 224 | ]; |
| 225 | assert_eq!(EXPECTED_CONFIG_DESCRIPTOR, &config_descriptor); |
| 226 | Ok(()) |
| 227 | } |
| 228 | } |