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 | |
| 15 | //! This module implements a retry version for multiple DICE functions that |
| 16 | //! require preallocated output buffer. As the retry functions require |
| 17 | //! memory allocation on heap, currently we only expose these functions in |
| 18 | //! std environment. |
| 19 | |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame^] | 20 | use crate::bcc::{bcc_format_config_descriptor, bcc_main_flow}; |
| 21 | use crate::dice::{Cdi, CdiValues, InputValues}; |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 22 | use crate::error::{DiceError, Result}; |
| 23 | use std::ffi::CStr; |
| 24 | |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame^] | 25 | /// Artifacts stores a set of dice artifacts comprising CDI_ATTEST, CDI_SEAL, |
| 26 | /// and the BCC formatted attestation certificate chain. |
| 27 | /// As we align with the DICE standards today, this is the certificate chain |
| 28 | /// is also called DICE certificate chain. |
| 29 | pub struct OwnedDiceArtifacts { |
| 30 | /// CDI Values. |
| 31 | pub cdi_values: CdiValues, |
| 32 | /// Boot Certificate Chain. |
| 33 | pub bcc: Vec<u8>, |
| 34 | } |
| 35 | |
Alice Wang | 9c40eca | 2023-02-03 13:10:24 +0000 | [diff] [blame] | 36 | /// Retries the given function with bigger output buffer size. |
| 37 | fn retry_with_bigger_buffer<F>(mut f: F) -> Result<Vec<u8>> |
| 38 | where |
| 39 | F: FnMut(&mut Vec<u8>) -> Result<usize>, |
| 40 | { |
| 41 | const INITIAL_BUFFER_SIZE: usize = 256; |
| 42 | const MAX_BUFFER_SIZE: usize = 64 * 1024 * 1024; |
| 43 | |
| 44 | let mut buffer = vec![0u8; INITIAL_BUFFER_SIZE]; |
| 45 | while buffer.len() <= MAX_BUFFER_SIZE { |
| 46 | match f(&mut buffer) { |
| 47 | Err(DiceError::BufferTooSmall) => { |
| 48 | let new_size = buffer.len() * 2; |
| 49 | buffer.resize(new_size, 0); |
| 50 | } |
| 51 | Err(e) => return Err(e), |
| 52 | Ok(actual_size) => { |
| 53 | if actual_size > buffer.len() { |
| 54 | panic!( |
| 55 | "actual_size larger than buffer size: open-dice function |
| 56 | may have written past the end of the buffer." |
| 57 | ); |
| 58 | } |
| 59 | buffer.truncate(actual_size); |
| 60 | return Ok(buffer); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | Err(DiceError::PlatformError) |
| 65 | } |
| 66 | |
| 67 | /// Formats a configuration descriptor following the BCC's specification. |
| 68 | pub fn retry_bcc_format_config_descriptor( |
| 69 | name: Option<&CStr>, |
| 70 | version: Option<u64>, |
| 71 | resettable: bool, |
| 72 | ) -> Result<Vec<u8>> { |
| 73 | retry_with_bigger_buffer(|buffer| { |
| 74 | bcc_format_config_descriptor(name, version, resettable, buffer) |
| 75 | }) |
| 76 | } |
Alice Wang | f4bd1c6 | 2023-02-08 08:38:44 +0000 | [diff] [blame^] | 77 | |
| 78 | /// Executes the main BCC flow. |
| 79 | /// |
| 80 | /// Given a full set of input values along with the current BCC and CDI values, |
| 81 | /// computes the next CDI values and matching updated BCC. |
| 82 | pub fn retry_bcc_main_flow( |
| 83 | current_cdi_attest: &Cdi, |
| 84 | current_cdi_seal: &Cdi, |
| 85 | bcc: &[u8], |
| 86 | input_values: &InputValues, |
| 87 | ) -> Result<OwnedDiceArtifacts> { |
| 88 | let mut next_cdi_values = CdiValues::default(); |
| 89 | let next_bcc = retry_with_bigger_buffer(|next_bcc| { |
| 90 | bcc_main_flow( |
| 91 | current_cdi_attest, |
| 92 | current_cdi_seal, |
| 93 | bcc, |
| 94 | input_values, |
| 95 | &mut next_cdi_values, |
| 96 | next_bcc, |
| 97 | ) |
| 98 | })?; |
| 99 | Ok(OwnedDiceArtifacts { cdi_values: next_cdi_values, bcc: next_bcc }) |
| 100 | } |