blob: 58648b88f631007b58684e2ba3abbb4de6ea7c9d [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
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 Wangf4bd1c62023-02-08 08:38:44 +000020use crate::bcc::{bcc_format_config_descriptor, bcc_main_flow};
Alice Wang44f48b22023-02-09 09:51:22 +000021use crate::dice::{dice_main_flow, Cdi, CdiValues, InputValues};
Alice Wang9c40eca2023-02-03 13:10:24 +000022use crate::error::{DiceError, Result};
23use std::ffi::CStr;
24
Alice Wangf4bd1c62023-02-08 08:38:44 +000025/// 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.
29pub struct OwnedDiceArtifacts {
30 /// CDI Values.
31 pub cdi_values: CdiValues,
32 /// Boot Certificate Chain.
33 pub bcc: Vec<u8>,
34}
35
Alice Wang9c40eca2023-02-03 13:10:24 +000036/// Retries the given function with bigger output buffer size.
37fn retry_with_bigger_buffer<F>(mut f: F) -> Result<Vec<u8>>
38where
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.
68pub 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 Wangf4bd1c62023-02-08 08:38:44 +000077
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.
82pub 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}
Alice Wang44f48b22023-02-09 09:51:22 +0000101
102/// Executes the main DICE flow.
103///
104/// Given a full set of input values and the current CDI values, computes the
105/// next CDI values and a matching certificate.
106pub fn retry_dice_main_flow(
107 current_cdi_attest: &Cdi,
108 current_cdi_seal: &Cdi,
109 input_values: &InputValues,
110) -> Result<(CdiValues, Vec<u8>)> {
111 let mut next_cdi_values = CdiValues::default();
112 let next_cdi_certificate = retry_with_bigger_buffer(|next_cdi_certificate| {
113 dice_main_flow(
114 current_cdi_attest,
115 current_cdi_seal,
116 input_values,
117 next_cdi_certificate,
118 &mut next_cdi_values,
119 )
120 })?;
121 Ok((next_cdi_values, next_cdi_certificate))
122}