blob: 543cb57c039400c01c606b51c0d2841e87fed3e8 [file] [log] [blame]
Alice Wang9c40eca2023-02-03 13:10:24 +00001// 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 Scull0275aa52023-08-18 18:03:11 +000015//! This module mirrors the content in open-dice/include/dice/android.h
Alice Wang9c40eca2023-02-03 13:10:24 +000016
Alice Wang4d3059a2023-02-15 10:24:33 +000017use crate::dice::{Cdi, CdiValues, DiceArtifacts, InputValues, CDI_SIZE};
Alice Wang1a933a82023-02-14 11:02:51 +000018use crate::error::{check_result, DiceError, Result};
Andrew Scull0275aa52023-08-18 18:03:11 +000019use 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 Wang9c40eca2023-02-03 13:10:24 +000023};
24use std::{ffi::CStr, ptr};
25
Andrew Scull0275aa52023-08-18 18:03:11 +000026/// 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 Wang9c40eca2023-02-03 13:10:24 +000028pub fn bcc_format_config_descriptor(
29 name: Option<&CStr>,
30 version: Option<u64>,
31 resettable: bool,
32 buffer: &mut [u8],
33) -> Result<usize> {
Andrew Scull0275aa52023-08-18 18:03:11 +000034 let mut configs = 0;
Alice Wang9c40eca2023-02-03 13:10:24 +000035 if name.is_some() {
Andrew Scull0275aa52023-08-18 18:03:11 +000036 configs |= DICE_ANDROID_CONFIG_COMPONENT_NAME;
Alice Wang9c40eca2023-02-03 13:10:24 +000037 }
38 if version.is_some() {
Andrew Scull0275aa52023-08-18 18:03:11 +000039 configs |= DICE_ANDROID_CONFIG_COMPONENT_VERSION;
Alice Wang9c40eca2023-02-03 13:10:24 +000040 }
41 if resettable {
Andrew Scull0275aa52023-08-18 18:03:11 +000042 configs |= DICE_ANDROID_CONFIG_RESETTABLE;
Alice Wang9c40eca2023-02-03 13:10:24 +000043 }
44
Andrew Scull0275aa52023-08-18 18:03:11 +000045 let values = DiceAndroidConfigValues {
46 configs,
Alice Wang9c40eca2023-02-03 13:10:24 +000047 component_name: name.map_or(ptr::null(), |p| p.as_ptr()),
48 component_version: version.unwrap_or(0),
Andrew Scull0275aa52023-08-18 18:03:11 +000049 security_version: 0,
Alice Wang9c40eca2023-02-03 13:10:24 +000050 };
51
52 let mut buffer_size = 0;
Alice Wangef999242023-05-22 11:14:59 +000053 check_result(
Andrew Walbran7f30e542023-07-07 13:42:25 +010054 // 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 Wangef999242023-05-22 11:14:59 +000056 unsafe {
Andrew Scull0275aa52023-08-18 18:03:11 +000057 DiceAndroidFormatConfigDescriptor(
58 &values,
59 buffer.len(),
60 buffer.as_mut_ptr(),
61 &mut buffer_size,
62 )
Alice Wangef999242023-05-22 11:14:59 +000063 },
64 buffer_size,
65 )?;
Alice Wang9c40eca2023-02-03 13:10:24 +000066 Ok(buffer_size)
67}
Alice Wangf4bd1c62023-02-08 08:38:44 +000068
Andrew Scull0275aa52023-08-18 18:03:11 +000069/// Executes the main Android DICE flow.
Alice Wangf4bd1c62023-02-08 08:38:44 +000070///
Andrew Scull0275aa52023-08-18 18:03:11 +000071/// 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 Wangf4bd1c62023-02-08 08:38:44 +000073pub fn bcc_main_flow(
74 current_cdi_attest: &Cdi,
75 current_cdi_seal: &Cdi,
Andrew Scull0275aa52023-08-18 18:03:11 +000076 current_chain: &[u8],
Alice Wangf4bd1c62023-02-08 08:38:44 +000077 input_values: &InputValues,
78 next_cdi_values: &mut CdiValues,
Andrew Scull0275aa52023-08-18 18:03:11 +000079 next_chain: &mut [u8],
Alice Wangf4bd1c62023-02-08 08:38:44 +000080) -> Result<usize> {
Andrew Scull0275aa52023-08-18 18:03:11 +000081 let mut next_chain_size = 0;
Alice Wangef999242023-05-22 11:14:59 +000082 check_result(
Andrew Scull0275aa52023-08-18 18:03:11 +000083 // 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 Walbran7f30e542023-07-07 13:42:25 +010086 // The first argument can be null and is not used in the current implementation.
Alice Wangef999242023-05-22 11:14:59 +000087 unsafe {
Andrew Scull0275aa52023-08-18 18:03:11 +000088 DiceAndroidMainFlow(
Alice Wangef999242023-05-22 11:14:59 +000089 ptr::null_mut(), // context
90 current_cdi_attest.as_ptr(),
91 current_cdi_seal.as_ptr(),
Andrew Scull0275aa52023-08-18 18:03:11 +000092 current_chain.as_ptr(),
93 current_chain.len(),
Alice Wangef999242023-05-22 11:14:59 +000094 input_values.as_ptr(),
Andrew Scull0275aa52023-08-18 18:03:11 +000095 next_chain.len(),
96 next_chain.as_mut_ptr(),
97 &mut next_chain_size,
Alice Wangef999242023-05-22 11:14:59 +000098 next_cdi_values.cdi_attest.as_mut_ptr(),
99 next_cdi_values.cdi_seal.as_mut_ptr(),
100 )
101 },
Andrew Scull0275aa52023-08-18 18:03:11 +0000102 next_chain_size,
Alice Wangef999242023-05-22 11:14:59 +0000103 )?;
Andrew Scull0275aa52023-08-18 18:03:11 +0000104 Ok(next_chain_size)
Alice Wangf4bd1c62023-02-08 08:38:44 +0000105}
Alice Wang9b2c38e2023-02-14 08:53:31 +0000106
Andrew Scull0275aa52023-08-18 18:03:11 +0000107/// Executes the main Android DICE handover flow.
Alice Wang9b2c38e2023-02-14 08:53:31 +0000108///
Andrew Scull0275aa52023-08-18 18:03:11 +0000109/// 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 Wang9b2c38e2023-02-14 08:53:31 +0000111/// bundle for the next stage.
112pub fn bcc_handover_main_flow(
Andrew Scull0275aa52023-08-18 18:03:11 +0000113 current_handover: &[u8],
Alice Wang9b2c38e2023-02-14 08:53:31 +0000114 input_values: &InputValues,
Andrew Scull0275aa52023-08-18 18:03:11 +0000115 next_handover: &mut [u8],
Alice Wang9b2c38e2023-02-14 08:53:31 +0000116) -> Result<usize> {
Andrew Scull0275aa52023-08-18 18:03:11 +0000117 let mut next_handover_size = 0;
Alice Wangef999242023-05-22 11:14:59 +0000118 check_result(
Andrew Scull0275aa52023-08-18 18:03:11 +0000119 // SAFETY: The function only reads `current_handover` and writes to `next_handover`
Andrew Walbran7f30e542023-07-07 13:42:25 +0100120 // 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 Wangef999242023-05-22 11:14:59 +0000123 unsafe {
Andrew Scull0275aa52023-08-18 18:03:11 +0000124 DiceAndroidHandoverMainFlow(
Alice Wangef999242023-05-22 11:14:59 +0000125 ptr::null_mut(), // context
Andrew Scull0275aa52023-08-18 18:03:11 +0000126 current_handover.as_ptr(),
127 current_handover.len(),
Alice Wangef999242023-05-22 11:14:59 +0000128 input_values.as_ptr(),
Andrew Scull0275aa52023-08-18 18:03:11 +0000129 next_handover.len(),
130 next_handover.as_mut_ptr(),
131 &mut next_handover_size,
Alice Wangef999242023-05-22 11:14:59 +0000132 )
133 },
Andrew Scull0275aa52023-08-18 18:03:11 +0000134 next_handover_size,
Alice Wangef999242023-05-22 11:14:59 +0000135 )?;
Alice Wang9b2c38e2023-02-14 08:53:31 +0000136
Andrew Scull0275aa52023-08-18 18:03:11 +0000137 Ok(next_handover_size)
Alice Wang9b2c38e2023-02-14 08:53:31 +0000138}
Alice Wang1a933a82023-02-14 11:02:51 +0000139
Andrew Scull0275aa52023-08-18 18:03:11 +0000140/// 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 Wangacee4fb2023-02-15 09:42:07 +0000143#[derive(Debug)]
Alice Wang1a933a82023-02-14 11:02:51 +0000144pub struct BccHandover<'a> {
145 /// Attestation CDI.
Alice Wang4d3059a2023-02-15 10:24:33 +0000146 cdi_attest: &'a [u8; CDI_SIZE],
Alice Wang1a933a82023-02-14 11:02:51 +0000147 /// Sealing CDI.
Alice Wang4d3059a2023-02-15 10:24:33 +0000148 cdi_seal: &'a [u8; CDI_SIZE],
Andrew Scull0275aa52023-08-18 18:03:11 +0000149 /// DICE chain.
Alice Wang4d3059a2023-02-15 10:24:33 +0000150 bcc: Option<&'a [u8]>,
151}
152
153impl<'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 Wang1a933a82023-02-14 11:02:51 +0000165}
166
Andrew Scull0275aa52023-08-18 18:03:11 +0000167/// 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.
169pub fn bcc_handover_parse(handover: &[u8]) -> Result<BccHandover> {
Alice Wang1a933a82023-02-14 11:02:51 +0000170 let mut cdi_attest: *const u8 = ptr::null();
171 let mut cdi_seal: *const u8 = ptr::null();
Andrew Scull0275aa52023-08-18 18:03:11 +0000172 let mut chain: *const u8 = ptr::null();
173 let mut chain_size = 0;
Alice Wangef999242023-05-22 11:14:59 +0000174 check_result(
Andrew Scull0275aa52023-08-18 18:03:11 +0000175 // 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 Wangef999242023-05-22 11:14:59 +0000177 unsafe {
Andrew Scull0275aa52023-08-18 18:03:11 +0000178 DiceAndroidHandoverParse(
179 handover.as_ptr(),
180 handover.len(),
Alice Wangef999242023-05-22 11:14:59 +0000181 &mut cdi_attest,
182 &mut cdi_seal,
Andrew Scull0275aa52023-08-18 18:03:11 +0000183 &mut chain,
184 &mut chain_size,
Alice Wangef999242023-05-22 11:14:59 +0000185 )
186 },
Andrew Scull0275aa52023-08-18 18:03:11 +0000187 chain_size,
Alice Wangef999242023-05-22 11:14:59 +0000188 )?;
Andrew Scull0275aa52023-08-18 18:03:11 +0000189 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 Wang1a933a82023-02-14 11:02:51 +0000192 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.
201fn 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 Wangacee4fb2023-02-15 09:42:07 +0000209 start.checked_add(len).and_then(|end| buffer.get(start..end)).ok_or(DiceError::PlatformError)
Alice Wang1a933a82023-02-14 11:02:51 +0000210}