blob: c28b69107623ae0bdf0d73e50b0274286c6683df [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 Wang4d611772023-02-13 09:45:21 +000021use crate::dice::{dice_main_flow, Cdi, CdiValues, InputValues, PRIVATE_KEY_SEED_SIZE};
Alice Wang9c40eca2023-02-03 13:10:24 +000022use crate::error::{DiceError, Result};
Alice Wang4d611772023-02-13 09:45:21 +000023use crate::ops::generate_certificate;
Alice Wang9c40eca2023-02-03 13:10:24 +000024use std::ffi::CStr;
25
Alice Wangf4bd1c62023-02-08 08:38:44 +000026/// Artifacts stores a set of dice artifacts comprising CDI_ATTEST, CDI_SEAL,
27/// and the BCC formatted attestation certificate chain.
28/// As we align with the DICE standards today, this is the certificate chain
29/// is also called DICE certificate chain.
30pub struct OwnedDiceArtifacts {
31 /// CDI Values.
32 pub cdi_values: CdiValues,
33 /// Boot Certificate Chain.
34 pub bcc: Vec<u8>,
35}
36
Alice Wang9c40eca2023-02-03 13:10:24 +000037/// Retries the given function with bigger output buffer size.
38fn retry_with_bigger_buffer<F>(mut f: F) -> Result<Vec<u8>>
39where
40 F: FnMut(&mut Vec<u8>) -> Result<usize>,
41{
42 const INITIAL_BUFFER_SIZE: usize = 256;
43 const MAX_BUFFER_SIZE: usize = 64 * 1024 * 1024;
44
45 let mut buffer = vec![0u8; INITIAL_BUFFER_SIZE];
46 while buffer.len() <= MAX_BUFFER_SIZE {
47 match f(&mut buffer) {
48 Err(DiceError::BufferTooSmall) => {
49 let new_size = buffer.len() * 2;
50 buffer.resize(new_size, 0);
51 }
52 Err(e) => return Err(e),
53 Ok(actual_size) => {
54 if actual_size > buffer.len() {
55 panic!(
56 "actual_size larger than buffer size: open-dice function
57 may have written past the end of the buffer."
58 );
59 }
60 buffer.truncate(actual_size);
61 return Ok(buffer);
62 }
63 }
64 }
65 Err(DiceError::PlatformError)
66}
67
68/// Formats a configuration descriptor following the BCC's specification.
69pub fn retry_bcc_format_config_descriptor(
70 name: Option<&CStr>,
71 version: Option<u64>,
72 resettable: bool,
73) -> Result<Vec<u8>> {
74 retry_with_bigger_buffer(|buffer| {
75 bcc_format_config_descriptor(name, version, resettable, buffer)
76 })
77}
Alice Wangf4bd1c62023-02-08 08:38:44 +000078
79/// Executes the main BCC flow.
80///
81/// Given a full set of input values along with the current BCC and CDI values,
82/// computes the next CDI values and matching updated BCC.
83pub fn retry_bcc_main_flow(
84 current_cdi_attest: &Cdi,
85 current_cdi_seal: &Cdi,
86 bcc: &[u8],
87 input_values: &InputValues,
88) -> Result<OwnedDiceArtifacts> {
89 let mut next_cdi_values = CdiValues::default();
90 let next_bcc = retry_with_bigger_buffer(|next_bcc| {
91 bcc_main_flow(
92 current_cdi_attest,
93 current_cdi_seal,
94 bcc,
95 input_values,
96 &mut next_cdi_values,
97 next_bcc,
98 )
99 })?;
100 Ok(OwnedDiceArtifacts { cdi_values: next_cdi_values, bcc: next_bcc })
101}
Alice Wang44f48b22023-02-09 09:51:22 +0000102
103/// Executes the main DICE flow.
104///
105/// Given a full set of input values and the current CDI values, computes the
106/// next CDI values and a matching certificate.
107pub fn retry_dice_main_flow(
108 current_cdi_attest: &Cdi,
109 current_cdi_seal: &Cdi,
110 input_values: &InputValues,
111) -> Result<(CdiValues, Vec<u8>)> {
112 let mut next_cdi_values = CdiValues::default();
113 let next_cdi_certificate = retry_with_bigger_buffer(|next_cdi_certificate| {
114 dice_main_flow(
115 current_cdi_attest,
116 current_cdi_seal,
117 input_values,
118 next_cdi_certificate,
119 &mut next_cdi_values,
120 )
121 })?;
122 Ok((next_cdi_values, next_cdi_certificate))
123}
Alice Wang4d611772023-02-13 09:45:21 +0000124
125/// Generates an X.509 certificate from the given `subject_private_key_seed` and
126/// `input_values`, and signed by `authority_private_key_seed`.
127/// The subject private key seed is supplied here so the implementation can choose
128/// between asymmetric mechanisms, for example ECDSA vs Ed25519.
129/// Returns the generated certificate.
130pub fn retry_generate_certificate(
131 subject_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE],
132 authority_private_key_seed: &[u8; PRIVATE_KEY_SEED_SIZE],
133 input_values: &InputValues,
134) -> Result<Vec<u8>> {
135 retry_with_bigger_buffer(|certificate| {
136 generate_certificate(
137 subject_private_key_seed,
138 authority_private_key_seed,
139 input_values,
140 certificate,
141 )
142 })
143}