Alice Wang | 9c40eca | 2023-02-03 13:10:24 +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 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 15 | //! This module mirrors the content in open-dice/include/dice/android.h |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 16 | |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 17 | use crate::dice::{Cdi, CdiValues, DiceArtifacts, InputValues, CDI_SIZE}; |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 18 | use crate::error::{check_result, DiceError, Result}; |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 19 | use open_dice_android_bindgen::{ |
| 20 | DiceAndroidConfigValues, DiceAndroidFormatConfigDescriptor, DiceAndroidHandoverMainFlow, |
| 21 | DiceAndroidHandoverParse, DiceAndroidMainFlow, DICE_ANDROID_CONFIG_COMPONENT_NAME, |
| 22 | DICE_ANDROID_CONFIG_COMPONENT_VERSION, DICE_ANDROID_CONFIG_RESETTABLE, |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 23 | }; |
| 24 | use std::{ffi::CStr, ptr}; |
| 25 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 26 | /// Formats a configuration descriptor following the Android Profile for DICE specification. |
| 27 | /// See https://pigweed.googlesource.com/open-dice/+/refs/heads/main/docs/android.md |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 28 | pub fn bcc_format_config_descriptor( |
| 29 | name: Option<&CStr>, |
| 30 | version: Option<u64>, |
| 31 | resettable: bool, |
| 32 | buffer: &mut [u8], |
| 33 | ) -> Result<usize> { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 34 | let mut configs = 0; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 35 | if name.is_some() { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 36 | configs |= DICE_ANDROID_CONFIG_COMPONENT_NAME; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 37 | } |
| 38 | if version.is_some() { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 39 | configs |= DICE_ANDROID_CONFIG_COMPONENT_VERSION; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 40 | } |
| 41 | if resettable { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 42 | configs |= DICE_ANDROID_CONFIG_RESETTABLE; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 45 | let values = DiceAndroidConfigValues { |
| 46 | configs, |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 47 | component_name: name.map_or(ptr::null(), |p| p.as_ptr()), |
| 48 | component_version: version.unwrap_or(0), |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 49 | security_version: 0, |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | let mut buffer_size = 0; |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 53 | check_result( |
Andrew Walbran | 7f30e54 | 2023-07-07 13:42:25 +0100 | [diff] [blame] | 54 | // SAFETY: The function writes to the buffer, within the given bounds, and only reads the |
| 55 | // input values. It writes its result to buffer_size. |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 56 | unsafe { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 57 | DiceAndroidFormatConfigDescriptor( |
| 58 | &values, |
| 59 | buffer.len(), |
| 60 | buffer.as_mut_ptr(), |
| 61 | &mut buffer_size, |
| 62 | ) |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 63 | }, |
| 64 | buffer_size, |
| 65 | )?; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 66 | Ok(buffer_size) |
| 67 | } |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 68 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 69 | /// Executes the main Android DICE flow. |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 70 | /// |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 71 | /// Given a full set of input values along with the current DICE chain and CDI values, |
| 72 | /// computes the next CDI values and matching updated DICE chain. |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 73 | pub fn bcc_main_flow( |
| 74 | current_cdi_attest: &Cdi, |
| 75 | current_cdi_seal: &Cdi, |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 76 | current_chain: &[u8], |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 77 | input_values: &InputValues, |
| 78 | next_cdi_values: &mut CdiValues, |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 79 | next_chain: &mut [u8], |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 80 | ) -> Result<usize> { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 81 | let mut next_chain_size = 0; |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 82 | check_result( |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 83 | // SAFETY: `DiceAndroidMainFlow` only reads the `current_chain` and CDI values and writes |
| 84 | // to `next_chain` and next CDI values within its bounds. It also reads `input_values` as a |
| 85 | // constant input and doesn't store any pointer. |
Andrew Walbran | 7f30e54 | 2023-07-07 13:42:25 +0100 | [diff] [blame] | 86 | // The first argument can be null and is not used in the current implementation. |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 87 | unsafe { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 88 | DiceAndroidMainFlow( |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 89 | ptr::null_mut(), // context |
| 90 | current_cdi_attest.as_ptr(), |
| 91 | current_cdi_seal.as_ptr(), |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 92 | current_chain.as_ptr(), |
| 93 | current_chain.len(), |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 94 | input_values.as_ptr(), |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 95 | next_chain.len(), |
| 96 | next_chain.as_mut_ptr(), |
| 97 | &mut next_chain_size, |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 98 | next_cdi_values.cdi_attest.as_mut_ptr(), |
| 99 | next_cdi_values.cdi_seal.as_mut_ptr(), |
| 100 | ) |
| 101 | }, |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 102 | next_chain_size, |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 103 | )?; |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 104 | Ok(next_chain_size) |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame] | 105 | } |
Alice Wang | 9b2c38e | 2023-02-14 08:53:31 +0000 | [diff] [blame] | 106 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 107 | /// Executes the main Android DICE handover flow. |
Alice Wang | 9b2c38e | 2023-02-14 08:53:31 +0000 | [diff] [blame] | 108 | /// |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 109 | /// A handover combines the DICE chain and CDIs in a single CBOR object. |
| 110 | /// This function takes the current boot stage's handover bundle and produces a |
Alice Wang | 9b2c38e | 2023-02-14 08:53:31 +0000 | [diff] [blame] | 111 | /// bundle for the next stage. |
| 112 | pub fn bcc_handover_main_flow( |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 113 | current_handover: &[u8], |
Alice Wang | 9b2c38e | 2023-02-14 08:53:31 +0000 | [diff] [blame] | 114 | input_values: &InputValues, |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 115 | next_handover: &mut [u8], |
Alice Wang | 9b2c38e | 2023-02-14 08:53:31 +0000 | [diff] [blame] | 116 | ) -> Result<usize> { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 117 | let mut next_handover_size = 0; |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 118 | check_result( |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 119 | // SAFETY: The function only reads `current_handover` and writes to `next_handover` |
Andrew Walbran | 7f30e54 | 2023-07-07 13:42:25 +0100 | [diff] [blame] | 120 | // within its bounds, |
| 121 | // It also reads `input_values` as a constant input and doesn't store any pointer. |
| 122 | // The first argument can be null and is not used in the current implementation. |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 123 | unsafe { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 124 | DiceAndroidHandoverMainFlow( |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 125 | ptr::null_mut(), // context |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 126 | current_handover.as_ptr(), |
| 127 | current_handover.len(), |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 128 | input_values.as_ptr(), |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 129 | next_handover.len(), |
| 130 | next_handover.as_mut_ptr(), |
| 131 | &mut next_handover_size, |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 132 | ) |
| 133 | }, |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 134 | next_handover_size, |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 135 | )?; |
Alice Wang | 9b2c38e | 2023-02-14 08:53:31 +0000 | [diff] [blame] | 136 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 137 | Ok(next_handover_size) |
Alice Wang | 9b2c38e | 2023-02-14 08:53:31 +0000 | [diff] [blame] | 138 | } |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 139 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 140 | /// An Android DICE handover object combines the DICE chain and CDIs in a single CBOR object. |
| 141 | /// This struct is used as return of the function `android_dice_handover_parse`, its lifetime is |
| 142 | /// tied to the lifetime of the raw handover slice. |
Alice Wang | acee4fb | 2023-02-15 09:42:07 +0000 | [diff] [blame] | 143 | #[derive(Debug)] |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 144 | pub struct BccHandover<'a> { |
| 145 | /// Attestation CDI. |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 146 | cdi_attest: &'a [u8; CDI_SIZE], |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 147 | /// Sealing CDI. |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 148 | cdi_seal: &'a [u8; CDI_SIZE], |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 149 | /// DICE chain. |
Alice Wang | 4d3059a | 2023-02-15 10:24:33 +0000 | [diff] [blame] | 150 | bcc: Option<&'a [u8]>, |
| 151 | } |
| 152 | |
| 153 | impl<'a> DiceArtifacts for BccHandover<'a> { |
| 154 | fn cdi_attest(&self) -> &[u8; CDI_SIZE] { |
| 155 | self.cdi_attest |
| 156 | } |
| 157 | |
| 158 | fn cdi_seal(&self) -> &[u8; CDI_SIZE] { |
| 159 | self.cdi_seal |
| 160 | } |
| 161 | |
| 162 | fn bcc(&self) -> Option<&[u8]> { |
| 163 | self.bcc |
| 164 | } |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 167 | /// This function parses the `handover` to extracts the DICE chain and CDIs. |
| 168 | /// The lifetime of the returned `DiceAndroidHandover` is tied to the given `handover` slice. |
| 169 | pub fn bcc_handover_parse(handover: &[u8]) -> Result<BccHandover> { |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 170 | let mut cdi_attest: *const u8 = ptr::null(); |
| 171 | let mut cdi_seal: *const u8 = ptr::null(); |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 172 | let mut chain: *const u8 = ptr::null(); |
| 173 | let mut chain_size = 0; |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 174 | check_result( |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 175 | // SAFETY: The `handover` is only read and never stored and the returned pointers should |
| 176 | // all point within the address range of the `handover` or be NULL. |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 177 | unsafe { |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 178 | DiceAndroidHandoverParse( |
| 179 | handover.as_ptr(), |
| 180 | handover.len(), |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 181 | &mut cdi_attest, |
| 182 | &mut cdi_seal, |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 183 | &mut chain, |
| 184 | &mut chain_size, |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 185 | ) |
| 186 | }, |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 187 | chain_size, |
Alice Wang | ef99924 | 2023-05-22 11:14:59 +0000 | [diff] [blame] | 188 | )?; |
Andrew Scull | 0275aa5 | 2023-08-18 18:03:11 +0000 | [diff] [blame^] | 189 | let cdi_attest = sub_slice(handover, cdi_attest, CDI_SIZE)?; |
| 190 | let cdi_seal = sub_slice(handover, cdi_seal, CDI_SIZE)?; |
| 191 | let bcc = sub_slice(handover, chain, chain_size).ok(); |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 192 | Ok(BccHandover { |
| 193 | cdi_attest: cdi_attest.try_into().map_err(|_| DiceError::PlatformError)?, |
| 194 | cdi_seal: cdi_seal.try_into().map_err(|_| DiceError::PlatformError)?, |
| 195 | bcc, |
| 196 | }) |
| 197 | } |
| 198 | |
| 199 | /// Gets a slice the `addr` points to and of length `len`. |
| 200 | /// The slice should be contained in the buffer. |
| 201 | fn sub_slice(buffer: &[u8], addr: *const u8, len: usize) -> Result<&[u8]> { |
| 202 | if addr.is_null() || !buffer.as_ptr_range().contains(&addr) { |
| 203 | return Err(DiceError::PlatformError); |
| 204 | } |
| 205 | // SAFETY: This is safe because addr is not null and is within the range of the buffer. |
| 206 | let start: usize = unsafe { |
| 207 | addr.offset_from(buffer.as_ptr()).try_into().map_err(|_| DiceError::PlatformError)? |
| 208 | }; |
Alice Wang | acee4fb | 2023-02-15 09:42:07 +0000 | [diff] [blame] | 209 | start.checked_add(len).and_then(|end| buffer.get(start..end)).ok_or(DiceError::PlatformError) |
Alice Wang | 1a933a8 | 2023-02-14 11:02:51 +0000 | [diff] [blame] | 210 | } |