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